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