• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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"""Classes for dealing with git."""
5
6import subprocess
7
8# pylint: disable=relative-import
9from common import RunCommandPropagateErr
10
11
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."""
20    RunCommandPropagateErr(['git', 'checkout', branch], exit_status_on_error=1)
21
22  def FetchOriginMaster(self):
23    """Fetches new changes on origin/master."""
24    RunCommandPropagateErr(['git', 'fetch', 'origin', 'master'],
25                           exit_status_on_error=1)
26
27  def StashPush(self):
28    """Stashes uncommitted changes."""
29    output = RunCommandPropagateErr(['git', 'stash', '--include-untracked'],
30                                    exit_status_on_error=1)
31    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:
40      RunCommandPropagateErr(['git', 'stash', 'pop'], exit_status_on_error=1)
41      self.stashed -= 1
42
43  def GetCurrentBranchName(self):
44    """Returns a string with the current branch name."""
45    return RunCommandPropagateErr(['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
46                                  exit_status_on_error=1).strip()
47
48  def GetCurrentBranchHash(self):
49    return RunCommandPropagateErr(['git', 'rev-parse', 'HEAD'],
50                                  exit_status_on_error=1).strip()
51
52  def IsCurrentBranchClean(self):
53    output = RunCommandPropagateErr(['git', 'status', '--porcelain'],
54                                    exit_status_on_error=1)
55    return not output
56
57  def BranchExists(self, branch_name):
58    """Return whether a branch with the given name exists."""
59    output = RunCommandPropagateErr(
60        ['git', 'rev-parse', '--verify', branch_name])
61    return output is not None
62
63  def CloneLocal(self, source_repo, new_repo):
64    RunCommandPropagateErr(['git', 'clone', source_repo, new_repo],
65                           exit_status_on_error=1)
66