1 #!/usr/bin/env python2.5
3 import options, git, index
7 def __init__(self, path):
9 self.fd = os.open(path, os.O_RDONLY|os.O_LARGEFILE|os.O_NOFOLLOW)
10 #self.st = os.fstat(self.fd)
24 saved_errors.append(e)
28 # the use of fchdir() and lstat() are for two reasons:
29 # - help out the kernel by not making it repeatedly look up the absolute path
30 # - avoid race conditions caused by doing listdir() on a changing symlink
31 def handle_path(ri, wi, dir, name, pst, xdev, can_delete_siblings):
35 return (0, index.FAKE_SHA)
39 #log('handle_path(%r,%r)\n' % (dir, name))
40 if stat.S_ISDIR(pst.st_mode):
41 if opt.verbose == 1: # log dirs only
42 sys.stdout.write('%s\n' % path)
47 add_error(Exception('in %s: %s' % (dir, str(e))))
52 #log('* %r: %r\n' % (name, ld))
54 add_error(Exception('in %s: %s' % (path, str(e))))
61 add_error(Exception('in %s: %s' % (path, str(e))))
63 if xdev != None and st.st_dev != xdev:
64 log('Skipping %r: different filesystem.\n'
67 if stat.S_ISDIR(st.st_mode):
70 for p,st in reversed(sorted(lds)):
71 dirty += handle_path(ri, wi, path, p, st, xdev,
72 can_delete_siblings = True)
75 #log('endloop: ri.cur:%r path:%r\n' % (ri.cur.name, path))
76 while ri.cur and ri.cur.name > path:
77 #log('ricur:%r path:%r\n' % (ri.cur, path))
78 if can_delete_siblings and dir and ri.cur.name.startswith(dir):
79 #log(' --- deleting\n')
80 ri.cur.flags &= ~(index.IX_EXISTS | index.IX_HASHVALID)
84 if ri.cur and ri.cur.name == path:
85 dirty += ri.cur.from_stat(pst)
86 if dirty or not (ri.cur.flags & index.IX_HASHVALID):
87 #log(' --- updating %r\n' % path)
89 (ri.cur.gitmode, ri.cur.sha) = hashgen(name)
90 ri.cur.flags |= index.IX_HASHVALID
94 wi.add(path, pst, hashgen = hashgen)
96 if opt.verbose > 1: # all files, not just dirs
97 sys.stdout.write('%s\n' % path)
102 def merge_indexes(out, r1, r2):
103 log('bup: merging indexes.\n')
104 for e in index._last_writer_wins_iter([r1, r2]):
105 #if e.flags & index.IX_EXISTS:
110 def __init__(self, l):
117 self.cur = self.i.next()
118 except StopIteration:
123 def update_index(path):
124 ri = index.Reader(indexfile)
125 wi = index.Writer(indexfile)
126 rig = MergeGetter(ri)
128 rpath = index.realpath(path)
137 (dir, name) = os.path.split(rpath)
138 dir = slashappend(dir)
139 if stat.S_ISDIR(st.st_mode) and (not rpath or rpath[-1] != '/'):
141 can_delete_siblings = True
143 can_delete_siblings = False
144 OsFile(dir or '/').fchdir()
145 dirty = handle_path(rig, wi, dir, name, st, xdev, can_delete_siblings)
147 # make sure all the parents of the updated path exist and are invalidated
150 (rpath, junk) = os.path.split(rpath)
157 while rig.cur and rig.cur.name > p:
158 #log('FINISHING: %r path=%r d=%r\n' % (rig.cur.name, p, dirty))
160 if rig.cur and rig.cur.name == p:
162 rig.cur.flags &= ~index.IX_HASHVALID
165 wi.add(p, os.lstat(p))
172 mi = index.Writer(indexfile)
173 merge_indexes(mi, ri, wi.new_reader())
179 bup index <-p|s|m|u> [options...] <filenames...>
181 p,print print the index entries for the given names (also works with -u)
182 m,modified print only added/deleted/modified files (implies -p)
183 s,status print each filename with a status char (A/M/D) (implies -p)
184 H,hash print the hash for each object next to its name (implies -p)
185 u,update (recursively) update the index entries for the given filenames
186 x,xdev,one-file-system don't cross filesystem boundaries
187 fake-valid mark all index entries as up-to-date even if they aren't
188 f,indexfile= the name of the index file (default 'index')
189 v,verbose increase log output (can be used more than once)
191 o = options.Options('bup index', optspec)
192 (opt, flags, extra) = o.parse(sys.argv[1:])
194 if not (opt.modified or opt['print'] or opt.status or opt.update):
195 log('bup index: you must supply one or more of -p, -s, -m, or -u\n')
197 if opt.fake_valid and not opt.update:
198 log('bup index: --fake-valid is meaningless without -u\n')
201 git.check_repo_or_die()
202 indexfile = opt.indexfile or git.repo('bupindex')
204 paths = index.reduce_paths(extra)
208 log('bup index: update (-u) requested but no paths given\n')
210 for (rp, path) in paths:
213 if opt['print'] or opt.status or opt.modified:
214 for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
215 if opt.modified and ent.flags & index.IX_HASHVALID:
219 if not ent.flags & index.IX_EXISTS:
221 elif not ent.flags & index.IX_HASHVALID:
222 if ent.sha == index.EMPTY_SHA:
229 line += ent.sha.encode('hex') + ' '
230 print line + (name or './')
234 log('WARNING: %d errors encountered.\n' % len(saved_errors))