3 from bup import options, git, index, drecurse
4 from bup.helpers import *
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?
14 def __init__(self, l):
21 self.cur = self.i.next()
27 def check_index(reader):
29 log('check: checking forward iteration...\n')
32 for e in reader.forward_iter():
35 log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n,
37 assert(e.children_ofs)
38 assert(e.name.endswith('/'))
39 assert(not d.get(e.children_ofs))
41 if e.flags & index.IX_HASHVALID:
42 assert(e.sha != index.EMPTY_SHA)
44 assert(not e or e.name == '/') # last entry is *always* /
45 log('check: checking normal iteration...\n')
52 log('index error! at %r\n' % e)
54 log('check: passed.\n')
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())
66 return (0100644, index.FAKE_SHA)
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)
73 progress('Indexing: %d\r' % total)
74 elif not (total % 128):
75 progress('Indexing: %d\r' % total)
77 while rig.cur and rig.cur.name > path: # deleted paths
82 if rig.cur and rig.cur.name == path: # paths that already existed
84 rig.cur.from_stat(pst, tstart)
85 if not (rig.cur.flags & index.IX_HASHVALID):
87 (rig.cur.gitmode, rig.cur.sha) = hashgen(path)
88 rig.cur.flags |= index.IX_HASHVALID
94 wi.add(path, pst, hashgen = hashgen)
95 progress('Indexing: %d, done.\n' % total)
103 log('check: before merging: oldfile\n')
105 log('check: before merging: newfile\n')
107 mi = index.Writer(indexfile)
108 merge_indexes(mi, ri, wr)
118 bup index <-p|m|u> [options...] <filenames...>
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)
133 o = options.Options('bup index', optspec)
134 (opt, flags, extra) = o.parse(sys.argv[1:])
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')
143 git.check_repo_or_die()
144 indexfile = opt.indexfile or git.repo('bupindex')
149 log('check: starting initial check.\n')
150 check_index(index.Reader(indexfile))
152 paths = index.reduce_paths(extra)
156 o.fatal('update (-u) requested but no paths given')
157 for (rp,path) in paths:
160 if opt['print'] or opt.status or opt.modified:
161 for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
163 and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
169 elif not ent.is_valid():
170 if ent.sha == index.EMPTY_SHA:
177 line += ent.sha.encode('hex') + ' '
179 line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
180 print line + (name or './')
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))
187 log('WARNING: %d errors encountered.\n' % len(saved_errors))