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): |
| 30 | common = True |
| 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): |
| 42 | p.add_option('-i', '--interactive', |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 43 | dest="interactive", action="store_true", |
| 44 | help="interactive rebase (single project only)") |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 45 | |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 46 | p.add_option('--fail-fast', |
| 47 | dest='fail_fast', action='store_true', |
| 48 | help='Stop rebasing after first error is hit') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 49 | p.add_option('-f', '--force-rebase', |
| 50 | dest='force_rebase', action='store_true', |
| 51 | help='Pass --force-rebase to git rebase') |
| 52 | p.add_option('--no-ff', |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 53 | dest='ff', default=True, action='store_false', |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 54 | help='Pass --no-ff to git rebase') |
| 55 | p.add_option('-q', '--quiet', |
| 56 | dest='quiet', action='store_true', |
| 57 | help='Pass --quiet to git rebase') |
| 58 | p.add_option('--autosquash', |
| 59 | dest='autosquash', action='store_true', |
| 60 | help='Pass --autosquash to git rebase') |
| 61 | p.add_option('--whitespace', |
| 62 | dest='whitespace', action='store', metavar='WS', |
| 63 | help='Pass --whitespace to git rebase') |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 64 | p.add_option('--auto-stash', |
| 65 | dest='auto_stash', action='store_true', |
| 66 | help='Stash local modifications before starting') |
Xiaohui Chen | 0b4cb32 | 2016-01-27 14:33:51 -0800 | [diff] [blame] | 67 | p.add_option('-m', '--onto-manifest', |
| 68 | dest='onto_manifest', action='store_true', |
| 69 | help='Rebase onto the manifest version instead of upstream ' |
| 70 | 'HEAD. This helps to make sure the local tree stays ' |
| 71 | 'consistent if you previously synced to a manifest.') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 72 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 73 | def Execute(self, opt, args): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 74 | all_projects = self.GetProjects(args) |
| 75 | one_project = len(all_projects) == 1 |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 76 | |
| 77 | if opt.interactive and not one_project: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 78 | print('error: interactive rebase not supported with multiple projects', |
| 79 | file=sys.stderr) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 80 | if len(args) == 1: |
| 81 | 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] | 82 | file=sys.stderr) |
Mike Frysinger | a850ca2 | 2019-08-07 17:19:24 -0400 | [diff] [blame] | 83 | return 1 |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 84 | |
Mike Frysinger | d5c306b | 2019-08-07 17:23:23 -0400 | [diff] [blame] | 85 | # Setup the common git rebase args that we use for all projects. |
| 86 | common_args = ['rebase'] |
| 87 | if opt.whitespace: |
| 88 | common_args.append('--whitespace=%s' % opt.whitespace) |
| 89 | if opt.quiet: |
| 90 | common_args.append('--quiet') |
| 91 | if opt.force_rebase: |
| 92 | common_args.append('--force-rebase') |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 93 | if not opt.ff: |
Mike Frysinger | d5c306b | 2019-08-07 17:23:23 -0400 | [diff] [blame] | 94 | common_args.append('--no-ff') |
| 95 | if opt.autosquash: |
| 96 | common_args.append('--autosquash') |
| 97 | if opt.interactive: |
| 98 | common_args.append('-i') |
| 99 | |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 100 | config = self.manifest.manifestProject.config |
| 101 | out = RebaseColoring(config) |
| 102 | out.redirect(sys.stdout) |
| 103 | |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 104 | ret = 0 |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 105 | for project in all_projects: |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 106 | if ret and opt.fail_fast: |
| 107 | break |
| 108 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 109 | cb = project.CurrentBranch |
| 110 | if not cb: |
| 111 | if one_project: |
David Pursehouse | 2f9e7e4 | 2013-03-05 17:26:46 +0900 | [diff] [blame] | 112 | print("error: project %s has a detached HEAD" % project.relpath, |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 113 | file=sys.stderr) |
Mike Frysinger | a850ca2 | 2019-08-07 17:19:24 -0400 | [diff] [blame] | 114 | return 1 |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 115 | # ignore branches with detatched HEADs |
| 116 | continue |
| 117 | |
| 118 | upbranch = project.GetBranch(cb) |
| 119 | if not upbranch.LocalMerge: |
| 120 | if one_project: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 121 | print("error: project %s does not track any remote branches" |
| 122 | % project.relpath, file=sys.stderr) |
Mike Frysinger | a850ca2 | 2019-08-07 17:19:24 -0400 | [diff] [blame] | 123 | return 1 |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 124 | # ignore branches without remotes |
| 125 | continue |
| 126 | |
Mike Frysinger | d5c306b | 2019-08-07 17:23:23 -0400 | [diff] [blame] | 127 | args = common_args[:] |
Xiaohui Chen | 0b4cb32 | 2016-01-27 14:33:51 -0800 | [diff] [blame] | 128 | if opt.onto_manifest: |
| 129 | args.append('--onto') |
| 130 | args.append(project.revisionExpr) |
| 131 | |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 132 | args.append(upbranch.LocalMerge) |
| 133 | |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 134 | out.project('project %s: rebasing %s -> %s', |
| 135 | project.relpath, cb, upbranch.LocalMerge) |
| 136 | out.nl() |
| 137 | out.flush() |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 138 | |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 139 | needs_stash = False |
| 140 | if opt.auto_stash: |
| 141 | stash_args = ["update-index", "--refresh", "-q"] |
| 142 | |
| 143 | if GitCommand(project, stash_args).Wait() != 0: |
| 144 | needs_stash = True |
| 145 | # Dirty index, requires stash... |
| 146 | stash_args = ["stash"] |
| 147 | |
| 148 | if GitCommand(project, stash_args).Wait() != 0: |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 149 | ret += 1 |
| 150 | continue |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 151 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 152 | if GitCommand(project, args).Wait() != 0: |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 153 | ret += 1 |
| 154 | continue |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 155 | |
| 156 | if needs_stash: |
| 157 | stash_args.append('pop') |
| 158 | stash_args.append('--quiet') |
| 159 | if GitCommand(project, stash_args).Wait() != 0: |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 160 | ret += 1 |
| 161 | |
| 162 | if ret: |
Mike Frysinger | e37aa5f | 2019-09-23 19:14:13 -0400 | [diff] [blame] | 163 | out.fail('%i projects had errors', ret) |
| 164 | out.nl() |
| 165 | |
Mike Frysinger | 4a07798 | 2019-09-23 18:54:30 -0400 | [diff] [blame] | 166 | return ret |