Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 # Copyright (C) 2008 Evan Martin <martine@danga.com> | 6 # Copyright (C) 2008 Evan Martin <martine@danga.com> |
| 7 | 7 |
| 8 """A git-command for integrating reviews on Rietveld.""" | 8 """A git-command for integrating reviews on Rietveld.""" |
| 9 | 9 |
| 10 from distutils.version import LooseVersion | 10 from distutils.version import LooseVersion |
| (...skipping 2513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2524 help='failed patches spew .rej files rather than ' | 2524 help='failed patches spew .rej files rather than ' |
| 2525 'attempting a 3-way merge') | 2525 'attempting a 3-way merge') |
| 2526 parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit', | 2526 parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit', |
| 2527 help="don't commit after patch applies") | 2527 help="don't commit after patch applies") |
| 2528 (options, args) = parser.parse_args(args) | 2528 (options, args) = parser.parse_args(args) |
| 2529 if len(args) != 1: | 2529 if len(args) != 1: |
| 2530 parser.print_help() | 2530 parser.print_help() |
| 2531 return 1 | 2531 return 1 |
| 2532 issue_arg = args[0] | 2532 issue_arg = args[0] |
| 2533 | 2533 |
| 2534 # We don't want uncommitted changes mixed up with the patch. | |
| 2535 if is_dirty_git_tree('patch'): | |
| 2536 return 1 | |
| 2537 | |
| 2534 # TODO(maruel): Use apply_issue.py | 2538 # TODO(maruel): Use apply_issue.py |
| 2535 # TODO(ukai): use gerrit-cherry-pick for gerrit repository? | 2539 # TODO(ukai): use gerrit-cherry-pick for gerrit repository? |
| 2536 | 2540 |
| 2537 if options.newbranch: | 2541 if options.newbranch: |
| 2538 if options.force: | 2542 if options.force: |
| 2539 RunGit(['branch', '-D', options.newbranch], | 2543 RunGit(['branch', '-D', options.newbranch], |
| 2540 stderr=subprocess2.PIPE, error_ok=True) | 2544 stderr=subprocess2.PIPE, error_ok=True) |
| 2541 RunGit(['checkout', '-b', options.newbranch, | 2545 RunGit(['checkout', '-b', options.newbranch, |
| 2542 Changelist().GetUpstreamBranch()]) | 2546 Changelist().GetUpstreamBranch()]) |
| 2543 | 2547 |
| 2544 return PatchIssue(issue_arg, options.reject, options.nocommit, | 2548 return PatchIssue(issue_arg, options.reject, options.nocommit, |
| 2545 options.directory) | 2549 options.directory) |
| 2546 | 2550 |
| 2547 | 2551 |
| 2548 def PatchIssue(issue_arg, reject, nocommit, directory): | 2552 def PatchIssue(issue_arg, reject, nocommit, directory): |
| 2553 # There's a "reset --hard" when failing to apply the patch. In order | |
| 2554 # not to destroy users' data, make sure the tree is not dirty here. | |
| 2555 assert(not is_dirty_git_tree('apply')) | |
| 2556 | |
| 2549 if type(issue_arg) is int or issue_arg.isdigit(): | 2557 if type(issue_arg) is int or issue_arg.isdigit(): |
| 2550 # Input is an issue id. Figure out the URL. | 2558 # Input is an issue id. Figure out the URL. |
| 2551 issue = int(issue_arg) | 2559 issue = int(issue_arg) |
| 2552 cl = Changelist(issue=issue) | 2560 cl = Changelist(issue=issue) |
| 2553 patchset = cl.GetMostRecentPatchset() | 2561 patchset = cl.GetMostRecentPatchset() |
| 2554 patch_data = cl.GetPatchSetDiff(issue, patchset) | 2562 patch_data = cl.GetPatchSetDiff(issue, patchset) |
| 2555 else: | 2563 else: |
| 2556 # Assume it's a URL to the patch. Default to https. | 2564 # Assume it's a URL to the patch. Default to https. |
| 2557 issue_url = gclient_utils.UpgradeToHttps(issue_arg) | 2565 issue_url = gclient_utils.UpgradeToHttps(issue_arg) |
| 2558 match = re.match(r'.*?/issue(\d+)_(\d+).diff', issue_url) | 2566 match = re.match(r'.*?/issue(\d+)_(\d+).diff', issue_url) |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 2587 if directory: | 2595 if directory: |
| 2588 cmd.extend(('--directory', directory)) | 2596 cmd.extend(('--directory', directory)) |
| 2589 if reject: | 2597 if reject: |
| 2590 cmd.append('--reject') | 2598 cmd.append('--reject') |
| 2591 elif IsGitVersionAtLeast('1.7.12'): | 2599 elif IsGitVersionAtLeast('1.7.12'): |
| 2592 cmd.append('--3way') | 2600 cmd.append('--3way') |
| 2593 try: | 2601 try: |
| 2594 subprocess2.check_call(cmd, env=GetNoGitPagerEnv(), | 2602 subprocess2.check_call(cmd, env=GetNoGitPagerEnv(), |
| 2595 stdin=patch_data, stdout=subprocess2.VOID) | 2603 stdin=patch_data, stdout=subprocess2.VOID) |
| 2596 except subprocess2.CalledProcessError: | 2604 except subprocess2.CalledProcessError: |
| 2605 RunGit(['reset', '--hard']) | |
|
yoichio
2015/04/27 07:52:31
This resets working tree. Why?
I prefer the state
| |
| 2597 DieWithError('Failed to apply the patch') | 2606 DieWithError('Failed to apply the patch') |
| 2598 | 2607 |
| 2599 # If we had an issue, commit the current state and register the issue. | 2608 # If we had an issue, commit the current state and register the issue. |
| 2600 if not nocommit: | 2609 if not nocommit: |
| 2601 RunGit(['commit', '-m', ('patch from issue %(i)s at patchset ' | 2610 RunGit(['commit', '-m', ('patch from issue %(i)s at patchset ' |
| 2602 '%(p)s (http://crrev.com/%(i)s#ps%(p)s)' | 2611 '%(p)s (http://crrev.com/%(i)s#ps%(p)s)' |
| 2603 % {'i': issue, 'p': patchset})]) | 2612 % {'i': issue, 'p': patchset})]) |
| 2604 cl = Changelist() | 2613 cl = Changelist() |
| 2605 cl.SetIssue(issue) | 2614 cl.SetIssue(issue) |
| 2606 cl.SetPatchset(patchset) | 2615 cl.SetPatchset(patchset) |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2893 cl = Changelist() | 2902 cl = Changelist() |
| 2894 # Ensure there actually is an issue to close. | 2903 # Ensure there actually is an issue to close. |
| 2895 cl.GetDescription() | 2904 cl.GetDescription() |
| 2896 cl.CloseIssue() | 2905 cl.CloseIssue() |
| 2897 return 0 | 2906 return 0 |
| 2898 | 2907 |
| 2899 | 2908 |
| 2900 def CMDdiff(parser, args): | 2909 def CMDdiff(parser, args): |
| 2901 """Shows differences between local tree and last upload.""" | 2910 """Shows differences between local tree and last upload.""" |
| 2902 parser.parse_args(args) | 2911 parser.parse_args(args) |
| 2912 | |
| 2913 # Uncommitted (staged and unstaged) changes will be destroyed by | |
| 2914 # "git reset --hard" if there are merging conflicts in PatchIssue(). | |
| 2915 # Staged changes would be committed along with the patch from last | |
| 2916 # upload, hence counted toward the "last upload" side in the final | |
| 2917 # diff output, and this is not what we want. | |
| 2918 if is_dirty_git_tree('diff'): | |
| 2919 return 1 | |
| 2920 | |
| 2903 cl = Changelist() | 2921 cl = Changelist() |
| 2904 issue = cl.GetIssue() | 2922 issue = cl.GetIssue() |
| 2905 branch = cl.GetBranch() | 2923 branch = cl.GetBranch() |
| 2906 if not issue: | 2924 if not issue: |
| 2907 DieWithError('No issue found for current branch (%s)' % branch) | 2925 DieWithError('No issue found for current branch (%s)' % branch) |
| 2908 TMP_BRANCH = 'git-cl-diff' | 2926 TMP_BRANCH = 'git-cl-diff' |
| 2909 base_branch = cl.GetCommonAncestorWithUpstream() | 2927 base_branch = cl.GetCommonAncestorWithUpstream() |
| 2910 | 2928 |
| 2911 # Create a new branch based on the merge-base | 2929 # Create a new branch based on the merge-base |
| 2912 RunGit(['checkout', '-q', '-b', TMP_BRANCH, base_branch]) | 2930 RunGit(['checkout', '-q', '-b', TMP_BRANCH, base_branch]) |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3137 if __name__ == '__main__': | 3155 if __name__ == '__main__': |
| 3138 # These affect sys.stdout so do it outside of main() to simplify mocks in | 3156 # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 3139 # unit testing. | 3157 # unit testing. |
| 3140 fix_encoding.fix_encoding() | 3158 fix_encoding.fix_encoding() |
| 3141 colorama.init() | 3159 colorama.init() |
| 3142 try: | 3160 try: |
| 3143 sys.exit(main(sys.argv[1:])) | 3161 sys.exit(main(sys.argv[1:])) |
| 3144 except KeyboardInterrupt: | 3162 except KeyboardInterrupt: |
| 3145 sys.stderr.write('interrupted\n') | 3163 sys.stderr.write('interrupted\n') |
| 3146 sys.exit(1) | 3164 sys.exit(1) |
| OLD | NEW |