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