]> arthur.barton.de Git - bup.git/blob - cmd/index-cmd.py
import cleanup
[bup.git] / cmd / index-cmd.py
1 #!/usr/bin/env python
2 import sys, stat, time
3 from bup import options, git, index, drecurse
4 from bup.helpers import *
5
6
7 def merge_indexes(out, r1, r2):
8     for e in index.MergeIter([r1, r2]):
9         # FIXME: shouldn't we remove deleted entries eventually?  When?
10         out.add_ixentry(e)
11
12
13 class IterHelper:
14     def __init__(self, l):
15         self.i = iter(l)
16         self.cur = None
17         self.next()
18
19     def next(self):
20         try:
21             self.cur = self.i.next()
22         except StopIteration:
23             self.cur = None
24         return self.cur
25
26
27 def check_index(reader):
28     try:
29         log('check: checking forward iteration...\n')
30         e = None
31         d = {}
32         for e in reader.forward_iter():
33             if e.children_n:
34                 if opt.verbose:
35                     log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n,
36                                             e.name))
37                 assert(e.children_ofs)
38                 assert(e.name.endswith('/'))
39                 assert(not d.get(e.children_ofs))
40                 d[e.children_ofs] = 1
41             if e.flags & index.IX_HASHVALID:
42                 assert(e.sha != index.EMPTY_SHA)
43                 assert(e.gitmode)
44         assert(not e or e.name == '/')  # last entry is *always* /
45         log('check: checking normal iteration...\n')
46         last = None
47         for e in reader:
48             if last:
49                 assert(last > e.name)
50             last = e.name
51     except:
52         log('index error! at %r\n' % e)
53         raise
54     log('check: passed.\n')
55
56
57 def update_index(top):
58     ri = index.Reader(indexfile)
59     wi = index.Writer(indexfile)
60     rig = IterHelper(ri.iter(name=top))
61     tstart = int(time.time())
62
63     hashgen = None
64     if opt.fake_valid:
65         def hashgen(name):
66             return (0100644, index.FAKE_SHA)
67
68     total = 0
69     for (path,pst) in drecurse.recursive_dirlist([top], xdev=opt.xdev):
70         if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
71             sys.stdout.write('%s\n' % path)
72             sys.stdout.flush()
73             progress('Indexing: %d\r' % total)
74         elif not (total % 128):
75             progress('Indexing: %d\r' % total)
76         total += 1
77         while rig.cur and rig.cur.name > path:  # deleted paths
78             if rig.cur.exists():
79                 rig.cur.set_deleted()
80                 rig.cur.repack()
81             rig.next()
82         if rig.cur and rig.cur.name == path:    # paths that already existed
83             if pst:
84                 rig.cur.from_stat(pst, tstart)
85             if not (rig.cur.flags & index.IX_HASHVALID):
86                 if hashgen:
87                     (rig.cur.gitmode, rig.cur.sha) = hashgen(path)
88                     rig.cur.flags |= index.IX_HASHVALID
89             if opt.fake_invalid:
90                 rig.cur.invalidate()
91             rig.cur.repack()
92             rig.next()
93         else:  # new paths
94             wi.add(path, pst, hashgen = hashgen)
95     progress('Indexing: %d, done.\n' % total)
96     
97     if ri.exists():
98         ri.save()
99         wi.flush()
100         if wi.count:
101             wr = wi.new_reader()
102             if opt.check:
103                 log('check: before merging: oldfile\n')
104                 check_index(ri)
105                 log('check: before merging: newfile\n')
106                 check_index(wr)
107             mi = index.Writer(indexfile)
108             merge_indexes(mi, ri, wr)
109             ri.close()
110             mi.close()
111             wr.close()
112         wi.abort()
113     else:
114         wi.close()
115
116
117 optspec = """
118 bup index <-p|m|u> [options...] <filenames...>
119 --
120 p,print    print the index entries for the given names (also works with -u)
121 m,modified print only added/deleted/modified files (implies -p)
122 s,status   print each filename with a status char (A/M/D) (implies -p)
123 H,hash     print the hash for each object next to its name (implies -p)
124 l,long     print more information about each file
125 u,update   (recursively) update the index entries for the given filenames
126 x,xdev,one-file-system  don't cross filesystem boundaries
127 fake-valid mark all index entries as up-to-date even if they aren't
128 fake-invalid mark all index entries as invalid
129 check      carefully check index file integrity
130 f,indexfile=  the name of the index file (default 'index')
131 v,verbose  increase log output (can be used more than once)
132 """
133 o = options.Options('bup index', optspec)
134 (opt, flags, extra) = o.parse(sys.argv[1:])
135
136 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
137     o.fatal('supply one or more of -p, -s, -m, -u, or --check')
138 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
139     o.fatal('--fake-{in,}valid are meaningless without -u')
140 if opt.fake_valid and opt.fake_invalid:
141     o.fatal('--fake-valid is incompatible with --fake-invalid')
142
143 git.check_repo_or_die()
144 indexfile = opt.indexfile or git.repo('bupindex')
145
146 handle_ctrl_c()
147
148 if opt.check:
149     log('check: starting initial check.\n')
150     check_index(index.Reader(indexfile))
151
152 paths = index.reduce_paths(extra)
153
154 if opt.update:
155     if not paths:
156         o.fatal('update (-u) requested but no paths given')
157     for (rp,path) in paths:
158         update_index(rp)
159
160 if opt['print'] or opt.status or opt.modified:
161     for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
162         if (opt.modified 
163             and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
164             continue
165         line = ''
166         if opt.status:
167             if ent.is_deleted():
168                 line += 'D '
169             elif not ent.is_valid():
170                 if ent.sha == index.EMPTY_SHA:
171                     line += 'A '
172                 else:
173                     line += 'M '
174             else:
175                 line += '  '
176         if opt.hash:
177             line += ent.sha.encode('hex') + ' '
178         if opt.long:
179             line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
180         print line + (name or './')
181
182 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
183     log('check: starting final check.\n')
184     check_index(index.Reader(indexfile))
185
186 if saved_errors:
187     log('WARNING: %d errors encountered.\n' % len(saved_errors))
188     sys.exit(1)