blob: 42cc57d3040e78bb4003d5ce74b59d4927af1c55 [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
9
10class GitHelper(object):
11 """Issues git commands. Stateful."""
12
13 def __init__(self):
14 self.stashed = 0
15
16 def Checkout(self, branch):
17 """Checks out a branch."""
18 subprocess.check_output(['git', 'checkout', branch])
19
20 def StashPush(self):
21 """Stashes uncommitted changes."""
22 output = subprocess.check_output(['git', 'stash', '--include-untracked'])
23 if 'No local changes to save' in output:
24 return False
25
26 self.stashed += 1
27 return True
28
29 def StashPopAll(self):
30 """Pops as many changes as this instance stashed."""
31 while self.stashed > 0:
32 subprocess.check_output(['git', 'stash', 'pop'])
33 self.stashed -= 1
34
35 def GetCurrentBranchName(self):
36 """Returns a string with the current branch name."""
37 return subprocess.check_output(
38 ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip()
39
40 def BranchExists(self, branch_name):
41 """Return whether a branch with the given name exists."""
42 try:
43 subprocess.check_output(['git', 'rev-parse', '--verify',
44 branch_name])
45 return True
46 except subprocess.CalledProcessError:
47 return False
48
49 def CloneLocal(self, source_repo, new_repo):
50 subprocess.check_call(['git', 'clone', source_repo, new_repo])