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 | |
| 16 | import sys |
| 17 | |
| 18 | from command import Command |
| 19 | from git_command import GitCommand |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 20 | |
| 21 | class Rebase(Command): |
| 22 | common = True |
| 23 | helpSummary = "Rebase local branches on upstream branch" |
| 24 | helpUsage = """ |
| 25 | %prog {[<project>...] | -i <project>...} |
| 26 | """ |
| 27 | helpDescription = """ |
| 28 | '%prog' uses git rebase to move local changes in the current topic branch to |
| 29 | the HEAD of the upstream history, useful when you have made commits in a topic |
| 30 | branch but need to incorporate new upstream changes "underneath" them. |
| 31 | """ |
| 32 | |
| 33 | def _Options(self, p): |
| 34 | p.add_option('-i', '--interactive', |
| 35 | dest="interactive", action="store_true", |
| 36 | help="interactive rebase (single project only)") |
| 37 | |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 38 | p.add_option('-f', '--force-rebase', |
| 39 | dest='force_rebase', action='store_true', |
| 40 | help='Pass --force-rebase to git rebase') |
| 41 | p.add_option('--no-ff', |
| 42 | dest='no_ff', action='store_true', |
| 43 | help='Pass --no-ff to git rebase') |
| 44 | p.add_option('-q', '--quiet', |
| 45 | dest='quiet', action='store_true', |
| 46 | help='Pass --quiet to git rebase') |
| 47 | p.add_option('--autosquash', |
| 48 | dest='autosquash', action='store_true', |
| 49 | help='Pass --autosquash to git rebase') |
| 50 | p.add_option('--whitespace', |
| 51 | dest='whitespace', action='store', metavar='WS', |
| 52 | help='Pass --whitespace to git rebase') |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 53 | p.add_option('--auto-stash', |
| 54 | dest='auto_stash', action='store_true', |
| 55 | help='Stash local modifications before starting') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 56 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 57 | def Execute(self, opt, args): |
| 58 | all = self.GetProjects(args) |
| 59 | one_project = len(all) == 1 |
| 60 | |
| 61 | if opt.interactive and not one_project: |
| 62 | print >>sys.stderr, 'error: interactive rebase not supported with multiple projects' |
| 63 | return -1 |
| 64 | |
| 65 | for project in all: |
| 66 | cb = project.CurrentBranch |
| 67 | if not cb: |
| 68 | if one_project: |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 69 | print >>sys.stderr, "error: project %s has a detatched HEAD" % project.relpath |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 70 | return -1 |
| 71 | # ignore branches with detatched HEADs |
| 72 | continue |
| 73 | |
| 74 | upbranch = project.GetBranch(cb) |
| 75 | if not upbranch.LocalMerge: |
| 76 | if one_project: |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 77 | print >>sys.stderr, "error: project %s does not track any remote branches" % project.relpath |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 78 | return -1 |
| 79 | # ignore branches without remotes |
| 80 | continue |
| 81 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 82 | args = ["rebase"] |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 83 | |
| 84 | if opt.whitespace: |
| 85 | args.append('--whitespace=%s' % opt.whitespace) |
| 86 | |
| 87 | if opt.quiet: |
| 88 | args.append('--quiet') |
| 89 | |
| 90 | if opt.force_rebase: |
| 91 | args.append('--force-rebase') |
| 92 | |
| 93 | if opt.no_ff: |
| 94 | args.append('--no-ff') |
| 95 | |
| 96 | if opt.autosquash: |
| 97 | args.append('--autosquash') |
| 98 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 99 | if opt.interactive: |
| 100 | args.append("-i") |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 101 | |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 102 | args.append(upbranch.LocalMerge) |
| 103 | |
| 104 | print >>sys.stderr, '# %s: rebasing %s -> %s' % \ |
| 105 | (project.relpath, cb, upbranch.LocalMerge) |
| 106 | |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 107 | needs_stash = False |
| 108 | if opt.auto_stash: |
| 109 | stash_args = ["update-index", "--refresh", "-q"] |
| 110 | |
| 111 | if GitCommand(project, stash_args).Wait() != 0: |
| 112 | needs_stash = True |
| 113 | # Dirty index, requires stash... |
| 114 | stash_args = ["stash"] |
| 115 | |
| 116 | if GitCommand(project, stash_args).Wait() != 0: |
| 117 | return -1 |
| 118 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 119 | if GitCommand(project, args).Wait() != 0: |
| 120 | return -1 |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame] | 121 | |
| 122 | if needs_stash: |
| 123 | stash_args.append('pop') |
| 124 | stash_args.append('--quiet') |
| 125 | if GitCommand(project, stash_args).Wait() != 0: |
| 126 | return -1 |