blob: 2e94196e858276fefbbe26237a9cf8dfb38b826d [file] [log] [blame]
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -04001# Copyright 2017 The PDFium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Classes for dealing with git."""
6
7import subprocess
8
Henrique Nakashima0da39e62017-08-15 14:37:58 -04009from common import RunCommandPropagateErr
10
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -040011
12class GitHelper(object):
13 """Issues git commands. Stateful."""
14
15 def __init__(self):
16 self.stashed = 0
17
18 def Checkout(self, branch):
19 """Checks out a branch."""
Henrique Nakashima0da39e62017-08-15 14:37:58 -040020 RunCommandPropagateErr(['git', 'checkout', branch], exit_status_on_error=1)
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -040021
Henrique Nakashimafdc3acb2017-08-10 17:40:11 -040022 def FetchOriginMaster(self):
23 """Fetches new changes on origin/master."""
Henrique Nakashima0da39e62017-08-15 14:37:58 -040024 RunCommandPropagateErr(['git', 'fetch', 'origin', 'master'],
25 exit_status_on_error=1)
Henrique Nakashimafdc3acb2017-08-10 17:40:11 -040026
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -040027 def StashPush(self):
28 """Stashes uncommitted changes."""
Henrique Nakashima0da39e62017-08-15 14:37:58 -040029 output = RunCommandPropagateErr(['git', 'stash', '--include-untracked'],
30 exit_status_on_error=1)
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -040031 if 'No local changes to save' in output:
32 return False
33
34 self.stashed += 1
35 return True
36
37 def StashPopAll(self):
38 """Pops as many changes as this instance stashed."""
39 while self.stashed > 0:
Henrique Nakashima0da39e62017-08-15 14:37:58 -040040 RunCommandPropagateErr(['git', 'stash', 'pop'], exit_status_on_error=1)
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -040041 self.stashed -= 1
42
43 def GetCurrentBranchName(self):
44 """Returns a string with the current branch name."""
Henrique Nakashima0da39e62017-08-15 14:37:58 -040045 return RunCommandPropagateErr(
46 ['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
47 exit_status_on_error=1).strip()
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -040048
Henrique Nakashimafdc3acb2017-08-10 17:40:11 -040049 def GetCurrentBranchHash(self):
Henrique Nakashima0da39e62017-08-15 14:37:58 -040050 return RunCommandPropagateErr(
51 ['git', 'rev-parse', 'HEAD'], exit_status_on_error=1).strip()
Henrique Nakashimafdc3acb2017-08-10 17:40:11 -040052
53 def IsCurrentBranchClean(self):
Henrique Nakashima0da39e62017-08-15 14:37:58 -040054 output = RunCommandPropagateErr(['git', 'status', '--porcelain'],
55 exit_status_on_error=1)
Henrique Nakashimafdc3acb2017-08-10 17:40:11 -040056 return not output
57
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -040058 def BranchExists(self, branch_name):
59 """Return whether a branch with the given name exists."""
Henrique Nakashima0da39e62017-08-15 14:37:58 -040060 output = RunCommandPropagateErr(['git', 'rev-parse', '--verify',
61 branch_name])
62 return output is not None
Henrique Nakashimaf24fc1e2017-08-03 13:29:22 -040063
64 def CloneLocal(self, source_repo, new_repo):
Henrique Nakashima0da39e62017-08-15 14:37:58 -040065 RunCommandPropagateErr(['git', 'clone', source_repo, new_repo],
66 exit_status_on_error=1)