Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 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 | |
| 7 | import subprocess |
| 8 | |
| 9 | |
| 10 | class 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 | |
Henrique Nakashima | fdc3acb | 2017-08-10 17:40:11 -0400 | [diff] [blame] | 20 | def FetchOriginMaster(self): |
| 21 | """Fetches new changes on origin/master.""" |
| 22 | subprocess.check_output(['git', 'fetch', 'origin', 'master']) |
| 23 | |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 24 | def StashPush(self): |
| 25 | """Stashes uncommitted changes.""" |
| 26 | output = subprocess.check_output(['git', 'stash', '--include-untracked']) |
| 27 | if 'No local changes to save' in output: |
| 28 | return False |
| 29 | |
| 30 | self.stashed += 1 |
| 31 | return True |
| 32 | |
| 33 | def StashPopAll(self): |
| 34 | """Pops as many changes as this instance stashed.""" |
| 35 | while self.stashed > 0: |
| 36 | subprocess.check_output(['git', 'stash', 'pop']) |
| 37 | self.stashed -= 1 |
| 38 | |
| 39 | def GetCurrentBranchName(self): |
| 40 | """Returns a string with the current branch name.""" |
| 41 | return subprocess.check_output( |
| 42 | ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip() |
| 43 | |
Henrique Nakashima | fdc3acb | 2017-08-10 17:40:11 -0400 | [diff] [blame] | 44 | def GetCurrentBranchHash(self): |
| 45 | return subprocess.check_output( |
| 46 | ['git', 'rev-parse', 'HEAD']).strip() |
| 47 | |
| 48 | def IsCurrentBranchClean(self): |
| 49 | output = subprocess.check_output(['git', 'status', '--porcelain']) |
| 50 | return not output |
| 51 | |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 52 | def BranchExists(self, branch_name): |
| 53 | """Return whether a branch with the given name exists.""" |
| 54 | try: |
| 55 | subprocess.check_output(['git', 'rev-parse', '--verify', |
| 56 | branch_name]) |
| 57 | return True |
| 58 | except subprocess.CalledProcessError: |
| 59 | return False |
| 60 | |
| 61 | def CloneLocal(self, source_repo, new_repo): |
| 62 | subprocess.check_call(['git', 'clone', source_repo, new_repo]) |