-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgitdater.py
94 lines (73 loc) · 2.93 KB
/
gitdater.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
87
88
89
90
91
92
93
94
#!/usr/bin/python
import re
from os import walk
from sys import stdout as sys_stdout
from subprocess import Popen, PIPE
def main():
for root, dirs, _ in walk("."):
if ".git" in dirs:
print("\"%s\" has git dir: \"%s\"" % (root, dirs))
dirs.remove('.git')
update(root)
print
def update(root):
"""
Updates the program via git pull.
"""
print("Checking for updates...")
process = Popen("git pull", cwd=root, shell=True,
stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
success = not process.returncode
if success:
updated = "Already" not in stdout
process = Popen("git rev-parse --verify HEAD", cwd=root, shell=True,
stdout=PIPE, stderr=PIPE)
stdout, _ = process.communicate()
revision = (stdout[:7] if stdout and
re.search(r"(?i)[0-9a-f]{32}", stdout) else "-")
print("%s the latest revision '%s'." %
("Already at" if not updated else "Updated to", revision))
else:
print("Problem occurred while updating program.\n")
_ = re.search(r"(?P<error>error:[^:]*files\swould\sbe\soverwritten"
r"\sby\smerge:(?:\n\t[^\n]+)*)", stderr)
if _:
def question():
"""Asks question until a valid answer of y or n is provided."""
print("\nWould you like to overwrite your changes and set "
"your local copy to the latest commit?")
sys_stdout.write("ALL of your local changes will be deleted"
" [Y/n]: ")
_ = raw_input()
if not _:
_ = "y"
if _.lower() == "n":
return False
elif _.lower() == "y":
return True
else:
print("Did not understand your answer! Try again.")
question()
print("%s" % _.group("error"))
if not question():
return
if "untracked" in stderr:
cmd = "git clean -df"
else:
cmd = "git reset --hard"
process = Popen(cmd, cwd=root, shell=True,
stdout=PIPE, stderr=PIPE)
stdout, _ = process.communicate()
if "HEAD is now at" in stdout:
print("\nLocal copy reset to current git branch.")
print("Attemping to run update again...\n")
else:
print("Unable to reset local copy to current git branch.")
return
update(root)
else:
print("Please make sure that you have a 'git' package installed.")
print(stderr)
if __name__ == "__main__":
main()