forked from SocketDev/socket-python-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_interface.py
More file actions
51 lines (43 loc) · 1.6 KB
/
git_interface.py
File metadata and controls
51 lines (43 loc) · 1.6 KB
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
import urllib.parse
import os
from git import Repo
from socketsecurity.core import log
class Git:
repo: Repo
path: str
def __init__(self, path: str):
self.path = path
self.repo = Repo(path)
assert self.repo
self.head = self.repo.head
# Use GITHUB_SHA if available, otherwise fall back to head commit
github_sha = os.getenv('GITHUB_SHA')
if github_sha:
try:
self.commit = self.repo.commit(github_sha)
except Exception as error:
log.debug(f"Failed to get commit from GITHUB_SHA: {error}")
self.commit = self.head.commit
else:
self.commit = self.head.commit
self.repo_name = self.repo.remotes.origin.url.split('.git')[0].split('/')[-1]
try:
self.branch = self.head.reference
urllib.parse.unquote(str(self.branch))
except Exception as error:
self.branch = None
log.debug(error)
self.author = self.commit.author
self.commit_sha = self.commit.binsha
self.commit_message = self.commit.message
self.committer = self.commit.committer
self.show_files = self.repo.git.show(self.commit, name_only=True, format="%n").splitlines()
self.changed_files = []
for item in self.show_files:
if item != "":
full_path = f"{self.path}/{item}"
self.changed_files.append(full_path)
@property
def commit_str(self) -> str:
"""Return commit SHA as a string"""
return self.commit.hexsha