""""Wrapper around a few git commands. Used by builder to determin document
version.
"""

import sys
import subprocess
from os.path import join


def get_version():
    """Generate document version based on git tag and number of revisions."""

    components = []

    try:
        components.append(
            subprocess.check_output(
                "git describe --abbrev=0",
                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                shell=True
            ).strip().decode()
        )
        components.append(
            subprocess.check_output(
                "git rev-list --count master",
                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                shell=True
            ).strip().decode()
        )
    except:
        pass

    return '.'.join(components) if components else None


def sync_repo(repo_url, target_dir, revision="master"):
    """Clone git repository if it's not cloned yet or pull changes otherwise.
    """

    repo_name = repo_url.split('/')[-1].split('.')[0]
    repo_path = join(target_dir, repo_name)

    print("Syncing %s at %s..." % (repo_url, revision), end=' ')
    sys.stdout.flush()

    if subprocess.run(
        "git clone %s %s" % (repo_url, repo_path),
        stdout=subprocess.PIPE, stderr=subprocess.PIPE,
        shell=True
    ):
        subprocess.run(
            "git remote update origin --prune",
            cwd=repo_path,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
            shell=True
        )

    subprocess.run(
        "git checkout %s" % revision,
        cwd=repo_path,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE,
        shell=True
    )

    print("Done!")


if __name__ == "__main__":
    sync_repo(
        "git@git.restr.im:docs-itv/doorstopper_itv.git",
        "foliantcache",
        revision="develop"
    )

@@ -1318,16 +1318,27 @@
 ue%0A    )
+.returncode
 :%0A      
@@ -1711,161 +1711,25 @@
 %22)%0A%0A
-%0Aif __name__ == %22__main__%22:%0A    sync_repo(%0A        %22git@git.restr.im:docs-itv/doorstopper_itv.git%22,%0A        %22foliantcache%22,%0A        revision=%22develop%22%0A    )
+    return repo_path
 %0A

Remove redundant update after clone.