-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.py
104 lines (93 loc) · 4.45 KB
/
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
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
import os
import reconstruct as re
import terminal_output as TO
import shutil
def pastport_checkout(new_file_location, commit_id):
file_name_with_extension = os.path.basename(new_file_location)
old_file_location = os.path.dirname(new_file_location) + f"/pastport\u00b6/{file_name_with_extension}"
if os.path.exists(new_file_location):
normal_checkout(new_file_location=new_file_location, old_file_location=old_file_location,commit_id=commit_id)
else:
if os.path.exists(old_file_location):
deleted_checkout(old_file_location=old_file_location, new_file_location=new_file_location, commit_id=commit_id)
else:
TO.output(message=f"[Error] Unknown and Untracked file detected !!!\nFile location: {new_file_location}", color="r")
return False
return True
def deleted_checkout(old_file_location,new_file_location, commit_id):
if commit_id == 0:
shutil.copy2(src=old_file_location, dst=new_file_location)
return
file_name_with_extension = os.path.basename(old_file_location)
file_name, extension = os.path.splitext(file_name_with_extension)
track_file_location = os.path.dirname(old_file_location) + f"/{file_name}_{extension[1:]}.track"
new_file_location = os.path.dirname(os.path.dirname(old_file_location)) + f"/{file_name}{extension}"
# print(old_file_location)
# print(new_file_location)
commit_data = {}
with open(track_file_location, "r") as track_file:
try:
track_line = track_file.readlines()[commit_id]
except:
TO.output(message="\u26a0 [Error]: Invalid commit id!!!", color="r")
return
commit_data = extract_commit_data(tracked_line=track_line)
new_file_lines = re.reconstruct_file(old_file_location=old_file_location, commit_data=commit_data)
with open(new_file_location, "w") as new_file:
for new_line in new_file_lines:
new_file.write(new_line + "\n")
def normal_checkout(new_file_location, old_file_location, commit_id):
if commit_id == 0:
shutil.copy2(src=old_file_location, dst=new_file_location)
return
new_file_location = os.path.abspath(new_file_location)
file_name_with_extension = os.path.basename(new_file_location)
file_name, extension = os.path.splitext(file_name_with_extension)
track_file_location = os.path.dirname(new_file_location) + f"/pastport\u00b6/{file_name}_{extension[1:]}.track"
old_file_location = os.path.dirname(new_file_location) + f"/pastport\u00b6/{file_name_with_extension}"
commit_data = {}
with open(track_file_location, "r") as track_file:
try:
track_line = track_file.readlines()[commit_id]
except:
TO.output(message="\u26a0 [Error]: Invalid commit id!!!", color="r")
return
commit_data = extract_commit_data(tracked_line=track_line)
new_file_lines = re.reconstruct_file(old_file_location=old_file_location, commit_data=commit_data)
with open(new_file_location, "w") as new_file:
for new_line in new_file_lines:
new_file.write(new_line + "\n")
def extract_commit_data(tracked_line):
tracked_line = tracked_line.strip("\n")
cd = {}
tracking_data = tracked_line.split("\u00b6")
commit_message = tracking_data[-1]
pointer = 0
while pointer < len(tracking_data) - 2:
pointer += 1
line_number = int(tracking_data[pointer])
insertion = []
deletion = []
pointer += 1
ins_len = int(tracking_data[pointer])
pointer += 1
del_len = int(tracking_data[pointer])
for _ in range(ins_len):
pointer += 1
ins_word = tracking_data[pointer]
pointer += 1
ins_index = int(tracking_data[pointer])
insertion.append((ins_word, ins_index))
for _ in range(del_len):
pointer += 1
del_word = tracking_data[pointer]
pointer += 1
del_index = int(tracking_data[pointer])
deletion.append((del_word, del_index))
cd[line_number] = [insertion, deletion]
return cd
# pastport_checkout(r"C:\Users\HP\Desktop\test\New Text Document.txt", 4)
# "C:\Users\HP\Desktop\test\New Rich Text Document (2).rtf"
# pastport_checkout(r"C:\Users\HP\Desktop\test\New Rich Text Document (2).rtf", 4)
# "C:\Users\HP\Desktop\test\New Text Document (2).txt"
# pastport_checkout(r"C:\Users\HP\Desktop\test\New Text Document (2).txt", 1)