1 #!/usr/bin/env python2.5
3 import options, git, index
8 O_LARGEFILE = os.O_LARGEFILE
14 def __init__(self, path):
16 self.fd = os.open(path, os.O_RDONLY|O_LARGEFILE|os.O_NOFOLLOW)
30 saved_errors.append(e)
34 # the use of fchdir() and lstat() are for two reasons:
35 # - help out the kernel by not making it repeatedly look up the absolute path
36 # - avoid race conditions caused by doing listdir() on a changing symlink
37 def handle_path(ri, wi, dir, name, pst, xdev, can_delete_siblings):
41 return (0, index.FAKE_SHA)
45 #log('handle_path(%r,%r)\n' % (dir, name))
46 if stat.S_ISDIR(pst.st_mode):
47 if opt.verbose == 1: # log dirs only
48 sys.stdout.write('%s\n' % path)
53 add_error(Exception('in %s: %s' % (dir, str(e))))
58 #log('* %r: %r\n' % (name, ld))
60 add_error(Exception('in %s: %s' % (path, str(e))))
67 add_error(Exception('in %s: %s' % (path, str(e))))
69 if xdev != None and st.st_dev != xdev:
70 log('Skipping %r: different filesystem.\n'
73 if stat.S_ISDIR(st.st_mode):
76 for p,st in reversed(sorted(lds)):
77 dirty += handle_path(ri, wi, path, p, st, xdev,
78 can_delete_siblings = True)
81 #log('endloop: ri.cur:%r path:%r\n' % (ri.cur.name, path))
82 while ri.cur and ri.cur.name > path:
83 #log('ricur:%r path:%r\n' % (ri.cur, path))
84 if can_delete_siblings and dir and ri.cur.name.startswith(dir):
85 #log(' --- deleting\n')
86 ri.cur.flags &= ~(index.IX_EXISTS | index.IX_HASHVALID)
90 if ri.cur and ri.cur.name == path:
91 dirty += ri.cur.from_stat(pst)
92 if dirty or not (ri.cur.flags & index.IX_HASHVALID):
93 #log(' --- updating %r\n' % path)
95 (ri.cur.gitmode, ri.cur.sha) = hashgen(name)
96 ri.cur.flags |= index.IX_HASHVALID
100 wi.add(path, pst, hashgen = hashgen)
102 if opt.verbose > 1: # all files, not just dirs
103 sys.stdout.write('%s\n' % path)
108 def merge_indexes(out, r1, r2):
109 log('bup: merging indexes.\n')
110 for e in index._last_writer_wins_iter([r1, r2]):
111 #if e.flags & index.IX_EXISTS:
116 def __init__(self, l):
123 self.cur = self.i.next()
124 except StopIteration:
129 def update_index(path):
130 ri = index.Reader(indexfile)
131 wi = index.Writer(indexfile)
132 rig = MergeGetter(ri)
134 rpath = index.realpath(path)
143 (dir, name) = os.path.split(rpath)
144 dir = slashappend(dir)
145 if stat.S_ISDIR(st.st_mode) and (not rpath or rpath[-1] != '/'):
147 can_delete_siblings = True
149 can_delete_siblings = False
150 OsFile(dir or '/').fchdir()
151 dirty = handle_path(rig, wi, dir, name, st, xdev, can_delete_siblings)
153 # make sure all the parents of the updated path exist and are invalidated
156 (rpath, junk) = os.path.split(rpath)
163 while rig.cur and rig.cur.name > p:
164 #log('FINISHING: %r path=%r d=%r\n' % (rig.cur.name, p, dirty))
166 if rig.cur and rig.cur.name == p:
168 rig.cur.flags &= ~index.IX_HASHVALID
171 wi.add(p, os.lstat(p))
178 mi = index.Writer(indexfile)
179 merge_indexes(mi, ri, wi.new_reader())
186 bup index <-p|s|m|u> [options...] <filenames...>
188 p,print print the index entries for the given names (also works with -u)
189 m,modified print only added/deleted/modified files (implies -p)
190 s,status print each filename with a status char (A/M/D) (implies -p)
191 H,hash print the hash for each object next to its name (implies -p)
192 u,update (recursively) update the index entries for the given filenames
193 x,xdev,one-file-system don't cross filesystem boundaries
194 fake-valid mark all index entries as up-to-date even if they aren't
195 f,indexfile= the name of the index file (default 'index')
196 v,verbose increase log output (can be used more than once)
198 o = options.Options('bup index', optspec)
199 (opt, flags, extra) = o.parse(sys.argv[1:])
201 if not (opt.modified or opt['print'] or opt.status or opt.update):
202 log('bup index: you must supply one or more of -p, -s, -m, or -u\n')
204 if opt.fake_valid and not opt.update:
205 log('bup index: --fake-valid is meaningless without -u\n')
208 git.check_repo_or_die()
209 indexfile = opt.indexfile or git.repo('bupindex')
211 paths = index.reduce_paths(extra)
215 log('bup index: update (-u) requested but no paths given\n')
217 for (rp, path) in paths:
220 if opt['print'] or opt.status or opt.modified:
221 for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
222 if opt.modified and (ent.flags & index.IX_HASHVALID
223 or stat.S_ISDIR(ent.mode)):
227 if not ent.flags & index.IX_EXISTS:
229 elif not ent.flags & index.IX_HASHVALID:
230 if ent.sha == index.EMPTY_SHA:
237 line += ent.sha.encode('hex') + ' '
238 print line + (name or './')
242 log('WARNING: %d errors encountered.\n' % len(saved_errors))