Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: git_rebase_update.py

Issue 184253003: Add git-reup and friends (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@freeze_thaw
Patch Set: fix pylint Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « git_new_branch.py ('k') | git_rename_branch.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """
7 Tool to update all branches to have the latest changes from their upstreams.
8 """
9
10 import argparse
11 import collections
12 import logging
13 import sys
14 import textwrap
15
16 from pprint import pformat
17
18 import git_common as git
19
20
21 STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch'
22
23
24 def find_return_branch():
25 """Finds the branch which we should return to after rebase-update completes.
26
27 This value may persist across multiple invocations of rebase-update, if
28 rebase-update runs into a conflict mid-way.
29 """
30 return_branch = git.config(STARTING_BRANCH_KEY)
31 if return_branch is None:
32 return_branch = git.current_branch()
33 if return_branch != 'HEAD':
34 git.set_config(STARTING_BRANCH_KEY, return_branch)
35
36 return return_branch
37
38
39 def fetch_remotes(branch_tree):
40 """Fetches all remotes which are needed to update |branch_tree|."""
41 fetch_tags = False
42 remotes = set()
43 tag_set = git.tags()
44 for parent in branch_tree.itervalues():
45 if parent in tag_set:
46 fetch_tags = True
47 else:
48 full_ref = git.run('rev-parse', '--symbolic-full-name', parent)
49 if full_ref.startswith('refs/remotes'):
50 parts = full_ref.split('/')
51 remote_name = parts[2]
52 remotes.add(remote_name)
53
54 fetch_args = []
55 if fetch_tags:
56 # Need to fetch all because we don't know what remote the tag comes from :(
57 # TODO(iannucci): assert that the tags are in the remote fetch refspec
58 fetch_args = ['--all']
59 else:
60 fetch_args.append('--multiple')
61 fetch_args.extend(remotes)
62 # TODO(iannucci): Should we fetch git-svn?
63
64 if not fetch_args: # pragma: no cover
65 print 'Nothing to fetch.'
66 else:
67 out, err = git.run_with_stderr('fetch', *fetch_args)
68 for data, stream in zip((out, err), (sys.stdout, sys.stderr)):
69 if data:
70 print >> stream, data
71
72
73 def remove_empty_branches(branch_tree):
74 tag_set = git.tags()
75 ensure_root_checkout = git.once(lambda: git.run('checkout', git.root()))
76
77 downstreams = collections.defaultdict(list)
78 for branch, parent in git.topo_iter(branch_tree, top_down=False):
79 downstreams[parent].append(branch)
80
81 if git.hash_one(branch) == git.hash_one(parent):
82 ensure_root_checkout()
83
84 logging.debug('branch %s merged to %s', branch, parent)
85
86 for down in downstreams[branch]:
87 if parent in tag_set:
88 git.set_branch_config(down, 'remote', '.')
89 git.set_branch_config(down, 'merge', 'refs/tags/%s' % parent)
90 print ('Reparented %s to track %s [tag] (was tracking %s)'
91 % (down, parent, branch))
92 else:
93 git.run('branch', '--set-upstream-to', parent, down)
94 print ('Reparented %s to track %s (was tracking %s)'
95 % (down, parent, branch))
96
97 print git.run('branch', '-d', branch)
98
99
100 def rebase_branch(branch, parent, start_hash):
101 logging.debug('considering %s(%s) -> %s(%s) : %s',
102 branch, git.hash_one(branch), parent, git.hash_one(parent),
103 start_hash)
104
105 # If parent has FROZEN commits, don't base branch on top of them. Instead,
106 # base branch on top of whatever commit is before them.
107 back_ups = 0
108 orig_parent = parent
109 while git.run('log', '-n1', '--format=%s',
110 parent, '--').startswith(git.FREEZE):
111 back_ups += 1
112 parent = git.run('rev-parse', parent+'~')
113
114 if back_ups:
115 logging.debug('Backed parent up by %d from %s to %s',
116 back_ups, orig_parent, parent)
117
118 if git.hash_one(parent) != start_hash:
119 # Try a plain rebase first
120 print 'Rebasing:', branch
121 if not git.rebase(parent, start_hash, branch, abort=True).success:
122 # TODO(iannucci): Find collapsible branches in a smarter way?
123 print "Failed! Attempting to squash", branch, "...",
124 squash_branch = branch+"_squash_attempt"
125 git.run('checkout', '-b', squash_branch)
126 git.squash_current_branch(merge_base=start_hash)
127
128 # Try to rebase the branch_squash_attempt branch to see if it's empty.
129 squash_ret = git.rebase(parent, start_hash, squash_branch, abort=True)
130 empty_rebase = git.hash_one(squash_branch) == git.hash_one(parent)
131 git.run('checkout', branch)
132 git.run('branch', '-D', squash_branch)
133 if squash_ret.success and empty_rebase:
134 print 'Success!'
135 git.squash_current_branch(merge_base=start_hash)
136 git.rebase(parent, start_hash, branch)
137 else:
138 # rebase and leave in mid-rebase state.
139 git.rebase(parent, start_hash, branch)
140 print squash_ret.message
141 print
142 print textwrap.dedent(
143 """
144 Squashing failed. You probably have a real merge conflict.
145
146 Your working copy is in mid-rebase. Either:
147 * completely resolve like a normal git-rebase; OR
148 * abort the rebase and mark this branch as dormant:
149 git config branch.%s.dormant true
150
151 And then run `git rebase-update` again to resume.
152 """ % branch)
153 return False
154 else:
155 print '%s up-to-date' % branch
156
157 git.remove_merge_base(branch)
158 git.get_or_create_merge_base(branch)
159
160 return True
161
162
163 def main(args=()):
164 parser = argparse.ArgumentParser()
165 parser.add_argument('--verbose', '-v', action='store_true')
166 parser.add_argument('--no_fetch', '-n', action='store_true',
167 help='Skip fetching remotes.')
168 opts = parser.parse_args(args)
169
170 if opts.verbose: # pragma: no cover
171 logging.getLogger().setLevel(logging.DEBUG)
172
173 # TODO(iannucci): snapshot all branches somehow, so we can implement
174 # `git rebase-update --undo`.
175 # * Perhaps just copy packed-refs + refs/ + logs/ to the side?
176 # * commit them to a secret ref?
177 # * Then we could view a summary of each run as a
178 # `diff --stat` on that secret ref.
179
180 if git.in_rebase():
181 # TODO(iannucci): Be able to resume rebase with flags like --continue,
182 # etc.
183 print (
184 'Rebase in progress. Please complete the rebase before running '
185 '`git rebase-update`.'
186 )
187 return 1
188
189 return_branch = find_return_branch()
190
191 if git.current_branch() == 'HEAD':
192 if git.run('status', '--porcelain'):
193 print 'Cannot rebase-update with detached head + uncommitted changes.'
194 return 1
195 else:
196 git.freeze() # just in case there are any local changes.
197
198 skipped, branch_tree = git.get_branch_tree()
199 for branch in skipped:
200 print 'Skipping %s: No upstream specified' % branch
201
202 if not opts.no_fetch:
203 fetch_remotes(branch_tree)
204
205 merge_base = {}
206 for branch, parent in branch_tree.iteritems():
207 merge_base[branch] = git.get_or_create_merge_base(branch, parent)
208
209 logging.debug('branch_tree: %s' % pformat(branch_tree))
210 logging.debug('merge_base: %s' % pformat(merge_base))
211
212 retcode = 0
213 # Rebase each branch starting with the root-most branches and working
214 # towards the leaves.
215 for branch, parent in git.topo_iter(branch_tree):
216 if git.is_dormant(branch):
217 print 'Skipping dormant branch', branch
218 else:
219 ret = rebase_branch(branch, parent, merge_base[branch])
220 if not ret:
221 retcode = 1
222 break
223
224 if not retcode:
225 remove_empty_branches(branch_tree)
226
227 # return_branch may not be there any more.
228 if return_branch in git.branches():
229 git.run('checkout', return_branch)
230 git.thaw()
231 else:
232 root_branch = git.root()
233 if return_branch != 'HEAD':
234 print (
235 "%r was merged with its parent, checking out %r instead."
236 % (return_branch, root_branch)
237 )
238 git.run('checkout', root_branch)
239 git.del_config(STARTING_BRANCH_KEY)
240
241 return retcode
242
243
244 if __name__ == '__main__': # pragma: no cover
245 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « git_new_branch.py ('k') | git_rename_branch.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698