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 | |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 9 | from common import RunCommandPropagateErr |
| 10 | |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 11 | |
| 12 | class 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 Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 20 | RunCommandPropagateErr(['git', 'checkout', branch], exit_status_on_error=1) |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 21 | |
Henrique Nakashima | fdc3acb | 2017-08-10 17:40:11 -0400 | [diff] [blame] | 22 | def FetchOriginMaster(self): |
| 23 | """Fetches new changes on origin/master.""" |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 24 | RunCommandPropagateErr(['git', 'fetch', 'origin', 'master'], |
| 25 | exit_status_on_error=1) |
Henrique Nakashima | fdc3acb | 2017-08-10 17:40:11 -0400 | [diff] [blame] | 26 | |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 27 | def StashPush(self): |
| 28 | """Stashes uncommitted changes.""" |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 29 | output = RunCommandPropagateErr(['git', 'stash', '--include-untracked'], |
| 30 | exit_status_on_error=1) |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 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: |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 40 | RunCommandPropagateErr(['git', 'stash', 'pop'], exit_status_on_error=1) |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 41 | self.stashed -= 1 |
| 42 | |
| 43 | def GetCurrentBranchName(self): |
| 44 | """Returns a string with the current branch name.""" |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 45 | return RunCommandPropagateErr( |
| 46 | ['git', 'rev-parse', '--abbrev-ref', 'HEAD'], |
| 47 | exit_status_on_error=1).strip() |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 48 | |
Henrique Nakashima | fdc3acb | 2017-08-10 17:40:11 -0400 | [diff] [blame] | 49 | def GetCurrentBranchHash(self): |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 50 | return RunCommandPropagateErr( |
| 51 | ['git', 'rev-parse', 'HEAD'], exit_status_on_error=1).strip() |
Henrique Nakashima | fdc3acb | 2017-08-10 17:40:11 -0400 | [diff] [blame] | 52 | |
| 53 | def IsCurrentBranchClean(self): |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 54 | output = RunCommandPropagateErr(['git', 'status', '--porcelain'], |
| 55 | exit_status_on_error=1) |
Henrique Nakashima | fdc3acb | 2017-08-10 17:40:11 -0400 | [diff] [blame] | 56 | return not output |
| 57 | |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 58 | def BranchExists(self, branch_name): |
| 59 | """Return whether a branch with the given name exists.""" |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 60 | output = RunCommandPropagateErr(['git', 'rev-parse', '--verify', |
| 61 | branch_name]) |
| 62 | return output is not None |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame] | 63 | |
| 64 | def CloneLocal(self, source_repo, new_repo): |
Henrique Nakashima | 0da39e6 | 2017-08-15 14:37:58 -0400 | [diff] [blame^] | 65 | RunCommandPropagateErr(['git', 'clone', source_repo, new_repo], |
| 66 | exit_status_on_error=1) |