5 from bup import vfs2 as vfs
6 from bup.client import ClientError
7 from bup.git import get_commit_items
8 from bup.helpers import add_error, die_if_errors, log, saved_errors
11 def append_commit(hash, parent, cp, writer):
12 ci = get_commit_items(hash, cp)
13 tree = ci.tree.decode('hex')
14 author = '%s <%s>' % (ci.author_name, ci.author_mail)
15 committer = '%s <%s>' % (ci.committer_name, ci.committer_mail)
16 c = writer.new_commit(tree, parent,
17 author, ci.author_sec, ci.author_offset,
18 committer, ci.committer_sec, ci.committer_offset,
23 def filter_branch(tip_commit_hex, exclude, writer):
24 # May return None if everything is excluded.
25 commits = [x.decode('hex') for x in git.rev_list(tip_commit_hex)]
27 last_c, tree = None, None
28 # Rather than assert that we always find an exclusion here, we'll
29 # just let the StopIteration signal the error.
30 first_exclusion = next(i for i, c in enumerate(commits) if exclude(c))
31 if first_exclusion != 0:
32 last_c = commits[first_exclusion - 1]
33 tree = get_commit_items(last_c.encode('hex'),
34 git.cp()).tree.decode('hex')
35 commits = commits[first_exclusion:]
39 last_c, tree = append_commit(c.encode('hex'), last_c, git.cp(), writer)
43 if isinstance(item, vfs.Commit):
45 assert isinstance(item, vfs.RevList)
48 def rm_saves(saves, writer):
50 first_branch_item = saves[0][1]
51 for save, branch in saves: # Be certain they're all on the same branch
52 assert(branch == first_branch_item)
53 rm_commits = frozenset([commit_oid(save) for save, branch in saves])
54 orig_tip = commit_oid(first_branch_item)
55 new_tip = filter_branch(orig_tip.encode('hex'),
56 lambda x: x in rm_commits,
59 assert(new_tip != orig_tip)
60 return orig_tip, new_tip
63 def dead_items(repo, paths):
64 """Return an optimized set of removals, reporting errors via
65 add_error, and if there are any errors, return None, None."""
68 # Scan for bad requests, and opportunities to optimize
71 resolved = vfs.lresolve(repo, path)
72 except vfs.IOError as e:
76 leaf_name, leaf_item = resolved[-1]
78 add_error('error: cannot access %r in %r'
79 % ('/'.join(name for name, item in resolved),
82 if isinstance(leaf_item, vfs.RevList): # rm /foo
83 branchname = leaf_name
84 dead_branches[branchname] = leaf_item
85 dead_saves.pop(branchname, None) # rm /foo obviates rm /foo/bar
86 elif isinstance(leaf_item, vfs.Commit): # rm /foo/bar
87 if leaf_name == 'latest':
88 add_error("error: cannot delete 'latest' symlink")
90 branchname, branchitem = resolved[-2]
91 if branchname not in dead_branches:
92 dead = leaf_item, branchitem
93 dead_saves.setdefault(branchname, []).append(dead)
95 add_error("don't know how to remove %r yet" % path)
98 return dead_branches, dead_saves
101 def bup_rm(repo, paths, compression=6, verbosity=None):
102 dead_branches, dead_saves = dead_items(repo, paths)
103 die_if_errors('not proceeding with any removals\n')
105 updated_refs = {} # ref_name -> (original_ref, tip_commit(bin))
107 for branchname, branchitem in dead_branches.iteritems():
108 ref = 'refs/heads/' + branchname
109 assert(not ref in updated_refs)
110 updated_refs[ref] = (branchitem.oid, None)
113 writer = git.PackWriter(compression_level=compression)
115 for branch, saves in dead_saves.iteritems():
117 updated_refs['refs/heads/' + branch] = rm_saves(saves, writer)
124 # Must close before we can update the ref(s) below.
127 # Only update the refs here, at the very end, so that if something
128 # goes wrong above, the old refs will be undisturbed. Make an attempt
129 # to update each ref.
130 for ref_name, info in updated_refs.iteritems():
131 orig_ref, new_ref = info
134 git.delete_ref(ref_name, orig_ref.encode('hex'))
136 git.update_ref(ref_name, new_ref, orig_ref)
138 new_hex = new_ref.encode('hex')
140 orig_hex = orig_ref.encode('hex')
141 log('updated %r (%s -> %s)\n'
142 % (ref_name, orig_hex, new_hex))
144 log('updated %r (%s)\n' % (ref_name, new_hex))
145 except (git.GitError, ClientError) as ex:
147 add_error('while trying to update %r (%s -> %s): %s'
148 % (ref_name, orig_ref, new_ref, ex))
150 add_error('while trying to delete %r (%s): %s'
151 % (ref_name, orig_ref, ex))