-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.py
executable file
·87 lines (68 loc) · 2.84 KB
/
git.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
def prompt_multiple_lines(message):
print message + " (press enter twice to terminate)\n"
lines = []
while lines[-2:] != ["", ""]:
lines.append(raw_input())
return "\n".join(lines).strip()
def call(popenargs, input=None, requested_return=None, check_return_code=False, **kwargs):
if "stdin" not in kwargs and input != None:
kwargs["stdin"] = subprocess.PIPE
if "stdout" not in kwargs and requested_return == "stdout":
kwargs["stdout"] = subprocess.PIPE
elif "stderr" not in kwargs and requested_return == "stderr":
kwargs["stderr"] = subprocess.PIPE
p = subprocess.Popen(popenargs, **kwargs)
stdout, stderr = p.communicate(input)
output = p.returncode
if requested_return == "stdout":
output = stdout
elif requested_return == "stderr":
output = stderr
elif requested_return == "code":
output = p.returncode
return output
def check_call(popenargs, input=None, requested_return="", **kwargs):
return call(popenargs, input=input, requested_return=requested_return, check_return_code=True, **kwargs)
def check_call_and_get_stdout_and_stderr(popenargs, **kwargs):
return check_call(popenargs, requested_return="stdout", stderr=subprocess.STDOUT, **kwargs)
def git(space_delimited_args, *args):
list_of_args = space_delimited_args.split() + list(args)
print "Running git %s" % " ".join(list_of_args)
return check_call_and_get_stdout_and_stderr(["git"] + list_of_args)
def current_branch():
for branch in git("branch").split("\n"):
if branch.startswith("*"):
return branch[2:]
raise RuntimeError("No current branch")
def git_commit(message):
return git("commit -am " + message)
def git_fetch():
return git("fetch")
def git_subtree_merge(subtree, prefix):
git_subtree_pull(subtree, prefix)
git_subtree_push(subtree, prefix)
def git_subtree_pull(subtree, prefix):
default_pull_message = "[%s] Merging %s into %s" % (subtree, subtree, current_branch())
git("subtree pull", "--prefix=%s" % prefix, "--squash", "-m", default_pull_message, subtree, "master")
def git_subtree_push(subtree, prefix):
git("subtree push", "--prefix=%s" % prefix, subtree, "master")
def git_pa():
git_subtree_push("scripts", "scripts")
git_subtree_push("primary", "app/assets/stylesheets/primary")
def git_up():
git_fetch()
git_subtree_pull("scripts", "scripts")
git_subtree_pull("primary", "app/assets/stylesheets/primary")
initial_branch = current_branch()
branches = ["master"]
previous_branch = None
for branch in branches:
git("checkout", branch)
git("merge origin/%s" % branch)
if previous_branch:
git("merge", previous_branch)
previous_branch = branch
git("checkout", initial_branch)