-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithelper.py
73 lines (64 loc) · 2.16 KB
/
githelper.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from git import Repo
from git.exc import NoSuchPathError
from termcolor import colored
from hashstore import HashStore
import sys
import functools
import time
class GitHelper:
def __init__(self, directory):
self.directory = directory
try:
repo = Repo(directory)
except NoSuchPathError as e:
print(colored("Specified directory does not exists", "red"))
sys.exit(0)
if repo.bare:
print(colored("The directory does not contains the .git folder", "red"))
sys.exit(0)
self.repo = repo
self.ignored_hashes = HashStore()
@functools.lru_cache(maxsize = None)
def branches(self):
return [branch.name for branch in self.repo.branches]
def validate_branches_are_present(self, *args):
for branch in args:
if branch not in self.branches():
print(colored("Specified branch {} does not exists".format(branch), "red"))
sys.exit(0)
def commits(self, branch_name, max_count = None):
if max_count is not None:
return list(self.repo.iter_commits(branch_name, max_count = max_count))
else:
return list(self.repo.iter_commits(branch_name))
def different_commits(self, commits_in_first, commits_in_second):
different_commits = []
for commit in commits_in_first:
matched = False
for second_commit in commits_in_second:
if second_commit.message == commit.message and second_commit.stats.files == commit.stats.files:
matched = True
if not matched:
different_commits.append(commit)
return different_commits
def should_print_commit(self, commit):
return not self.ignored_hashes.is_present(commit.hexsha)
def resolve_commit(self, commit, resolve):
if resolve:
while True:
inp = input("Resolve[R] / Ignore[I] : ")
if inp.upper() == 'I':
ignoredHashes.insert(commit.hexsha)
break
elif inp.upper() == 'R':
break
else:
print("Invalid choice. Please try again.")
@staticmethod
def print_commit_info(commit):
print(colored("commit {}".format(commit.hexsha), 'yellow'))
print("Author: {} <{}>".format(commit.author.name, commit.author.email))
print("Date : {}".format(time.strftime("%c %Z", time.localtime(commit.authored_date))))
print()
print("\t{}".format(commit.message.strip()))
print()