-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_api.py
79 lines (72 loc) · 2.5 KB
/
git_api.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
import datetime
import pathlib
import subprocess
import tempfile
first_commit_hash = ['git', 'hash-object', '-t', 'tree', '/dev/null']
def create_branch(
branch_name: str,
cwd: pathlib.Path,
):
# git checkout -b {branch_name}
cmd = ['git', 'checkout', '-b', branch_name]
output = subprocess.run(cmd, capture_output=True, cwd=str(cwd))
# print('create_branch, stdout:', output.stdout.decode())
# print('create_branch, stderr:', output.stderr.decode())
return output.returncode, output.stdout.decode(), output.stderr.decode()
def add_file(
file_name: pathlib.Path,
cwd: pathlib.Path,
):
# git add {file_name}
cmd = ['git', 'add', str(file_name)]
output = subprocess.run(cmd, capture_output=True, cwd=str(cwd))
# print('add_file, stdout:', output.stdout.decode())
# print('add_file, stderr:', output.stderr.decode())
return output.returncode, output.stdout.decode(), output.stderr.decode()
def commit(
desc: str,
author: str,
date: datetime.datetime,
cwd: pathlib.Path,
):
# git commit -m {desc} --date={date.isoformat()} --author={author}
cmd = [
'git', 'commit',
'-m', f'{desc}'
]
name = author.split('<')[0].strip()
email = author.split('<')[0].replace('>', '').strip()
env = {
'GIT_AUTHOR_NAME': name,
'GIT_AUTHOR_EMAIL': email,
'GIT_AUTHOR_DATE': date.isoformat(),
'GIT_COMMITTER_NAME': name,
'GIT_COMMITTER_EMAIL': email,
'GIT_COMMITTER_DATE': date.isoformat(),
}
output = subprocess.run(cmd, capture_output=True, cwd=str(cwd), env=env)
# print('Commit, stdout:', output.stdout.decode())
# print('Commit, stderr:', output.stderr.decode())
return output.returncode, output.stdout.decode(), output.stderr.decode()
def create_file(
file: pathlib.Path,
diff: bytes,
cwd: pathlib.Path,
):
# git apply {diff}
# print(f'{cwd=}')
# print(f'{file=}')
new_file: pathlib.Path = cwd / file
new_file.parent.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile('wb') as temp_file:
temp_file.write(diff)
temp_file.flush()
cmd = ['git', 'apply', temp_file.name]
output = subprocess.run(
cmd,
capture_output=True,
cwd=str(cwd) if cwd is not None else cwd
)
# print('Create_file, stdout: ', output.stdout.decode())
# print('Create_file, stderr: ', output.stderr.decode())
return output.returncode, output.stdout.decode(), output.stderr.decode()