• 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
5"""Classes for dealing with git."""
6
7import subprocess
8
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(
46        ['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
47        exit_status_on_error=1).strip()
48
49  def GetCurrentBranchHash(self):
50    return RunCommandPropagateErr(
51        ['git', 'rev-parse', 'HEAD'], exit_status_on_error=1).strip()
52
53  def IsCurrentBranchClean(self):
54    output = RunCommandPropagateErr(['git', 'status', '--porcelain'],
55                                    exit_status_on_error=1)
56    return not output
57
58  def BranchExists(self, branch_name):
59    """Return whether a branch with the given name exists."""
60    output = RunCommandPropagateErr(['git', 'rev-parse', '--verify',
61                                     branch_name])
62    return output is not None
63
64  def CloneLocal(self, source_repo, new_repo):
65    RunCommandPropagateErr(['git', 'clone', source_repo, new_repo],
66                           exit_status_on_error=1)
67