Add a --rebase option to sync command

Previously repo would abort a sync if there were published changes not
merged upstream. The --rebase option allows the modification of
published commits.

This is a copy of http://go/grev/369694 with the merge conflicts
resolved.

Bug: 40014610
Change-Id: Idac8199400346327b530abea33f1ed794e5bb4c2
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/435838
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
Tested-by: Jeroen Dhollander <jeroendh@google.com>
Commit-Queue: Jeroen Dhollander <jeroendh@google.com>
This commit is contained in:
Jeroen Dhollander 2024-08-20 10:28:41 +02:00 committed by LUCI
parent 4592a63de5
commit c44ad09309
2 changed files with 21 additions and 3 deletions

View File

@ -1535,6 +1535,7 @@ class Project:
syncbuf, syncbuf,
force_sync=False, force_sync=False,
force_checkout=False, force_checkout=False,
force_rebase=False,
submodules=False, submodules=False,
errors=None, errors=None,
verbose=False, verbose=False,
@ -1680,14 +1681,15 @@ class Project:
if pub: if pub:
not_merged = self._revlist(not_rev(revid), pub) not_merged = self._revlist(not_rev(revid), pub)
if not_merged: if not_merged:
if upstream_gain: if upstream_gain and not force_rebase:
# The user has published this branch and some of those # The user has published this branch and some of those
# commits are not yet merged upstream. We do not want # commits are not yet merged upstream. We do not want
# to rewrite the published commits so we punt. # to rewrite the published commits so we punt.
fail( fail(
LocalSyncFail( LocalSyncFail(
"branch %s is published (but not merged) and is " "branch %s is published (but not merged) and is "
"now %d commits behind" "now %d commits behind. Fix this manually or rerun "
"with the --rebase option to force a rebase."
% (branch.name, len(upstream_gain)), % (branch.name, len(upstream_gain)),
project=self.name, project=self.name,
) )

View File

@ -400,6 +400,13 @@ later is required to fix a server side protocol bug.
"projects no longer exist in the manifest. " "projects no longer exist in the manifest. "
"WARNING: this may cause loss of data", "WARNING: this may cause loss of data",
) )
p.add_option(
"--rebase",
dest="rebase",
action="store_true",
help="rebase local commits regardless of whether they are "
"published",
)
p.add_option( p.add_option(
"-l", "-l",
"--local-only", "--local-only",
@ -1009,7 +1016,13 @@ later is required to fix a server side protocol bug.
return _FetchMainResult(all_projects) return _FetchMainResult(all_projects)
def _CheckoutOne( def _CheckoutOne(
self, detach_head, force_sync, force_checkout, verbose, project self,
detach_head,
force_sync,
force_checkout,
force_rebase,
verbose,
project,
): ):
"""Checkout work tree for one project """Checkout work tree for one project
@ -1019,6 +1032,7 @@ later is required to fix a server side protocol bug.
existing git directory that was previously linked to a different existing git directory that was previously linked to a different
object directory). object directory).
force_checkout: Force checking out of the repo content. force_checkout: Force checking out of the repo content.
force_rebase: Force rebase.
verbose: Whether to show verbose messages. verbose: Whether to show verbose messages.
project: Project object for the project to checkout. project: Project object for the project to checkout.
@ -1036,6 +1050,7 @@ later is required to fix a server side protocol bug.
syncbuf, syncbuf,
force_sync=force_sync, force_sync=force_sync,
force_checkout=force_checkout, force_checkout=force_checkout,
force_rebase=force_rebase,
errors=errors, errors=errors,
verbose=verbose, verbose=verbose,
) )
@ -1109,6 +1124,7 @@ later is required to fix a server side protocol bug.
opt.detach_head, opt.detach_head,
opt.force_sync, opt.force_sync,
opt.force_checkout, opt.force_checkout,
opt.rebase,
opt.verbose, opt.verbose,
), ),
projects, projects,