forked from dodola/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_utils.py
58 lines (47 loc) · 1.87 KB
/
git_utils.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os.path
import subprocess
def _get_diff_base():
"""Returns the newest local commit that is also in the upstream branch, or
"HEAD" if there is no upstream branch.
"""
try:
upstream = subprocess.check_output([
"git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"
]).strip()
# Get local commits not in upstream.
local_commits = filter(
len,
subprocess.check_output(
["git", "rev-list", "HEAD", "^" + upstream, "--"]).split("\n"))
if not local_commits:
return "HEAD"
# Return parent of the oldest commit.
return subprocess.check_output(
["git", "rev-parse", local_commits[-1] + "^"]).strip()
except subprocess.CalledProcessError:
return "HEAD"
def get_git_root():
"""Returns the path of the root of the git repository."""
return subprocess.check_output(["git", "rev-parse",
"--show-toplevel"]).strip()
def get_diff_files():
"""Returns absolute paths to files that are locally modified, staged or
touched by any commits introduced on the local branch.
"""
list_command = [
"git", "diff-index", "--name-only",
_get_diff_base()
]
git_root_path = get_git_root()
paths = filter(len, subprocess.check_output(list_command).split("\n"))
return [ os.path.join(git_root_path, x) for x in paths ]
def get_all_files():
"""Returns absolute paths to all files in the git repo under the current
working directory.
"""
list_command = ["git", "ls-files"]
paths = filter(len, subprocess.check_output(list_command).split("\n"))
return [ os.path.abspath(x) for x in paths ]