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