2 import sys, re, errno, stat, client
3 import hashsplit, git, options, index
8 bup save [-tc] [-n name] <filenames...>
10 r,remote= remote repository path
11 t,tree output a tree id
12 c,commit output a commit id
13 n,name= name of backup set to update (if any)
14 v,verbose increase log output (can be used more than once)
15 q,quiet don't show progress meter
16 smaller= only back up files smaller than n bytes
18 o = options.Options('bup save', optspec)
19 (opt, flags, extra) = o.parse(sys.argv[1:])
21 git.check_repo_or_die()
22 if not (opt.tree or opt.commit or opt.name):
23 log("bup save: use one or more of -t, -c, -n\n")
26 log("bup save: no filenames given.\n")
29 opt.progress = (istty and not opt.quiet)
31 refname = opt.name and 'refs/heads/%s' % opt.name or None
33 cli = client.Client(opt.remote)
34 oldref = refname and cli.read_ref(refname) or None
35 w = cli.new_packwriter()
38 oldref = refname and git.read_ref(refname) or None
58 assert(len(parts) > 1)
60 shalist = shalists.pop()
61 tree = w.new_tree(shalist)
62 shalists[-1].append(('40000', part, tree))
64 def progress_report(n):
67 pct = count*100.0/total
68 progress('Saving: %.2f%% (%d/%dk, %d/%d files)\r'
69 % (pct, count/1024, total/1024, fcount, ftotal))
72 r = index.Reader(git.repo('bupindex'))
76 for (transname,ent) in r.filter(extra):
77 if not (ftotal % 10024):
78 progress('Reading index: %d\r' % ftotal)
79 exists = (ent.flags & index.IX_EXISTS)
80 hashvalid = (ent.flags & index.IX_HASHVALID) and w.exists(ent.sha)
81 if exists and not hashvalid:
84 progress('Reading index: %d, done.\n' % ftotal)
85 hashsplit.progress_callback = progress_report
88 for (transname,ent) in r.filter(extra):
89 (dir, file) = os.path.split(ent.name)
90 exists = (ent.flags & index.IX_EXISTS)
91 hashvalid = (ent.flags & index.IX_HASHVALID) and w.exists(ent.sha)
96 if ent.sha == index.EMPTY_SHA:
102 if opt.verbose >= 2 or stat.S_ISDIR(ent.mode):
103 log('%s %-70s\n' % (status, ent.name))
112 assert(dir.startswith('/'))
113 dirp = dir.split('/')
117 for part in dirp[len(parts):]:
121 # directory already handled.
122 # FIXME: not using the indexed tree sha1's for anything, which is
123 # a waste. That's a potential optimization...
129 mode = '%o' % ent.mode
131 shalists[-1].append((mode, file, id))
132 elif opt.smaller and ent.size >= opt.smaller:
133 add_error('skipping large file "%s"' % ent.name)
135 if stat.S_ISREG(ent.mode):
143 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f])
145 if stat.S_ISDIR(ent.mode):
146 assert(0) # handled above
147 elif stat.S_ISLNK(ent.mode):
149 rl = os.readlink(ent.name)
155 (mode, id) = ('120000', w.new_blob(rl))
157 add_error(Exception('skipping special file "%s"' % ent.name))
162 shalists[-1].append((mode, file, id))
165 pct = total and count*100.0/total or 100
166 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done.\n'
167 % (pct, count/1024, total/1024, fcount, ftotal))
169 #log('parts out: %r\n' % parts)
170 #log('stk out: %r\n' % shalists)
171 while len(parts) > 1:
173 #log('parts out: %r\n' % parts)
174 #log('stk out: %r\n' % shalists)
175 assert(len(shalists) == 1)
176 tree = w.new_tree(shalists[-1])
178 print tree.encode('hex')
179 if opt.commit or opt.name:
180 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
181 ref = opt.name and ('refs/heads/%s' % opt.name) or None
182 commit = w.new_commit(oldref, tree, msg)
184 print commit.encode('hex')
186 w.close() # must close before we can update the ref
190 cli.update_ref(refname, commit, oldref)
192 git.update_ref(refname, commit, oldref)
198 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))