Chromium Code Reviews| Index: checkout.py |
| =================================================================== |
| --- checkout.py (revision 218449) |
| +++ checkout.py (working copy) |
| @@ -552,14 +552,17 @@ |
| class GitCheckoutBase(CheckoutBase): |
|
M-A Ruel
2013/08/23 17:05:10
The reason for this base class was for a git-svn s
rmistry
2013/08/26 13:08:17
Makes sense, Done.
|
| """Base class for git checkout. Not to be used as-is.""" |
| - def __init__(self, root_dir, project_name, remote_branch, |
| + def __init__(self, root_dir, project_name, remote_branch, git_url, |
| post_processors=None): |
| super(GitCheckoutBase, self).__init__( |
| root_dir, project_name, post_processors) |
| - # There is no reason to not hardcode it. |
| - self.remote = 'origin' |
| + self.git_url = git_url |
| self.remote_branch = remote_branch |
| + # The working branch where patches will be applied. It will track the |
| + # remote branch. |
| self.working_branch = 'working_branch' |
| + # There is no reason to not hardcode origin. |
| + self.remote = 'origin' |
| def prepare(self, revision): |
| """Resets the git repository in a clean state. |
| @@ -567,8 +570,22 @@ |
| Checks it out if not present and deletes the working branch. |
| """ |
| assert self.remote_branch |
| - assert os.path.isdir(self.project_path) |
| - self._check_call_git(['reset', '--hard', '--quiet']) |
| + |
| + if not os.path.isdir(self.project_path): |
| + # Clone the repo if the directory is not present. |
| + logging.info('Checking out %s in %s' % |
| + (self.project_name, self.project_path)) |
| + assert self.git_url |
|
M-A Ruel
2013/08/23 17:05:10
This assert should be in the constructor.
rmistry
2013/08/26 13:08:17
Done.
|
| + self._check_call_git( |
| + ['clone', self.git_url, '-b', self.remote_branch, self.project_path], |
| + cwd=None, timeout=FETCH_TIMEOUT) |
| + else: |
| + # Throw away all uncommitted changes in the existing checkout. |
| + self._check_call_git(['checkout', self.remote_branch]) |
| + self._check_call_git( |
| + ['reset', '--hard', '--quiet', |
| + '%s/%s' % (self.remote, self.remote_branch)]) |
| + |
| if revision: |
| try: |
|
M-A Ruel
2013/08/23 17:05:10
Add a comment:
# Look if the commit hash already e
rmistry
2013/08/26 13:08:17
Done.
|
| revision = self._check_output_git(['rev-parse', revision]) |
| @@ -581,12 +598,17 @@ |
| branches, active = self._branches() |
| if active != 'master': |
| self._check_call_git(['checkout', '--force', '--quiet', 'master']) |
| - self._check_call_git(['pull', self.remote, self.remote_branch, '--quiet']) |
| + self._check_call_git(['pull', '--quiet']) |
|
M-A Ruel
2013/08/23 17:05:10
Why not keep the explicit remote?
rmistry
2013/08/26 13:08:17
There is difference in behavior here between these
M-A Ruel
2013/08/26 14:01:11
Wouha, thanks, I had never realized that.
Then I'
rmistry
2013/08/26 15:16:17
Done.
Isaac (away)
2013/09/02 23:38:58
Why not "git fetch origin"? git pull is roughly a
rmistry
2013/09/03 12:40:36
The documentation on pull and fetch are a little c
|
| if self.working_branch in branches: |
| self._call_git(['branch', '-D', self.working_branch]) |
| + return self._get_revision() |
| + def _get_revision(self): |
| + """Gets the current revision from the local branch.""" |
| + return self._check_output_git(['rev-parse', 'HEAD']).strip() |
| + |
| def apply_patch(self, patches, post_processors=None, verbose=False): |
| - """Applies a patch on 'working_branch' and switch to it. |
| + """Applies a patch on 'working_branch' and switches to it. |
| Also commits the changes on the local branch. |
| @@ -597,8 +619,9 @@ |
| # trying again? |
| if self.remote_branch: |
| self._check_call_git( |
| - ['checkout', '-b', self.working_branch, |
| - '%s/%s' % (self.remote, self.remote_branch), '--quiet']) |
| + ['checkout', '-b', self.working_branch, '-t', self.remote_branch, |
| + '--quiet']) |
| + |
| for index, p in enumerate(patches): |
| stdout = [] |
| try: |
| @@ -667,20 +690,20 @@ |
| if verbose: |
| cmd.append('--verbose') |
| self._check_call_git(cmd) |
| - # TODO(maruel): Weirdly enough they don't match, need to investigate. |
| - #found_files = self._check_output_git( |
| - # ['diff', 'master', '--name-only']).splitlines(False) |
| - #assert sorted(patches.filenames) == sorted(found_files), ( |
| - # sorted(out), sorted(found_files)) |
| + found_files = self._check_output_git( |
| + ['diff', '%s/%s' % (self.remote, self.remote_branch), |
| + '--name-only']).splitlines(False) |
| + assert sorted(patches.filenames) == sorted(found_files), ( |
| + sorted(patches.filenames), sorted(found_files)) |
| def commit(self, commit_message, user): |
| - """Updates the commit message. |
| + """Commits and Updates the commit message. |
| Subclass needs to dcommit or push. |
| """ |
| assert isinstance(commit_message, unicode) |
| self._check_call_git(['commit', '--amend', '-m', commit_message]) |
|
M-A Ruel
2013/08/23 17:08:04
You also want '--author', user. You don't need any
rmistry
2013/08/26 13:08:17
I do not completely understand this. If the CQ is
M-A Ruel
2013/08/26 14:01:11
What tool output this error, git? It's interesting
rmistry
2013/08/26 15:16:17
I see what I was doing wrong. I was doing:
--autho
rmistry
2013/08/27 12:44:49
Went ahead and added functionality for --author @
|
| - return self._check_output_git(['rev-parse', 'HEAD']).strip() |
| + return self._get_revision() |
| def _check_call_git(self, args, **kwargs): |
| kwargs.setdefault('cwd', self.project_path) |
| @@ -733,11 +756,21 @@ |
| class GitCheckout(GitCheckoutBase): |
| """Git checkout implementation.""" |
| def _fetch_remote(self): |
| - # git fetch is always verbose even with -q -q so redirect its output. |
| + # git fetch is always verbose even with -q, so redirect its output. |
| self._check_output_git(['fetch', self.remote, self.remote_branch], |
| timeout=FETCH_TIMEOUT) |
| + def commit(self, commit_message, user): |
| + """Updates the commit message and pushes.""" |
| + # Update the commit message and 'git commit' by calling the superclass. |
| + rev = super(GitCheckout, self).commit(commit_message, user) |
| + # Push to the remote repository. |
| + self._check_call_git( |
| + ['push', 'origin', '%s:%s' % (self.working_branch, self.remote_branch), |
| + '--force', '--quiet']) |
| + return rev |
|
M-A Ruel
2013/08/23 17:05:10
Also, it should switch back to the master branch a
rmistry
2013/08/26 13:08:17
That is done in prepare step before the patches ar
M-A Ruel
2013/08/26 14:01:11
What I'm saying is that it should be done at the 2
rmistry
2013/08/26 15:16:17
I guess it does not hurt to have it in both places
|
| + |
| class ReadOnlyCheckout(object): |
| """Converts a checkout into a read-only one.""" |
| def __init__(self, checkout, post_processors=None): |