-
Notifications
You must be signed in to change notification settings - Fork 73
/
git_repo_diff.py
executable file
·77 lines (67 loc) · 2.73 KB
/
git_repo_diff.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
#!/usr/bin/env python
"""
Python script to tell what branches differ between two git repos,
both by existence and head commit.
Reqires GitPython>=0.3.2.RC1
##################
Copyright 2014 Jason Antman <[email protected]> <http://www.jasonantman.com>
Free for any use provided that patches are submitted back to me.
The latest version of this script can be found at:
<https://github.com/jantman/misc-scripts/blob/master/git_repo_diff.py>
CHANGELOG:
- initial script
"""
import os
import sys
import git
def main():
if len(sys.argv) < 2:
print("USAGE: git_repo_diff.py path1 path2")
sys.exit(2)
repos = {sys.argv[1]: {'other': sys.argv[2]}, sys.argv[2]: {'other': sys.argv[1]}}
for p in repos:
if not os.path.exists(p) or not os.path.isdir(p):
print("ERROR: %s is not a directory or does not exist." % p)
sys.exit(1)
d = os.path.join(p, '.git')
if not os.path.exists(d) or not os.path.isdir(d):
print("ERROR: %s does not appear to be a git clone" % p)
sys.exit(1)
o = git.Repo(p)
if o.bare:
print("ERROR: repo in %s is bare." % p)
sys.exit(1)
repos[p]['obj'] = o
# find the branches and their commits
origin = o.remotes.origin
origin.fetch()
refs = {}
for b in origin.refs:
foo = {}
foo['hexsha'] = b.commit.hexsha
foo['author'] = b.commit.author
foo['date'] = b.commit.authored_date
foo['message'] = b.commit.message
refs[b] = foo
repos[p]['refs'] = refs
# now compare them
reported = []
for p in repos:
other = repos[p]['other']
for ref in repos[p]['refs']:
if ref not in repos[other]['refs']:
print("ref %s in %s but not %s" % (ref, p, other))
else:
if repos[p]['refs'][ref]['hexsha'] != repos[other]['refs'][ref]['hexsha']:
if ref in reported:
continue
print("ref %s differs between repos:" % ref)
print("\t%s: sha=%s author=%s date=%s" % (p, repos[p]['refs'][ref]['hexsha'],
repos[p]['refs'][ref]['author'],
repos[p]['refs'][ref]['date']))
print("\t%s: sha=%s author=%s date=%s" % (other, repos[other]['refs'][ref]['hexsha'],
repos[other]['refs'][ref]['author'],
repos[other]['refs'][ref]['date']))
reported.append(ref)
if __name__ == "__main__":
main()