-
Notifications
You must be signed in to change notification settings - Fork 21
/
git-alternate
executable file
·61 lines (50 loc) · 1.54 KB
/
git-alternate
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
#!/usr/bin/env python
# vim: et
from __future__ import with_statement
import os
import sys
import commands
import exceptions
def nearest_git_objects(path):
dotgit = os.path.join(path, ".git")
objects = os.path.join(path, "objects")
if os.path.isdir(dotgit):
return nearest_git_objects(dotgit)
elif os.path.isdir(objects):
return objects
raise exceptions.RuntimeError("Could not find git dir from " + path)
def alt_path(git_path):
return os.path.join(git_path, "info", "alternates")
def read_objs(git_path):
fn = alt_path(git_path)
alts = set()
if os.path.exists(fn):
with open(fn) as f:
alts = set([l.strip() for l in f.readlines()])
return alts
def setup_alternates(from_objects, to_objects):
alts = read_objs(from_objects)
alts.add(to_objects)
with open(alt_path(from_objects), "w") as f:
f.write("\n".join(alts) + "\n")
def here():
(e, o) = commands.getstatusoutput("git rev-parse --git-dir")
if e != 0:
raise exceptions.RuntimeError("This is not a git repo.")
return nearest_git_objects(o.strip())
def setup_new_alternate(where):
alt = nearest_git_objects(where)
loc = here()
print loc, "->", alt
setup_alternates(loc, alt)
def display_alternates():
p = here()
alts = read_objs(p)
print "Alternates for %s (%d):" % (p, len(alts))
for s in sorted(alts):
print " %s" % s
if __name__ == '__main__':
if len(sys.argv) < 2:
display_alternates()
else:
setup_new_alternate(sys.argv[1])