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 |
| 20 | from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M |
| 21 | from error import GitError |
| 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') |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 58 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 59 | def Execute(self, opt, args): |
| 60 | all = self.GetProjects(args) |
| 61 | one_project = len(all) == 1 |
| 62 | |
| 63 | if opt.interactive and not one_project: |
| 64 | print >>sys.stderr, 'error: interactive rebase not supported with multiple projects' |
| 65 | return -1 |
| 66 | |
| 67 | for project in all: |
| 68 | cb = project.CurrentBranch |
| 69 | if not cb: |
| 70 | if one_project: |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 71 | print >>sys.stderr, "error: project %s has a detatched HEAD" % project.relpath |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 72 | return -1 |
| 73 | # ignore branches with detatched HEADs |
| 74 | continue |
| 75 | |
| 76 | upbranch = project.GetBranch(cb) |
| 77 | if not upbranch.LocalMerge: |
| 78 | if one_project: |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 79 | 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] | 80 | return -1 |
| 81 | # ignore branches without remotes |
| 82 | continue |
| 83 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 84 | args = ["rebase"] |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 85 | |
| 86 | if opt.whitespace: |
| 87 | args.append('--whitespace=%s' % opt.whitespace) |
| 88 | |
| 89 | if opt.quiet: |
| 90 | args.append('--quiet') |
| 91 | |
| 92 | if opt.force_rebase: |
| 93 | args.append('--force-rebase') |
| 94 | |
| 95 | if opt.no_ff: |
| 96 | args.append('--no-ff') |
| 97 | |
| 98 | if opt.autosquash: |
| 99 | args.append('--autosquash') |
| 100 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 101 | if opt.interactive: |
| 102 | args.append("-i") |
Shawn O. Pearce | a22f99a | 2010-07-15 17:40:41 -0700 | [diff] [blame] | 103 | |
| 104 | args.append(upbranch.LocalMerge) |
| 105 | |
| 106 | print >>sys.stderr, '# %s: rebasing %s -> %s' % \ |
| 107 | (project.relpath, cb, upbranch.LocalMerge) |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 108 | |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame^] | 109 | needs_stash = False |
| 110 | if opt.auto_stash: |
| 111 | stash_args = ["update-index", "--refresh", "-q"] |
| 112 | |
| 113 | if GitCommand(project, stash_args).Wait() != 0: |
| 114 | needs_stash = True |
| 115 | # Dirty index, requires stash... |
| 116 | stash_args = ["stash"] |
| 117 | |
| 118 | if GitCommand(project, stash_args).Wait() != 0: |
| 119 | return -1 |
| 120 | |
Daniel Sandler | 9e426aa | 2010-04-01 10:42:33 -0400 | [diff] [blame] | 121 | if GitCommand(project, args).Wait() != 0: |
| 122 | return -1 |
Joe Hansche | 5e57234 | 2012-03-05 11:41:19 -0500 | [diff] [blame^] | 123 | |
| 124 | if needs_stash: |
| 125 | stash_args.append('pop') |
| 126 | stash_args.append('--quiet') |
| 127 | if GitCommand(project, stash_args).Wait() != 0: |
| 128 | return -1 |