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

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: address comment 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 ref to use for the CL.
1684
1685 Args:
1686 remote (str): The git remote for the CL.
1687 remote_branch (str): The git remote branch for the CL.
1688 target_branch (str): The target branch specified by the user.
1689 pending_prefix (str): The pending prefix from the settings.
1690 """
1691 if not (remote and remote_branch):
1692 return None
1693
1694 if target_branch:
1695 # Cannonicalize branch references to the equivalent local full symbolic
1696 # refs, which are then translated into the remote full symbolic refs
1697 # below.
1698 if '/' not in target_branch:
1699 remote_branch = 'refs/remotes/%s/%s' % (remote, target_branch)
1700 else:
1701 prefix_replacements = (
1702 ('^((refs/)?remotes/)?branch-heads/', 'refs/remotes/branch-heads/'),
1703 ('^((refs/)?remotes/)?%s/' % remote, 'refs/remotes/%s/' % remote),
1704 ('^(refs/)?heads/', 'refs/remotes/%s/' % remote),
1705 )
1706 match = None
1707 for regex, replacement in prefix_replacements:
1708 match = re.search(regex, target_branch)
1709 if match:
1710 remote_branch = target_branch.replace(match.group(0), replacement)
1711 break
1712 if not match:
1713 # This is a branch path but not one we recognize; use as-is.
1714 remote_branch = target_branch
1715 elif (not remote_branch.startswith('refs/remotes/branch-heads') and
1716 not remote_branch.startswith('refs/remotes/%s/refs' % remote)):
1717 # Default to master for refs that are not branches.
1718 remote_branch = 'refs/remotes/%s/master' % remote
1719
1720 # Create the true path to the remote branch.
1721 # Does the following translation:
1722 # * refs/remotes/origin/refs/diff/test -> refs/diff/test
1723 # * refs/remotes/origin/master -> refs/heads/master
1724 # * refs/remotes/branch-heads/test -> refs/branch-heads/test
1725 if remote_branch.startswith('refs/remotes/%s/refs/' % remote):
1726 remote_branch = remote_branch.replace('refs/remotes/%s/' % remote, '')
1727 elif remote_branch.startswith('refs/remotes/%s/' % remote):
1728 remote_branch = remote_branch.replace('refs/remotes/%s/' % remote,
1729 'refs/heads/')
1730 elif remote_branch.startswith('refs/remotes/branch-heads'):
1731 remote_branch = remote_branch.replace('refs/remotes/', 'refs/')
1732 # If a pending prefix exists then replace refs/ with it.
1733 if pending_prefix:
1734 remote_branch = remote_branch.replace('refs/', pending_prefix)
1735 return remote_branch
1736
1737
1682 def RietveldUpload(options, args, cl, change): 1738 def RietveldUpload(options, args, cl, change):
1683 """upload the patch to rietveld.""" 1739 """upload the patch to rietveld."""
1684 upload_args = ['--assume_yes'] # Don't ask about untracked files. 1740 upload_args = ['--assume_yes'] # Don't ask about untracked files.
1685 upload_args.extend(['--server', cl.GetRietveldServer()]) 1741 upload_args.extend(['--server', cl.GetRietveldServer()])
1686 if options.emulate_svn_auto_props: 1742 if options.emulate_svn_auto_props:
1687 upload_args.append('--emulate_svn_auto_props') 1743 upload_args.append('--emulate_svn_auto_props')
1688 1744
1689 change_desc = None 1745 change_desc = None
1690 1746
1691 if options.email is not None: 1747 if options.email is not None:
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1750 if not remote_url: 1806 if not remote_url:
1751 if settings.GetIsGitSvn(): 1807 if settings.GetIsGitSvn():
1752 remote_url = cl.GetGitSvnRemoteUrl() 1808 remote_url = cl.GetGitSvnRemoteUrl()
1753 else: 1809 else:
1754 if cl.GetRemoteUrl() and '/' in cl.GetUpstreamBranch(): 1810 if cl.GetRemoteUrl() and '/' in cl.GetUpstreamBranch():
1755 remote_url = (cl.GetRemoteUrl() + '@' 1811 remote_url = (cl.GetRemoteUrl() + '@'
1756 + cl.GetUpstreamBranch().split('/')[-1]) 1812 + cl.GetUpstreamBranch().split('/')[-1])
1757 if remote_url: 1813 if remote_url:
1758 upload_args.extend(['--base_url', remote_url]) 1814 upload_args.extend(['--base_url', remote_url])
1759 remote, remote_branch = cl.GetRemoteBranch() 1815 remote, remote_branch = cl.GetRemoteBranch()
1760 if remote and remote_branch: 1816 target_ref = GetTargetRef(remote, remote_branch, options.target_branch,
1761 # Create the true path to the remote branch. 1817 settings.GetPendingRefPrefix())
1762 # Does the following translation: 1818 if target_ref:
1763 # * refs/remotes/origin/refs/diff/test -> refs/diff/test 1819 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 1820
1779 project = settings.GetProject() 1821 project = settings.GetProject()
1780 if project: 1822 if project:
1781 upload_args.extend(['--project', project]) 1823 upload_args.extend(['--project', project])
1782 1824
1783 try: 1825 try:
1784 upload_args = ['upload'] + upload_args + args 1826 upload_args = ['upload'] + upload_args + args
1785 logging.info('upload.RealMain(%s)', upload_args) 1827 logging.info('upload.RealMain(%s)', upload_args)
1786 issue, patchset = upload.RealMain(upload_args) 1828 issue, patchset = upload.RealMain(upload_args)
1787 issue = int(issue) 1829 issue = int(issue)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1844 '--emulate-svn-auto-props', 1886 '--emulate-svn-auto-props',
1845 action="store_true", 1887 action="store_true",
1846 dest="emulate_svn_auto_props", 1888 dest="emulate_svn_auto_props",
1847 help="Emulate Subversion's auto properties feature.") 1889 help="Emulate Subversion's auto properties feature.")
1848 parser.add_option('-c', '--use-commit-queue', action='store_true', 1890 parser.add_option('-c', '--use-commit-queue', action='store_true',
1849 help='tell the commit queue to commit this patchset') 1891 help='tell the commit queue to commit this patchset')
1850 parser.add_option('--private', action='store_true', 1892 parser.add_option('--private', action='store_true',
1851 help='set the review private (rietveld only)') 1893 help='set the review private (rietveld only)')
1852 parser.add_option('--target_branch', 1894 parser.add_option('--target_branch',
1853 '--target-branch', 1895 '--target-branch',
1854 help='When uploading to gerrit, remote branch to ' 1896 metavar='TARGET',
1855 'use for CL. Default: master') 1897 help='Apply CL to remote ref TARGET. ' +
1898 'Default: remote branch head, or master')
1856 parser.add_option('--email', default=None, 1899 parser.add_option('--email', default=None,
1857 help='email address to use to connect to Rietveld') 1900 help='email address to use to connect to Rietveld')
1858 parser.add_option('--tbr-owners', dest='tbr_owners', action='store_true', 1901 parser.add_option('--tbr-owners', dest='tbr_owners', action='store_true',
1859 help='add a set of OWNERS to TBR') 1902 help='add a set of OWNERS to TBR')
1860 1903
1861 add_git_similarity(parser) 1904 add_git_similarity(parser)
1862 (options, args) = parser.parse_args(args) 1905 (options, args) = parser.parse_args(args)
1863 1906
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'): 1907 if is_dirty_git_tree('upload'):
1868 return 1 1908 return 1
1869 1909
1870 options.reviewers = cleanup_list(options.reviewers) 1910 options.reviewers = cleanup_list(options.reviewers)
1871 options.cc = cleanup_list(options.cc) 1911 options.cc = cleanup_list(options.cc)
1872 1912
1873 cl = Changelist() 1913 cl = Changelist()
1874 if args: 1914 if args:
1875 # TODO(ukai): is it ok for gerrit case? 1915 # TODO(ukai): is it ok for gerrit case?
1876 base_branch = args[0] 1916 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 ' 2974 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith '
2935 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) 2975 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e)))
2936 2976
2937 2977
2938 if __name__ == '__main__': 2978 if __name__ == '__main__':
2939 # These affect sys.stdout so do it outside of main() to simplify mocks in 2979 # These affect sys.stdout so do it outside of main() to simplify mocks in
2940 # unit testing. 2980 # unit testing.
2941 fix_encoding.fix_encoding() 2981 fix_encoding.fix_encoding()
2942 colorama.init() 2982 colorama.init()
2943 sys.exit(main(sys.argv[1:])) 2983 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