Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 1 | # Copyright (C) 2010 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import sys |
| 16 | |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 17 | from color import Coloring |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 18 | from command import Command |
| 19 | from git_command import GitCommand |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 20 | |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 21 | |
| 22 | class RebaseColoring(Coloring): |
| 23 | def __init__(self, config): |
| 24 | Coloring.__init__(self, config, 'rebase') |
| 25 | self.project = self.printer('project', attr='bold') |
| 26 | self.fail = self.printer('fail', fg='red') |
| 27 | |
| 28 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 29 | class Rebase(Command): |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame] | 30 | COMMON = True |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 31 | helpSummary = "Rebase local branches on upstream branch" |
| 32 | helpUsage = """ |
| 33 | %prog {[<project>...] | -i <project>...} |
| 34 | """ |
| 35 | helpDescription = """ |
| 36 | '%prog' uses git rebase to move local changes in the current topic branch to |
| 37 | the HEAD of the upstream history, useful when you have made commits in a topic |
| 38 | branch but need to incorporate new upstream changes "underneath" them. |
| 39 | """ |
| 40 | |
| 41 | def _Options(self, p): |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame] | 42 | g = p.get_option_group('--quiet') |
| 43 | g.add_option('-i', '--interactive', |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 44 | dest="interactive", action="store_true", |
| 45 | help="interactive rebase (single project only)") |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 46 | |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 47 | p.add_option('--fail-fast', |
| 48 | dest='fail_fast', action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 49 | help='stop rebasing after first error is hit') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 50 | p.add_option('-f', '--force-rebase', |
| 51 | dest='force_rebase', action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 52 | help='pass --force-rebase to git rebase') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 53 | p.add_option('--no-ff', |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 54 | dest='ff', default=True, action='store_false', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 55 | help='pass --no-ff to git rebase') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 56 | p.add_option('--autosquash', |
| 57 | dest='autosquash', action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 58 | help='pass --autosquash to git rebase') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 59 | p.add_option('--whitespace', |
| 60 | dest='whitespace', action='store', metavar='WS', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 61 | help='pass --whitespace to git rebase') |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 62 | p.add_option('--auto-stash', |
| 63 | dest='auto_stash', action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 64 | help='stash local modifications before starting') |
Xiaohui Chen | 0b4cb32 | 2016-01-27 14:33:51 -0800 | [diff] [blame] | 65 | p.add_option('-m', '--onto-manifest', |
| 66 | dest='onto_manifest', action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 67 | help='rebase onto the manifest version instead of upstream ' |
| 68 | 'HEAD (this helps to make sure the local tree stays ' |
| 69 | 'consistent if you previously synced to a manifest)') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 70 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 71 | def Execute(self, opt, args): |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 72 | all_projects = self.GetProjects(args, all_manifests=not opt.this_manifest_only) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 73 | one_project = len(all_projects) == 1 |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 74 | |
| 75 | if opt.interactive and not one_project: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 76 | print('error: interactive rebase not supported with multiple projects', |
| 77 | file=sys.stderr) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 78 | if len(args) == 1: |
| 79 | print('note: project %s is mapped to more than one path' % (args[0],), |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 80 | file=sys.stderr) |
Mike Frysinger | a850ca2 | 2019-08-07 17:19:24 -0400 | [diff] [blame] | 81 | return 1 |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 82 | |
Mike Frysinger | d5c306b | 2019-08-07 17:23:23 -0400 | [diff] [blame] | 83 | # Setup the common git rebase args that we use for all projects. |
| 84 | common_args = ['rebase'] |
| 85 | if opt.whitespace: |
| 86 | common_args.append('--whitespace=%s' % opt.whitespace) |
| 87 | if opt.quiet: |
| 88 | common_args.append('--quiet') |
| 89 | if opt.force_rebase: |
| 90 | common_args.append('--force-rebase') |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 91 | if not opt.ff: |
Mike Frysinger | d5c306b | 2019-08-07 17:23:23 -0400 | [diff] [blame] | 92 | common_args.append('--no-ff') |
| 93 | if opt.autosquash: |
| 94 | common_args.append('--autosquash') |
| 95 | if opt.interactive: |
| 96 | common_args.append('-i') |
| 97 | |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 98 | config = self.manifest.manifestProject.config |
| 99 | out = RebaseColoring(config) |
| 100 | out.redirect(sys.stdout) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 101 | _RelPath = lambda p: p.RelPath(local=opt.this_manifest_only) |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 102 | |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 103 | ret = 0 |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 104 | for project in all_projects: |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 105 | if ret and opt.fail_fast: |
| 106 | break |
| 107 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 108 | cb = project.CurrentBranch |
| 109 | if not cb: |
| 110 | if one_project: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 111 | print("error: project %s has a detached HEAD" % _RelPath(project), |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 112 | file=sys.stderr) |
Mike Frysinger | a850ca2 | 2019-08-07 17:19:24 -0400 | [diff] [blame] | 113 | return 1 |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 114 | # ignore branches with detatched HEADs |
| 115 | continue |
| 116 | |
| 117 | upbranch = project.GetBranch(cb) |
| 118 | if not upbranch.LocalMerge: |
| 119 | if one_project: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 120 | print("error: project %s does not track any remote branches" |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 121 | % _RelPath(project), file=sys.stderr) |
Mike Frysinger | a850ca2 | 2019-08-07 17:19:24 -0400 | [diff] [blame] | 122 | return 1 |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 123 | # ignore branches without remotes |
| 124 | continue |
| 125 | |
Mike Frysinger | d5c306b | 2019-08-07 17:23:23 -0400 | [diff] [blame] | 126 | args = common_args[:] |
Xiaohui Chen | 0b4cb32 | 2016-01-27 14:33:51 -0800 | [diff] [blame] | 127 | if opt.onto_manifest: |
| 128 | args.append('--onto') |
| 129 | args.append(project.revisionExpr) |
| 130 | |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 131 | args.append(upbranch.LocalMerge) |
| 132 | |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 133 | out.project('project %s: rebasing %s -> %s', |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 134 | _RelPath(project), cb, upbranch.LocalMerge) |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 135 | out.nl() |
| 136 | out.flush() |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 137 | |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 138 | needs_stash = False |
| 139 | if opt.auto_stash: |
| 140 | stash_args = ["update-index", "--refresh", "-q"] |
| 141 | |
| 142 | if GitCommand(project, stash_args).Wait() != 0: |
| 143 | needs_stash = True |
| 144 | # Dirty index, requires stash... |
| 145 | stash_args = ["stash"] |
| 146 | |
| 147 | if GitCommand(project, stash_args).Wait() != 0: |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 148 | ret += 1 |
| 149 | continue |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 150 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 151 | if GitCommand(project, args).Wait() != 0: |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 152 | ret += 1 |
| 153 | continue |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 154 | |
| 155 | if needs_stash: |
| 156 | stash_args.append('pop') |
| 157 | stash_args.append('--quiet') |
| 158 | if GitCommand(project, stash_args).Wait() != 0: |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 159 | ret += 1 |
| 160 | |
| 161 | if ret: |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 162 | out.fail('%i projects had errors', ret) |
| 163 | out.nl() |
| 164 | |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 165 | return ret |