blob: 4269ae8984dfa76a27c433ad189ad458b5c345dc [file] [log] [blame]
Ravi Mistrybd2d1112016-11-17 08:13:51 -05001#!/usr/bin/env python
2# Copyright (c) 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""This module contains functions for using git."""
7
8import re
Ravi Mistrybadc1372016-11-23 08:38:18 -05009import shutil
Ravi Mistrybd2d1112016-11-17 08:13:51 -050010import subprocess
Ravi Mistrybadc1372016-11-23 08:38:18 -050011import tempfile
12
13import utils
Ravi Mistrybd2d1112016-11-17 08:13:51 -050014
15
16class GitLocalConfig(object):
17 """Class to manage local git configs."""
18 def __init__(self, config_dict):
19 self._config_dict = config_dict
20 self._previous_values = {}
21
22 def __enter__(self):
23 for k, v in self._config_dict.iteritems():
24 try:
25 prev = subprocess.check_output(['git', 'config', '--local', k]).rstrip()
26 if prev:
27 self._previous_values[k] = prev
28 except subprocess.CalledProcessError:
29 # We are probably here because the key did not exist in the config.
30 pass
31 subprocess.check_call(['git', 'config', '--local', k, v])
32
33 def __exit__(self, exc_type, _value, _traceback):
34 for k in self._config_dict:
35 if self._previous_values.get(k):
36 subprocess.check_call(
37 ['git', 'config', '--local', k, self._previous_values[k]])
38 else:
39 subprocess.check_call(['git', 'config', '--local', '--unset', k])
40
41
42class GitBranch(object):
43 """Class to manage git branches.
44
45 This class allows one to create a new branch in a repository to make changes,
46 then it commits the changes, switches to master branch, and deletes the
47 created temporary branch upon exit.
48 """
49 def __init__(self, branch_name, commit_msg, upload=True, commit_queue=False,
Ravi Mistrya4ceaa12017-12-07 14:46:40 -050050 delete_when_finished=True, cc_list=None):
Ravi Mistrybd2d1112016-11-17 08:13:51 -050051 self._branch_name = branch_name
52 self._commit_msg = commit_msg
53 self._upload = upload
54 self._commit_queue = commit_queue
55 self._patch_set = 0
56 self._delete_when_finished = delete_when_finished
Ravi Mistrya4ceaa12017-12-07 14:46:40 -050057 self._cc_list = cc_list
Ravi Mistrybd2d1112016-11-17 08:13:51 -050058
59 def __enter__(self):
60 subprocess.check_call(['git', 'reset', '--hard', 'HEAD'])
61 subprocess.check_call(['git', 'checkout', 'master'])
62 if self._branch_name in subprocess.check_output(['git', 'branch']).split():
63 subprocess.check_call(['git', 'branch', '-D', self._branch_name])
64 subprocess.check_call(['git', 'checkout', '-b', self._branch_name,
65 '-t', 'origin/master'])
66 return self
67
68 def commit_and_upload(self, use_commit_queue=False):
69 """Commit all changes and upload a CL, returning the issue URL."""
70 subprocess.check_call(['git', 'commit', '-a', '-m', self._commit_msg])
71 upload_cmd = ['git', 'cl', 'upload', '-f', '--bypass-hooks',
72 '--bypass-watchlists']
73 self._patch_set += 1
74 if self._patch_set > 1:
75 upload_cmd.extend(['-t', 'Patch set %d' % self._patch_set])
76 if use_commit_queue:
77 upload_cmd.append('--use-commit-queue')
Ravi Mistrycf79ee52017-07-14 08:15:58 -040078 # Need the --send-mail flag to publish the CL and remove WIP bit.
79 upload_cmd.append('--send-mail')
Ravi Mistrya4ceaa12017-12-07 14:46:40 -050080 if self._cc_list:
81 upload_cmd.extend(['--cc=%s' % ','.join(self._cc_list)])
Ravi Mistrybd2d1112016-11-17 08:13:51 -050082 subprocess.check_call(upload_cmd)
83 output = subprocess.check_output(['git', 'cl', 'issue']).rstrip()
84 return re.match('^Issue number: (?P<issue>\d+) \((?P<issue_url>.+)\)$',
85 output).group('issue_url')
86
87 def __exit__(self, exc_type, _value, _traceback):
88 if self._upload:
89 # Only upload if no error occurred.
90 try:
91 if exc_type is None:
92 self.commit_and_upload(use_commit_queue=self._commit_queue)
93 finally:
94 subprocess.check_call(['git', 'checkout', 'master'])
95 if self._delete_when_finished:
96 subprocess.check_call(['git', 'branch', '-D', self._branch_name])
Ravi Mistrybadc1372016-11-23 08:38:18 -050097
98
99class NewGitCheckout(utils.tmp_dir):
100 """Creates a new local checkout of a Git repository."""
101
Eric Borenf19ef9b2019-04-17 12:32:07 -0400102 def __init__(self, repository, local=None):
Ravi Mistrybadc1372016-11-23 08:38:18 -0500103 """Set parameters for this local copy of a Git repository.
104
105 Because this is a new checkout, rather than a reference to an existing
106 checkout on disk, it is safe to assume that the calling thread is the
107 only thread manipulating the checkout.
108
109 You must use the 'with' statement to create this object:
110
111 with NewGitCheckout(*args) as checkout:
112 # use checkout instance
113 # the checkout is automatically cleaned up here
114
115 Args:
116 repository: URL of the remote repository (e.g.,
117 'https://skia.googlesource.com/common') or path to a local repository
118 (e.g., '/path/to/repo/.git') to check out a copy of
Eric Borenf19ef9b2019-04-17 12:32:07 -0400119 local: optional path to an existing copy of the remote repo on local disk.
120 If provided, the initial clone is performed with the local copy as the
121 upstream, then the upstream is switched to the remote repo and the
122 new copy is updated from there.
Ravi Mistrybadc1372016-11-23 08:38:18 -0500123 """
124 super(NewGitCheckout, self).__init__()
125 self._repository = repository
Eric Borenf19ef9b2019-04-17 12:32:07 -0400126 self._local = local
Ravi Mistrybadc1372016-11-23 08:38:18 -0500127
128 @property
129 def root(self):
130 """Returns the root directory containing the checked-out files."""
131 return self.name
132
133 def __enter__(self):
134 """Check out a new local copy of the repository.
135
136 Uses the parameters that were passed into the constructor.
137 """
138 super(NewGitCheckout, self).__enter__()
Eric Borenf19ef9b2019-04-17 12:32:07 -0400139 remote = self._repository
140 if self._local:
141 remote = self._local
142 subprocess.check_output(args=['git', 'clone', remote, self.root])
143 if self._local:
144 subprocess.check_call([
145 'git', 'remote', 'set-url', 'origin', self._repository])
146 subprocess.check_call(['git', 'remote', 'update'])
147 subprocess.check_call(['git', 'checkout', 'master'])
148 subprocess.check_call(['git', 'reset', '--hard', 'origin/master'])
Ravi Mistrybadc1372016-11-23 08:38:18 -0500149 return self