]> arthur.barton.de Git - bup.git/blob - cmd/index-cmd.py
Move cmd-*.py to cmd/*-cmd.py.
[bup.git] / cmd / index-cmd.py
1 #!/usr/bin/env python
2 import os, 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         wi.abort()
112     else:
113         wi.close()
114
115
116 optspec = """
117 bup index <-p|m|u> [options...] <filenames...>
118 --
119 p,print    print the index entries for the given names (also works with -u)
120 m,modified print only added/deleted/modified files (implies -p)
121 s,status   print each filename with a status char (A/M/D) (implies -p)
122 H,hash     print the hash for each object next to its name (implies -p)
123 l,long     print more information about each file
124 u,update   (recursively) update the index entries for the given filenames
125 x,xdev,one-file-system  don't cross filesystem boundaries
126 fake-valid mark all index entries as up-to-date even if they aren't
127 fake-invalid mark all index entries as invalid
128 check      carefully check index file integrity
129 f,indexfile=  the name of the index file (default 'index')
130 v,verbose  increase log output (can be used more than once)
131 """
132 o = options.Options('bup index', optspec)
133 (opt, flags, extra) = o.parse(sys.argv[1:])
134
135 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
136     o.fatal('supply one or more of -p, -s, -m, -u, or --check')
137 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
138     o.fatal('--fake-{in,}valid are meaningless without -u')
139 if opt.fake_valid and opt.fake_invalid:
140     o.fatal('--fake-valid is incompatible with --fake-invalid')
141
142 git.check_repo_or_die()
143 indexfile = opt.indexfile or git.repo('bupindex')
144
145 if opt.check:
146     log('check: starting initial check.\n')
147     check_index(index.Reader(indexfile))
148
149 paths = index.reduce_paths(extra)
150
151 if opt.update:
152     if not paths:
153         o.fatal('update (-u) requested but no paths given')
154     for (rp,path) in paths:
155         update_index(rp)
156
157 if opt['print'] or opt.status or opt.modified:
158     for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
159         if (opt.modified 
160             and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
161             continue
162         line = ''
163         if opt.status:
164             if ent.is_deleted():
165                 line += 'D '
166             elif not ent.is_valid():
167                 if ent.sha == index.EMPTY_SHA:
168                     line += 'A '
169                 else:
170                     line += 'M '
171             else:
172                 line += '  '
173         if opt.hash:
174             line += ent.sha.encode('hex') + ' '
175         if opt.long:
176             line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
177         print line + (name or './')
178
179 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
180     log('check: starting final check.\n')
181     check_index(index.Reader(indexfile))
182
183 if saved_errors:
184     log('WARNING: %d errors encountered.\n' % len(saved_errors))
185     sys.exit(1)