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

Side by Side Diff: git_cl.py

Issue 822503005: Support --target-branch option to git-cl upload for Rietveld (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: remove unnecessary statement Created 5 years, 11 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 | tests/git_cl_test.py » ('j') | 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 from distutils.version import LooseVersion 10 from distutils.version import LooseVersion
(...skipping 1661 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 git_command = ['push'] 1672 git_command = ['push']
1673 if receive_options: 1673 if receive_options:
1674 git_command.append('--receive-pack=git receive-pack %s' % 1674 git_command.append('--receive-pack=git receive-pack %s' %
1675 ' '.join(receive_options)) 1675 ' '.join(receive_options))
1676 git_command += [remote, 'HEAD:refs/for/' + branch] 1676 git_command += [remote, 'HEAD:refs/for/' + branch]
1677 RunGit(git_command) 1677 RunGit(git_command)
1678 # TODO(ukai): parse Change-Id: and set issue number? 1678 # TODO(ukai): parse Change-Id: and set issue number?
1679 return 0 1679 return 0
1680 1680
1681 1681
1682 def GetTargetRef(remote, remote_branch, target_branch, pending_prefix):
1683 """Computes the remote branch given the remote and remote branch state from
agable 2015/01/26 17:37:03 nit: Docstring style is """Single line descripti
Mike Wittman 2015/01/26 19:31:08 Done.
1684 the CL, the user's specified target branch (if any), and the pending prefix.
1685 """
1686 if not (remote and remote_branch):
1687 return None
1688
1689 if target_branch:
1690 # Cannonicalize branch references to the equivalent local full symbolic
1691 # refs, which are then translated into the remote full symbolic refs
1692 # below.
1693 if '/' not in target_branch:
1694 remote_branch = 'refs/remotes/%s/%s' % (remote, target_branch)
1695 else:
1696 prefix_replacements = (
1697 ('^((refs/)?remotes/)?branch-heads/', 'refs/remotes/branch-heads/'),
1698 ('^((refs/)?remotes/)?%s/' % remote, 'refs/remotes/%s/' % remote),
1699 ('^(refs/)?heads/', 'refs/remotes/%s/' % remote),
1700 )
1701 match = None
1702 for regex, replacement in prefix_replacements:
1703 match = re.search(regex, target_branch)
1704 if match:
1705 remote_branch = target_branch.replace(match.group(0), replacement)
1706 break
1707 if not match:
1708 # This is a branch path but not one we recognize; use as-is.
1709 remote_branch = target_branch
1710 elif (not remote_branch.startswith('refs/remotes/branch-heads') and
1711 not remote_branch.startswith('refs/remotes/%s/refs' % remote)):
1712 # Default to master for refs that are not branches.
1713 remote_branch = 'refs/remotes/%s/master' % remote
1714
1715 # Create the true path to the remote branch.
1716 # Does the following translation:
1717 # * refs/remotes/origin/refs/diff/test -> refs/diff/test
1718 # * refs/remotes/origin/master -> refs/heads/master
1719 # * refs/remotes/branch-heads/test -> refs/branch-heads/test
1720 if remote_branch.startswith('refs/remotes/%s/refs/' % remote):
1721 remote_branch = remote_branch.replace('refs/remotes/%s/' % remote, '')
1722 elif remote_branch.startswith('refs/remotes/%s/' % remote):
1723 remote_branch = remote_branch.replace('refs/remotes/%s/' % remote,
1724 'refs/heads/')
1725 elif remote_branch.startswith('refs/remotes/branch-heads'):
1726 remote_branch = remote_branch.replace('refs/remotes/', 'refs/')
1727 # If a pending prefix exists then replace refs/ with it.
1728 if pending_prefix:
1729 remote_branch = remote_branch.replace('refs/', pending_prefix)
1730 return remote_branch
1731
1732
1682 def RietveldUpload(options, args, cl, change): 1733 def RietveldUpload(options, args, cl, change):
1683 """upload the patch to rietveld.""" 1734 """upload the patch to rietveld."""
1684 upload_args = ['--assume_yes'] # Don't ask about untracked files. 1735 upload_args = ['--assume_yes'] # Don't ask about untracked files.
1685 upload_args.extend(['--server', cl.GetRietveldServer()]) 1736 upload_args.extend(['--server', cl.GetRietveldServer()])
1686 if options.emulate_svn_auto_props: 1737 if options.emulate_svn_auto_props:
1687 upload_args.append('--emulate_svn_auto_props') 1738 upload_args.append('--emulate_svn_auto_props')
1688 1739
1689 change_desc = None 1740 change_desc = None
1690 1741
1691 if options.email is not None: 1742 if options.email is not None:
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1750 if not remote_url: 1801 if not remote_url:
1751 if settings.GetIsGitSvn(): 1802 if settings.GetIsGitSvn():
1752 remote_url = cl.GetGitSvnRemoteUrl() 1803 remote_url = cl.GetGitSvnRemoteUrl()
1753 else: 1804 else:
1754 if cl.GetRemoteUrl() and '/' in cl.GetUpstreamBranch(): 1805 if cl.GetRemoteUrl() and '/' in cl.GetUpstreamBranch():
1755 remote_url = (cl.GetRemoteUrl() + '@' 1806 remote_url = (cl.GetRemoteUrl() + '@'
1756 + cl.GetUpstreamBranch().split('/')[-1]) 1807 + cl.GetUpstreamBranch().split('/')[-1])
1757 if remote_url: 1808 if remote_url:
1758 upload_args.extend(['--base_url', remote_url]) 1809 upload_args.extend(['--base_url', remote_url])
1759 remote, remote_branch = cl.GetRemoteBranch() 1810 remote, remote_branch = cl.GetRemoteBranch()
1760 if remote and remote_branch: 1811 target_ref = GetTargetRef(remote, remote_branch, options.target_branch,
1761 # Create the true path to the remote branch. 1812 settings.GetPendingRefPrefix())
1762 # Does the following translation: 1813 if target_ref:
1763 # * refs/remotes/origin/refs/diff/test -> refs/diff/test 1814 upload_args.extend(['--target_ref', target_ref])
1764 # * refs/remotes/origin/master -> refs/heads/master
1765 # * refs/remotes/branch-heads/test -> refs/branch-heads/test
1766 if remote_branch.startswith('refs/remotes/%s/refs/' % remote):
1767 remote_branch = remote_branch.replace('refs/remotes/%s/' % remote, '')
1768 elif remote_branch.startswith('refs/remotes/%s/' % remote):
1769 remote_branch = remote_branch.replace('refs/remotes/%s/' % remote,
1770 'refs/heads/')
1771 elif remote_branch.startswith('refs/remotes/branch-heads'):
1772 remote_branch = remote_branch.replace('refs/remotes/', 'refs/')
1773 pending_prefix = settings.GetPendingRefPrefix()
1774 # If a pending prefix exists then replace refs/ with it.
1775 if pending_prefix:
1776 remote_branch = remote_branch.replace('refs/', pending_prefix)
1777 upload_args.extend(['--target_ref', remote_branch])
1778 1815
1779 project = settings.GetProject() 1816 project = settings.GetProject()
1780 if project: 1817 if project:
1781 upload_args.extend(['--project', project]) 1818 upload_args.extend(['--project', project])
1782 1819
1783 try: 1820 try:
1784 upload_args = ['upload'] + upload_args + args 1821 upload_args = ['upload'] + upload_args + args
1785 logging.info('upload.RealMain(%s)', upload_args) 1822 logging.info('upload.RealMain(%s)', upload_args)
1786 issue, patchset = upload.RealMain(upload_args) 1823 issue, patchset = upload.RealMain(upload_args)
1787 issue = int(issue) 1824 issue = int(issue)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1844 '--emulate-svn-auto-props', 1881 '--emulate-svn-auto-props',
1845 action="store_true", 1882 action="store_true",
1846 dest="emulate_svn_auto_props", 1883 dest="emulate_svn_auto_props",
1847 help="Emulate Subversion's auto properties feature.") 1884 help="Emulate Subversion's auto properties feature.")
1848 parser.add_option('-c', '--use-commit-queue', action='store_true', 1885 parser.add_option('-c', '--use-commit-queue', action='store_true',
1849 help='tell the commit queue to commit this patchset') 1886 help='tell the commit queue to commit this patchset')
1850 parser.add_option('--private', action='store_true', 1887 parser.add_option('--private', action='store_true',
1851 help='set the review private (rietveld only)') 1888 help='set the review private (rietveld only)')
1852 parser.add_option('--target_branch', 1889 parser.add_option('--target_branch',
1853 '--target-branch', 1890 '--target-branch',
1854 help='When uploading to gerrit, remote branch to ' 1891 metavar='TARGET',
1855 'use for CL. Default: master') 1892 help='Apply CL to remote ref TARGET. ' +
1893 'Default: remote branch head, or master')
1856 parser.add_option('--email', default=None, 1894 parser.add_option('--email', default=None,
1857 help='email address to use to connect to Rietveld') 1895 help='email address to use to connect to Rietveld')
1858 parser.add_option('--tbr-owners', dest='tbr_owners', action='store_true', 1896 parser.add_option('--tbr-owners', dest='tbr_owners', action='store_true',
1859 help='add a set of OWNERS to TBR') 1897 help='add a set of OWNERS to TBR')
1860 1898
1861 add_git_similarity(parser) 1899 add_git_similarity(parser)
1862 (options, args) = parser.parse_args(args) 1900 (options, args) = parser.parse_args(args)
1863 1901
1864 if options.target_branch and not settings.GetIsGerrit():
1865 parser.error('Use --target_branch for non gerrit repository.')
1866
1867 if is_dirty_git_tree('upload'): 1902 if is_dirty_git_tree('upload'):
1868 return 1 1903 return 1
1869 1904
1870 options.reviewers = cleanup_list(options.reviewers) 1905 options.reviewers = cleanup_list(options.reviewers)
1871 options.cc = cleanup_list(options.cc) 1906 options.cc = cleanup_list(options.cc)
1872 1907
1873 cl = Changelist() 1908 cl = Changelist()
1874 if args: 1909 if args:
1875 # TODO(ukai): is it ok for gerrit case? 1910 # TODO(ukai): is it ok for gerrit case?
1876 base_branch = args[0] 1911 base_branch = args[0]
(...skipping 1057 matching lines...) Expand 10 before | Expand all | Expand 10 after
2934 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' 2969 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith '
2935 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) 2970 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e)))
2936 2971
2937 2972
2938 if __name__ == '__main__': 2973 if __name__ == '__main__':
2939 # These affect sys.stdout so do it outside of main() to simplify mocks in 2974 # These affect sys.stdout so do it outside of main() to simplify mocks in
2940 # unit testing. 2975 # unit testing.
2941 fix_encoding.fix_encoding() 2976 fix_encoding.fix_encoding()
2942 colorama.init() 2977 colorama.init()
2943 sys.exit(main(sys.argv[1:])) 2978 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « no previous file | tests/git_cl_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698