-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitpython_clone_and_checkout.py
56 lines (45 loc) · 1.27 KB
/
gitpython_clone_and_checkout.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
import os
import sys
from git import Repo
url = "https://github.com/hihunjin/Code-snippet-for-everything"
master_path = "main"
to_path = "temp_"
number_of_loop = 5
transform_config = dict(
resize=dict(
size=(300, 150),
),
)
# clone main branch
cloned_repo = Repo.clone_from(
url=url,
to_path=master_path,
branch=master_path,
)
# get commit ids
commit_ids = cloned_repo.git.log(f"-{number_of_loop}", "--pretty=format:%h").split("\n")
def clone_and_checkout(prefix: str = "", commit_id: str = "") -> Repo:
cloned_repo = Repo.clone_from(
url=url,
to_path=prefix + commit_id,
branch="main",
)
print(f"======== cloned at {prefix + commit_id} ========")
cloned_repo.git.checkout(commit_id)
return cloned_repo
if __name__ == "__main__":
# do preprocess
print("init")
for commit_id in commit_ids:
print(f" << commit id: {commit_id} >>")
# clone and checkout
cloned_repo = clone_and_checkout(to_path, commit_id)
# add system path
dir_name = cloned_repo.working_dir.split("/")[-1]
sys.path.append(dir_name)
# do jobs
L = os.listdir(dir_name)
print("os.listdir()", L)
# remove system path and del
sys.path.pop()
del L