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