-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitHelpers.py
139 lines (110 loc) · 3.32 KB
/
gitHelpers.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
from datetime import datetime, timezone, timedelta
from unicodedata import name
import pygit2 as git
from pygit2._pygit2 import GitError
# Format: Fri Sep 2 19:36:07 2022 +0530
GIT_TIME_FORMAT = "%c %z"
def getLastModifiedStr(date):
"""
Returns last modified string
date: offset-aware datetime.datetime object
"""
# Get time difference
now = datetime.now(timezone.utc)
delta = now - date
output = ""
days = delta.days
if days <= 0:
hours = delta.seconds // 3600
if hours <= 0:
mins = (delta.seconds // 60) % 60
if mins <= 0:
secs = delta.seconds - hours * 3600 - mins * 60
if secs <= 0:
output = "now"
# Secs
elif secs == 1:
output = f"{secs} sec"
else:
output = f"{secs} sec"
# Mins
elif mins == 1:
output = f"{mins} min"
else:
output = f"{mins} mins"
# Hours
elif hours == 1:
output = f"{hours} hr"
else:
output = f"{hours} hrs"
# Days
elif days == 1:
output = f"{days} day"
else:
output = f"{days} days"
return output
def commit(repo, message):
"""Add all and commit changes to current branch"""
# Add all
repo.index.add_all()
repo.index.write()
name = repo.config["User.name"]
email = repo.config["User.email"]
signature = git.Signature(name, email)
tree = repo.index.write_tree()
try:
# Assuming prior commits exist
ref = repo.head.name
parents = [repo.head.target]
except GitError:
# Initial Commit
ref = "HEAD"
parents = []
repo.create_commit(
ref,
signature,
signature,
message,
tree,
parents
)
def getCommits(repo):
"""Returns a list commit objects"""
commits = []
last = repo[repo.head.target]
for commit in repo.walk(last.id, git.GIT_SORT_TIME):
timezoneInfo = timezone(timedelta(minutes=commit.author.offset))
datetimeString = datetime.fromtimestamp(float(commit.author.time),
timezoneInfo).strftime(GIT_TIME_FORMAT)
commitDict = {}
commitDict["id"] = commit.hex
commitDict["name"] = commit.author.name
commitDict["email"] = commit.author.email
commitDict["date"] = datetimeString
commitDict["message"] = commit.message.strip(" \t\n\r")
commits.append(commitDict)
return commits
def makeGitIgnore(path):
"""Generates .gitignore file for Blendit project at given path"""
content = (
"# Blendit\n"
"assets/\n"
"*.blend\n"
"*.blend*\n"
"\n"
"# Python\n"
"# Byte-compiled / optimized / DLL files\n"
"__pycache__/\n"
"*.py[cod]\n"
"*$py.class\n"
"\n"
"# C extensions\n"
"*.so\n"
)
with open(os.path.join(path, ".gitignore"), "w") as file:
file.write(content)
def configUser(repo, name, email):
"""Set user.name and user.email to the given Repo object"""
repo.config["User.name"] = name
repo.config["User.email"] = email