-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·194 lines (123 loc) · 5.6 KB
/
test.py
File metadata and controls
executable file
·194 lines (123 loc) · 5.6 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
import os
import sys
import pickle
from shutil import copyfile
from os import listdir
import difflib
class Commit(object):
def __init__(self, commitId, parent, message):
self.commitId = commitId
self.parent = parent
self.message = message
class Git(object):
def __init__(self):
self.lastcommitId = -1
self.HEAD = None
def commit(self, message):
self.lastcommitId = self.lastcommitId + 1
new_commit = Commit(self.lastcommitId, self.HEAD, message)
self.HEAD = new_commit
return new_commit
def log(self):
commit = self.HEAD
history = []
while (commit):
history.append(commit)
commit = commit.parent
return history
argument = sys.argv[1]
if len(sys.argv) == 3:
specific = sys.argv[2]
elif len(sys.argv) == 4:
message = sys.argv[3]
files_in_cwd = os.listdir(os.getcwd())
if os.path.exists('.gitpy/'):
files_in_gitpy = os.listdir(os.getcwd() + "/.gitpy")
if argument == "init":
if not os.path.exists('.gitpy'):
os.makedirs('.gitpy')
print "Initialized an empty repository in " + os.getcwd()
else:
print "Already Exists"
repo = Git()
os.makedirs('.gitpy/dump')
fo = open(".gitpy/dump/dump.txt", "wb")
pickle.dump(repo, fo)
elif argument == "add":
fo = open(".gitpy/dump/dump.txt", "r")
repo = pickle.load(fo)
if specific:
if specific == "-A" or specific == "-a":
fo = open(".gitpy/dump/addinfo.txt", "wb")
for untracked in files_in_cwd:
if not os.path.isdir(os.getcwd()+ "/" + untracked) and untracked!="test.py":
if untracked not in files_in_gitpy:
print untracked
fo.write(untracked + "\n")
elif argument == "commit":
fo = open(".gitpy/dump/dump.txt", "r")
repo = pickle.load(fo)
fo.close()
try:
specific
except NameError:
print "Provide a message!"
else:
repo.commit(specific)
fo = open(".gitpy/dump/addinfo.txt", "r+")
change = fo.read()
fo.close()
change = change.splitlines()
if change:
if not os.path.exists('.gitpy/' + str(repo.HEAD.commitId)):
os.makedirs(".gitpy/" + str(repo.HEAD.commitId))
for i in change:
if i in os.listdir(os.getcwd()):
copyfile(os.getcwd()+ "/" + i, os.getcwd()+ "/.gitpy/" + str(repo.HEAD.commitId) + "/" + i)
fo = open(".gitpy/dump/addinfo.txt", 'w').close()
else:
print "Cannot commit before adding"
fo = open(".gitpy/dump/dump.txt", "wb")
pickle.dump(repo,fo)
fo.close()
elif argument == "diff":
fo = open(".gitpy/dump/dump.txt", "r")
repo = pickle.load(fo)
fo.close()
if repo.HEAD:
try:
specific
except NameError:
for i in files_in_cwd:
if not os.path.isdir(os.getcwd()+ "/" + i) and i!="test.py":
fo = open(i, "r+")
string1 = fo.read()
string1 = string1.splitlines()
fi = open(".gitpy/" + str(repo.HEAD.commitId) + "/" + i, "r+")
string2 = fi.read()
string2 = string2.splitlines()
if string1 != string2:
print 'Printing diff for file:' + i
diff = difflib.unified_diff(string2, string1, lineterm='')
print '\n'.join(list(diff))
print '\n\n\n'
else:
fo = open(specific, "r+")
string1 = fo.read()
string1 = string1.splitlines()
fi = open(".gitpy/" + str(repo.HEAD.commitId) + "/" + specific)
string2 = fo.read()
string2 = string2.splitlines()
diff = difflib.unified_diff(string2, string1, lineterm='')
print '\n'.join(list(diff))
print '\n\n\n'
else:
print "Cannot compare since no commits in place"
elif argument == "log":
fo = open(".gitpy/dump/dump.txt", "r")
repo = pickle.load(fo)
fo.close()
print repo.log()
else:
print "No recognized argument!"