-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.py
176 lines (152 loc) · 4.77 KB
/
git.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# library for git related functions
import os
import pygit2
from rich.console import Console
from rich.progress import Progress
import rich
class MyRemoteCallbacks(pygit2.RemoteCallbacks):
def transfer_progress(self, stats):
print(f'{stats.indexed_objects}/{stats.total_objects}')
class CloneCallbacks(pygit2.RemoteCallbacks):
def __init__(self, credentials=None, certificate=None, console=None):
"""
Init for callbacks
:param credentials: ssh credentials
:param certificate: tls certificate to use
:param console: rich console to use for updates
"""
super().__init__(credentials=credentials, certificate=certificate)
if console is None:
self.console = Console()
else:
self.console = console
self.progress = None
self.transferring = False
self.transfer_task = None
def start_progress(self) -> None:
"""
Set up a progress bar for cloning progress updates
:return: None
"""
self.progress = Progress()
def sideband_progress(self, string):
"""
Progress output callback. Override this function with your own
progress reporting function
Parameters:
string : str
Progress output from the remote.
"""
self.console.print(string)
def transfer_progress(self, stats) -> None:
"""
Update git repo sync progress bar
:param stats: stats from pygit2.clone_repository callback
:return: None
"""
print(f"{stats.indexed_objects}/{stats.total_objects}")
# if not self.transferring:
# print("Setup")
# self.transfer_task = self.progress.add_task("Syncing git objects...", total=stats.total_objects)
# print("Update")
# self.progress.update(self.transfer_task, advance=stats.indexed_objects)
# print("Update done")
def stop_progress(self) -> None:
"""
Finish a progress bar for cloning progress updates
:return: None
"""
self.progress = None
self.transferring = False
self.transfer_task = None
def checkout_repo(repo_url: str, repo_dir: str = None, console: rich.console.Console = None) -> pygit2.Repository | None:
"""
Clone repository, exit with error message if something goes wrong
:param repo_url: url to repository (e.g. https://github.com/ssl-hep/foo.git
:param repo_dir: directory for clone to go to
:param console: optional rich Console to update
"""
if console is None:
console = Console()
clone_callbacks = CloneCallbacks(console=console)
console.print(f"Checking out {repo_url}")
try:
os.system(f"git clone {repo_url} {repo_dir}")
repo = pygit2.Repository(repo_dir)
# print(repo_dir)
# clone_callbacks.start_progress()
# print(type(clone_callbacks))
# print(type(MyRemoteCallbacks()))
# repo = pygit2.clone_repository(repo_url, repo_dir, callbacks=clone_callbacks)
# repo = pygit2.clone_repository(repo_url, repo_dir, callbacks=MyRemoteCallbacks())
except Exception as e:
# if repo_dir:
# shutil.rmtree(repo_dir)
print(f"exception {e}")
return None
finally:
clone_callbacks.stop_progress()
return repo
def add_file(repo: pygit2.Repository, add_file: str) -> bool:
"""
Add a file to commit
:param repo: repo being used
:param add_file: path to file being added
:return: True on success, False otherwise
"""
cwd = os.getcwd()
try:
os.chdir(repo.workdir)
exit_code = os.system(f"git add {add_file}")
return exit_code == 0
finally:
os.chdir(cwd)
def checkout_branch(repo: pygit2.Repository, branch: str) -> bool:
"""
Switch to specified branch in given repo
:param repo: repo being used
:param branch: name of branch to switch to
:return: True on success, False otherwise
"""
# ref = repo.lookup_branch(branch)
# if not ref:
# print("no ref")
# return False
# repo.checkout(ref)
# return True
# checking out the branch doesn't always work so use git directly for now
cwd = os.getcwd()
try:
os.chdir(repo.workdir)
exit_code = os.system(f"git switch {branch}")
return exit_code == 0
finally:
os.chdir(cwd)
def commit(repo: pygit2.Repository) -> bool:
"""
Probably should use pygit2 for this, but it adds a lot of work to
get commit signing working correctly, so using git commit is easier
:param repo: repo being used
:return: True on success, False on failure
"""
start_dir = os.getcwd()
try:
os.chdir(repo.workdir)
exit_code = os.system("git commit")
return exit_code == 0
finally:
os.chdir(start_dir)
def push(repo: pygit2.Repository) -> bool:
"""
Push changes to origin
:param repo: repo being used
:return: True on success, False on failure
"""
start_dir = os.getcwd()
try:
os.chdir(repo.workdir)
exit_code = os.system("git push origin")
return exit_code == 0
finally:
os.chdir(start_dir)
# testing workflow actions