-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathadmin.py
44 lines (34 loc) · 1.38 KB
/
admin.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
import subprocess
import os
import argparse
def find_last_commit_with_path(path):
if os.path.exists(path):
raise ValueError(f"Path {path} already exists!")
try:
# This gives us the hash that deleted the file
commit_hash = subprocess.check_output(
["git", "log", "-n", "1", "--pretty=format:%H", "--", path],
).decode("utf-8").strip()
# This gives us the hash before that one
commit_hash = subprocess.check_output(
["git", "log", "-n", "1", "--pretty=format:%H", f"{commit_hash}^"],
).decode("utf8").strip()
return commit_hash
except subprocess.CalledProcessError:
return None
def copy_from_commit(commit_hash, path):
os.makedirs(path, exist_ok=True)
subprocess.run(["git", "checkout", commit_hash, "--", path])
def main():
parser = argparse.ArgumentParser(description="Copies an old assignment into the worktree.")
parser.add_argument("assignment", help="An assignment, e.g. 'assign0', 'assign1', etc.")
args = parser.parse_args()
path = args.assignment
commit_hash = find_last_commit_with_path(path)
if commit_hash:
print(f"Last commit for {path}: {commit_hash}")
copy_from_commit(commit_hash, path)
print(f"Copied {path} from commit {commit_hash}.")
else:
print(f"No commits found for the specified assignment: {path}.")
main()