Show More
@@ -23,20 +23,13 b' class Config:' | |||
|
23 | 23 | if not os.path.exists(os.path.join(d, '.hg')): |
|
24 | 24 | raise ConfigError("%s: not a mercurial repository" % d) |
|
25 | 25 | |
|
26 | try: | |
|
27 | cfg = Config(sys.argv) | |
|
28 | except ConfigError, inst: | |
|
29 | print str(inst) | |
|
30 | usage() | |
|
31 | sys.exit(1) | |
|
32 | ||
|
33 | 26 | def collect(src): |
|
34 | 27 | seplen = len(os.path.sep) |
|
35 | 28 | candidates = [] |
|
36 | 29 | for dirpath, dirnames, filenames in os.walk(src): |
|
37 | 30 | relpath = dirpath[len(src) + seplen:] |
|
38 | 31 | for filename in filenames: |
|
39 |
if not |
|
|
32 | if not filename.endswith('.i'): | |
|
40 | 33 | continue |
|
41 | 34 | st = os.stat(os.path.join(dirpath, filename)) |
|
42 | 35 | candidates.append((os.path.join(relpath, filename), st)) |
@@ -44,25 +37,56 b' def collect(src):' | |||
|
44 | 37 | return candidates |
|
45 | 38 | |
|
46 | 39 | def prune(candidates, dst): |
|
40 | def getdatafile(path): | |
|
41 | if not path.endswith('.i'): | |
|
42 | return None, None | |
|
43 | df = path[:-1] + 'd' | |
|
44 | try: | |
|
45 | st = os.stat(df) | |
|
46 | except OSError: | |
|
47 | return None, None | |
|
48 | return df, st | |
|
49 | ||
|
50 | def linkfilter(dst, st): | |
|
51 | try: | |
|
52 | ts = os.stat(dst) | |
|
53 | except OSError: | |
|
54 | # Destination doesn't have this file? | |
|
55 | return False | |
|
56 | if st.st_ino == ts.st_ino: | |
|
57 | return False | |
|
58 | if st.st_dev != ts.st_dev: | |
|
59 | # No point in continuing | |
|
60 | raise Exception('Source and destination are on different devices') | |
|
61 | if st.st_size != ts.st_size: | |
|
62 | # TODO: compare revlog heads | |
|
63 | return False | |
|
64 | return st | |
|
65 | ||
|
47 | 66 | targets = [] |
|
48 | 67 | for fn, st in candidates: |
|
49 | 68 | tgt = os.path.join(dst, fn) |
|
50 | try: | |
|
51 | ts = os.stat(tgt) | |
|
52 | except OSError: | |
|
53 | # Destination doesn't have this file? | |
|
54 | continue | |
|
55 | if st.st_ino == ts.st_ino: | |
|
56 | continue | |
|
57 | if st.st_dev != ts.st_dev: | |
|
58 | raise Exception('Source and destination are on different devices') | |
|
59 | if st.st_size != ts.st_size: | |
|
69 | ts = linkfilter(tgt, st) | |
|
70 | if not ts: | |
|
60 | 71 | continue |
|
61 | 72 | targets.append((fn, ts.st_size)) |
|
73 | df, ts = getdatafile(tgt) | |
|
74 | if df: | |
|
75 | targets.append((fn[:-1] + 'd', ts.st_size)) | |
|
62 | 76 | |
|
63 | 77 | return targets |
|
64 | 78 | |
|
65 | 79 | def relink(src, dst, files): |
|
80 | def relinkfile(src, dst): | |
|
81 | bak = dst + '.bak' | |
|
82 | os.rename(dst, bak) | |
|
83 | try: | |
|
84 | os.link(src, dst) | |
|
85 | except OSError: | |
|
86 | os.rename(bak, dst) | |
|
87 | raise | |
|
88 | os.remove(bak) | |
|
89 | ||
|
66 | 90 | CHUNKLEN = 65536 |
|
67 | 91 | relinked = 0 |
|
68 | 92 | savedbytes = 0 |
@@ -81,21 +105,22 b' def relink(src, dst, files):' | |||
|
81 | 105 | if sin: |
|
82 | 106 | continue |
|
83 | 107 | try: |
|
84 |
|
|
|
85 | try: | |
|
86 | os.link(source, tgt) | |
|
87 | except OSError: | |
|
88 | os.rename(tgt + '.bak', tgt) | |
|
89 | raise | |
|
108 | relinkfile(source, tgt) | |
|
90 | 109 | print 'Relinked %s' % f |
|
91 | 110 | relinked += 1 |
|
92 | 111 | savedbytes += sz |
|
93 | os.remove(tgt + '.bak') | |
|
94 | 112 | except OSError, inst: |
|
95 | 113 | print '%s: %s' % (tgt, str(inst)) |
|
96 | 114 | |
|
97 | 115 | print 'Relinked %d files (%d bytes reclaimed)' % (relinked, savedbytes) |
|
98 | 116 | |
|
117 | try: | |
|
118 | cfg = Config(sys.argv) | |
|
119 | except ConfigError, inst: | |
|
120 | print str(inst) | |
|
121 | usage() | |
|
122 | sys.exit(1) | |
|
123 | ||
|
99 | 124 | src = os.path.join(cfg.src, '.hg') |
|
100 | 125 | dst = os.path.join(cfg.dst, '.hg') |
|
101 | 126 | candidates = collect(src) |
General Comments 0
You need to be logged in to leave comments.
Login now