Debian Codegraph @

share/analysis-scripts/git_utils.py

raw
1 ##########################################################################
2 # #
3 # This file is part of Frama-C. #
4 # #
5 # Copyright (C) 2007-2022 #
6 # CEA (Commissariat à l'énergie atomique et aux énergies #
7 # alternatives) #
8 # #
9 # you can redistribute it and/or modify it under the terms of the GNU #
10 # Lesser General Public License as published by the Free Software #
11 # Foundation, version 2.1. #
12 # #
13 # It is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16 # GNU Lesser General Public License for more details. #
17 # #
18 # See the GNU Lesser General Public License version 2.1 #
19 # for more details (enclosed in the file licenses/LGPLv2.1). #
20 # #
21 ##########################################################################
22
23 import subprocess
24
25
26 def rev_parse(gitdir, rev):
27 res = subprocess.run(
28 ["git", "rev-parse", rev],
29 stdout=subprocess.PIPE,
30 stderr=subprocess.DEVNULL,
31 encoding="ascii",
32 cwd=gitdir,
33 )
34 name = res.stdout.strip()
35 return name if name else None
36
37
38 def name_rev(gitdir, rev):
39 res = subprocess.run(
40 ["git", "name-rev", "--name-only", rev],
41 stdout=subprocess.PIPE,
42 stderr=subprocess.DEVNULL,
43 encoding="ascii",
44 cwd=gitdir,
45 )
46 name = res.stdout.strip()
47 return name if name else None
48
49
50 def current_rev(gitdir):
51 return name_rev(gitdir, "HEAD")
52
53
54 def is_clean(gitdir):
55 # git diff and diff-index are not working on some of our case studies to
56 # decide whether the workingin dir is clean or not ; git status is more
57 # reliable
58 res = subprocess.run(
59 ["git", "status", "--untracked-files=no", "--porcelain"],
60 stdout=subprocess.PIPE,
61 stderr=subprocess.DEVNULL,
62 encoding="ascii",
63 cwd=gitdir,
64 )
65 return res.returncode == 0 and not res.stdout