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