Search code examples
gitsnakemake

How to include the current commit in a snakemake workflow for later reference?


My snakemake workflow lives under version control in a git repository. I'm after a possibility to include something like the output of git describe --always to track the git tag/commit the workflow repository is currently checked out to during runtime of the workflow.

I added a generic function in the workflow, that just calls git via subprocess:

def get_git_commit(wildcards):
  label = subprocess.check_output(["git", "describe", "--always"]).strip().decode("utf-8")
  return(label)

The output can be used in rule params, e.g.

  params:
    git_commit = get_git_commit

However, during runtime of the workflow, the command is executed in the working directory, not the source directory of the Snakefile. Is cding back and forth (into the source directory) advisable? Or are there cleaner strategies?


Solution

  • Assuming your Snakefile is in the same folder as the .git folder, you can get the path of the Snakefile (directly in the Snakefile) with:

    wfbasedir = workflow.basedir
    

    You can then invoke git with the parameter --git-dir:

    git --git-dir={wfbasedir}/.git
    

    or your way with:

    def get_git_commit(wildcards):
        label = subprocess.check_output(["git","--git-dir="+wfbasedir+"/.git", "describe", "--always"]).strip().decode("utf-8")
        return(label)