| e7514f6 by Dustin Sallings at 2008-12-27 |
1 |
#!/usr/bin/env python |
| e375909 by Dustin Sallings at 2008-12-30 |
2 |
# vim: et |
| e7514f6 by Dustin Sallings at 2008-12-27 |
3 |
|
|
4 |
from __future__ import with_statement |
|
5 |
|
|
6 |
import os |
|
7 |
import sys |
|
8 |
import commands |
|
9 |
import exceptions |
|
10 |
|
|
11 |
def nearest_git_objects(path): |
| e375909 by Dustin Sallings at 2008-12-30 |
12 |
dotgit = os.path.join(path, ".git") |
|
13 |
objects = os.path.join(path, "objects") |
|
14 |
if os.path.isdir(dotgit): |
|
15 |
return nearest_git_objects(dotgit) |
|
16 |
elif os.path.isdir(objects): |
|
17 |
return objects |
|
18 |
raise exceptions.RuntimeError("Could not find git dir from " + path) |
| e7514f6 by Dustin Sallings at 2008-12-27 |
19 |
|
| 5f0cfdb by Dustin Sallings at 2009-04-12 |
20 |
def alt_path(git_path): |
|
21 |
return os.path.join(git_path, "info", "alternates") |
|
22 |
|
|
23 |
def read_objs(git_path): |
|
24 |
fn = alt_path(git_path) |
|
25 |
alts = set() |
| e375909 by Dustin Sallings at 2008-12-30 |
26 |
if os.path.exists(fn): |
|
27 |
with open(fn) as f: |
|
28 |
alts = set([l.strip() for l in f.readlines()]) |
| 5f0cfdb by Dustin Sallings at 2009-04-12 |
29 |
|
|
30 |
return alts |
|
31 |
|
|
32 |
def setup_alternates(from_objects, to_objects): |
|
33 |
alts = read_objs(from_objects) |
| e375909 by Dustin Sallings at 2008-12-30 |
34 |
alts.add(to_objects) |
| 5f0cfdb by Dustin Sallings at 2009-04-12 |
35 |
with open(alt_path(from_objects), "w") as f: |
| e375909 by Dustin Sallings at 2008-12-30 |
36 |
f.write("\n".join(alts) + "\n") |
| e7514f6 by Dustin Sallings at 2008-12-27 |
37 |
|
| 5f0cfdb by Dustin Sallings at 2009-04-12 |
38 |
def here(): |
| e375909 by Dustin Sallings at 2008-12-30 |
39 |
(e, o) = commands.getstatusoutput("git rev-parse --git-dir") |
|
40 |
if e != 0: |
|
41 |
raise exceptions.RuntimeError("This is not a git repo.") |
| 5f0cfdb by Dustin Sallings at 2009-04-12 |
42 |
return nearest_git_objects(o.strip()) |
|
43 |
|
|
44 |
def setup_new_alternate(where): |
|
45 |
alt = nearest_git_objects(where) |
|
46 |
loc = here() |
| e375909 by Dustin Sallings at 2008-12-30 |
47 |
print loc, "->", alt |
|
48 |
setup_alternates(loc, alt) |
| 5f0cfdb by Dustin Sallings at 2009-04-12 |
49 |
|
|
50 |
def display_alternates(): |
|
51 |
p = here() |
|
52 |
alts = read_objs(p) |
|
53 |
print "Alternates for %s (%d):" % (p, len(alts)) |
|
54 |
for s in sorted(alts): |
|
55 |
print " %s" % s |
|
56 |
|
|
57 |
if __name__ == '__main__': |
|
58 |
if len(sys.argv) < 2: |
|
59 |
display_alternates() |
|
60 |
else: |
|
61 |
setup_new_alternate(sys.argv[1]) |