forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_helpers.py
39 lines (30 loc) · 900 Bytes
/
git_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import subprocess
from pathlib import Path
from shutil import which
def run_git_command(command):
process = subprocess.run(
command,
encoding='utf8',
capture_output=True,
check=True,
)
return process.stdout.strip()
def git_current_branch():
return run_git_command(['git', 'symbolic-ref', 'HEAD', '--short'])
def git_commit_hash(ref: str = 'HEAD'):
return run_git_command(['git', 'rev-parse', '--verify', ref])
def git_diff(file_a: Path, file_b: Path) -> int:
if which('git') is None:
raise RuntimeError('git not found.')
return subprocess.run([
'git',
'diff',
'--color',
'--word-diff=plain',
'--word-diff-regex=.',
'--ignore-space-change',
'--ignore-blank-lines',
'--exit-code',
file_a,
file_b,
], encoding='utf8', check=False).returncode