comparison env/lib/python3.7/site-packages/planemo/git.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 """Utilities for interacting with git using planemo abstractions."""
2 from __future__ import absolute_import
3
4 import os
5 import subprocess
6
7 from galaxy.util import unicodify
8
9 from planemo import io
10
11
12 def git_env_for(path):
13 """Setup env dictionary to target specified git repo with git commands."""
14 env = {
15 "GIT_WORK_DIR": path,
16 "GIT_DIR": os.path.join(path, ".git")
17 }
18 return env
19
20
21 def add(ctx, repo_path, file_path):
22 env = git_env_for(repo_path)
23 io.communicate("cd '%s' && git add '%s'" % (repo_path, os.path.abspath(file_path)), env=env)
24
25
26 def commit(ctx, repo_path, message=""):
27 env = git_env_for(repo_path)
28 io.communicate(["git", "commit", "-m", message], env=env)
29
30
31 def push(ctx, repo_path, to, branch, force=False):
32 env = git_env_for(repo_path)
33 cmd = ["git", "push"]
34 if force:
35 cmd += ["--force"]
36 cmd += [to, branch]
37 io.communicate(cmd, env=env)
38
39
40 def branch(ctx, repo_path, branch, from_branch=None):
41 env = git_env_for(repo_path)
42 cmd = ["git", "checkout", "-b", branch]
43 if from_branch is not None:
44 cmd.append(from_branch)
45 io.communicate(cmd, env=env)
46
47
48 def checkout(ctx, remote_repo, local_path, branch=None, remote="origin", from_branch="master"):
49 """Checkout a new branch from a remote repository."""
50 env = git_env_for(local_path)
51 if not os.path.exists(local_path):
52 io.communicate(command_clone(ctx, remote_repo, local_path))
53 else:
54 io.communicate(["git", "fetch", remote], env=env)
55
56 if branch:
57 io.communicate(["git", "checkout", "%s/%s" % (remote, from_branch), "-b", branch], env=env)
58 else:
59 io.communicate(["git", "merge", "--ff-only", "%s/%s" % (remote, from_branch)], env=env)
60
61
62 def command_clone(ctx, src, dest, mirror=False, branch=None):
63 """Produce a command-line string to clone a repository.
64
65 Take in ``ctx`` to allow more configurability down the road.
66 """
67 cmd = ['git', 'clone']
68 if mirror:
69 cmd.append("--mirror")
70 if branch is not None:
71 cmd.extend(["--branch", branch])
72 cmd.extend([src, dest])
73 return cmd
74
75
76 def diff(ctx, directory, range):
77 """Produce a list of diff-ed files for commit range."""
78 cmd_template = "cd '%s' && git diff --name-only '%s' --"
79 cmd = cmd_template % (directory, range)
80 stdout, _ = io.communicate(
81 cmd,
82 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
83 universal_newlines=True
84 )
85 return [l.strip() for l in unicodify(stdout).splitlines() if l]
86
87
88 def clone(*args, **kwds):
89 """Clone a git repository.
90
91 See :func:`command_clone` for description of arguments.
92 """
93 command = command_clone(*args, **kwds)
94 return io.communicate(command)
95
96
97 def rev(ctx, directory):
98 """Raw revision for git directory specified.
99
100 Throws ``RuntimeError`` if not a git directory.
101 """
102 cmd_template = "cd '%s' && git rev-parse HEAD"
103 cmd = cmd_template % directory
104 stdout, _ = io.communicate(
105 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
106 )
107 return unicodify(stdout).strip()
108
109
110 def is_rev_dirty(ctx, directory):
111 """Check if specified git repository has uncommitted changes."""
112 return io.shell(['git', 'diff', '--quiet'], cwd=directory) != 0
113
114
115 def rev_if_git(ctx, directory):
116 """Determine git revision (or ``None``)."""
117 try:
118 the_rev = rev(ctx, directory)
119 is_dirty = is_rev_dirty(ctx, directory)
120 if is_dirty:
121 the_rev += "-dirty"
122 return the_rev
123 except RuntimeError:
124 return None