Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: git_cl.py

Issue 16211005: Allow --3way to be passed to `git apply` via `git cl patch`. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: add a git version check ( >= 1.7.12 ) Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import difflib 10 import difflib
11 from distutils.version import LooseVersion
M-A Ruel 2013/07/09 13:27:15 I don't think this exists on Windows.
tapted 2013/07/10 01:11:52 All my tests seem happy. I think this is just part
11 import json 12 import json
12 import logging 13 import logging
13 import optparse 14 import optparse
14 import os 15 import os
15 import re 16 import re
16 import stat 17 import stat
17 import sys 18 import sys
18 import textwrap 19 import textwrap
19 import urllib2 20 import urllib2
20 import urlparse 21 import urlparse
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 try: 78 try:
78 out, code = subprocess2.communicate(['git', '--no-pager'] + args, 79 out, code = subprocess2.communicate(['git', '--no-pager'] + args,
79 stdout=subprocess2.PIPE) 80 stdout=subprocess2.PIPE)
80 return code, out[0] 81 return code, out[0]
81 except ValueError: 82 except ValueError:
82 # When the subprocess fails, it returns None. That triggers a ValueError 83 # When the subprocess fails, it returns None. That triggers a ValueError
83 # when trying to unpack the return value into (out, code). 84 # when trying to unpack the return value into (out, code).
84 return 1, '' 85 return 1, ''
85 86
86 87
88 def IsGitVersionAtLeast(min_version):
89 PREFIX='git version '
90 version = RunGit(['--version']).strip()
91 return (version.startswith(PREFIX) and
92 LooseVersion(version[len(PREFIX):]) >= LooseVersion(min_version))
93
94
87 def usage(more): 95 def usage(more):
88 def hook(fn): 96 def hook(fn):
89 fn.usage_more = more 97 fn.usage_more = more
90 return fn 98 return fn
91 return hook 99 return hook
92 100
93 101
94 def ask_for_data(prompt): 102 def ask_for_data(prompt):
95 try: 103 try:
96 return raw_input(prompt) 104 return raw_input(prompt)
(...skipping 1595 matching lines...) Expand 10 before | Expand all | Expand 10 after
1692 1700
1693 1701
1694 @usage('<patch url or issue id>') 1702 @usage('<patch url or issue id>')
1695 def CMDpatch(parser, args): 1703 def CMDpatch(parser, args):
1696 """patch in a code review""" 1704 """patch in a code review"""
1697 parser.add_option('-b', dest='newbranch', 1705 parser.add_option('-b', dest='newbranch',
1698 help='create a new branch off trunk for the patch') 1706 help='create a new branch off trunk for the patch')
1699 parser.add_option('-f', action='store_true', dest='force', 1707 parser.add_option('-f', action='store_true', dest='force',
1700 help='with -b, clobber any existing branch') 1708 help='with -b, clobber any existing branch')
1701 parser.add_option('--reject', action='store_true', dest='reject', 1709 parser.add_option('--reject', action='store_true', dest='reject',
1702 help='allow failed patches and spew .rej files') 1710 help='failed patches spew .rej files rather than '
1711 'attempting a 3-way merge')
1703 parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit', 1712 parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit',
1704 help="don't commit after patch applies") 1713 help="don't commit after patch applies")
1705 (options, args) = parser.parse_args(args) 1714 (options, args) = parser.parse_args(args)
1706 if len(args) != 1: 1715 if len(args) != 1:
1707 parser.print_help() 1716 parser.print_help()
1708 return 1 1717 return 1
1709 issue_arg = args[0] 1718 issue_arg = args[0]
1710 1719
1711 # TODO(maruel): Use apply_issue.py 1720 # TODO(maruel): Use apply_issue.py
1712 # TODO(ukai): use gerrit-cherry-pick for gerrit repository? 1721 # TODO(ukai): use gerrit-cherry-pick for gerrit repository?
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1750 ['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'], stdin=patch_data) 1759 ['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'], stdin=patch_data)
1751 except subprocess2.CalledProcessError: 1760 except subprocess2.CalledProcessError:
1752 DieWithError('Git patch mungling failed.') 1761 DieWithError('Git patch mungling failed.')
1753 logging.info(patch_data) 1762 logging.info(patch_data)
1754 # We use "git apply" to apply the patch instead of "patch" so that we can 1763 # We use "git apply" to apply the patch instead of "patch" so that we can
1755 # pick up file adds. 1764 # pick up file adds.
1756 # The --index flag means: also insert into the index (so we catch adds). 1765 # The --index flag means: also insert into the index (so we catch adds).
1757 cmd = ['git', '--no-pager', 'apply', '--index', '-p0'] 1766 cmd = ['git', '--no-pager', 'apply', '--index', '-p0']
1758 if options.reject: 1767 if options.reject:
1759 cmd.append('--reject') 1768 cmd.append('--reject')
1769 elif IsGitVersionAtLeast('1.7.12'):
1770 cmd.append('--3way')
1760 try: 1771 try:
1761 subprocess2.check_call(cmd, stdin=patch_data, stdout=subprocess2.VOID) 1772 subprocess2.check_call(cmd, stdin=patch_data, stdout=subprocess2.VOID)
1762 except subprocess2.CalledProcessError: 1773 except subprocess2.CalledProcessError:
1763 DieWithError('Failed to apply the patch') 1774 DieWithError('Failed to apply the patch')
1764 1775
1765 # If we had an issue, commit the current state and register the issue. 1776 # If we had an issue, commit the current state and register the issue.
1766 if not options.nocommit: 1777 if not options.nocommit:
1767 RunGit(['commit', '-m', 'patch from issue %s' % issue]) 1778 RunGit(['commit', '-m', 'patch from issue %s' % issue])
1768 cl = Changelist() 1779 cl = Changelist()
1769 cl.SetIssue(issue) 1780 cl.SetIssue(issue)
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
2142 GenUsage(parser, 'help') 2153 GenUsage(parser, 'help')
2143 return CMDhelp(parser, argv) 2154 return CMDhelp(parser, argv)
2144 2155
2145 2156
2146 if __name__ == '__main__': 2157 if __name__ == '__main__':
2147 # These affect sys.stdout so do it outside of main() to simplify mocks in 2158 # These affect sys.stdout so do it outside of main() to simplify mocks in
2148 # unit testing. 2159 # unit testing.
2149 fix_encoding.fix_encoding() 2160 fix_encoding.fix_encoding()
2150 colorama.init() 2161 colorama.init()
2151 sys.exit(main(sys.argv[1:])) 2162 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698