-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlet.py
428 lines (357 loc) · 12.5 KB
/
gitlet.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# Entry point for application
import os
import sys
import uuid
from util.branches import Branches
from util.commit import Commit
from util.blob import Blob
from util.stage import Stage
args = sys.argv[1:]
# Commands
def init():
if os.path.exists(".gitlet"):
print("ERROR: A gitlet directory already exists here")
return
os.makedirs(".gitlet/blobs")
os.makedirs(".gitlet/stage")
os.makedirs(".gitlet/commits")
os.makedirs(".gitlet/data")
file = open('.gitlet/rm_on_commit', 'w')
file.close()
newCommitId = uuid.uuid4().hex
Branches.updateCurrentBranch("master")
Branches.updateCurrentCommitId(newCommitId)
commit = Commit()
commit.createNew(newCommitId, '', 'Initial Commit', {})
commit.writeCommit()
print("gitlet repository has been initialized")
def add(filename):
Stage.addToStagingArea(filename)
print("the file has been added")
def commit(message):
commit = Commit()
commit.addCommit(uuid.uuid4().hex, message)
commit.writeCommit()
Stage.clearStagingArea()
def log():
id = Branches.getCurrentCommitId()
commit = Commit()
commit.getFromId(id)
while commit is not False:
print('===')
print(f'commit {commit.id}')
# NEED STUFF FOR MERGE
if commit.merge:
print(f'Merge: {commit.parent[0]} {commit.parent[1]}')
print(f'Date: {commit.time}')
print(commit.message)
if not commit.merge:
commit = commit.getParent()
else:
commit = commit.getParent()[0]
print()
def branch(branchName):
if branchName == "head":
print("a branch with this name is not allowed")
return
if os.path.exists(os.path.join('.gitlet/data', branchName)):
print('a branch with that name already exists')
return
currentCommitId = Branches.getCurrentCommitId()
Branches.updateCurrentBranch(branchName)
Branches.updateCurrentCommitId(currentCommitId)
print("branch has been added")
def rmBranch(branchname):
if branchname == "head":
print("a branch with that name does not exist")
elif Branches.getCurrentBranch() == branchname:
print("cannot remove the current branch")
elif not os.path.exists(os.path.join('.gitlet/data', branchname)):
print('a branch with that name does not exist')
else:
os.remove(os.path.join('.gitlet/data', branchname))
print("the branch has been removed")
def rm(filename):
if not os.path.exists(filename):
print("the file does not exist")
return
if os.path.exists(os.path.join('.gitlet/stage/', filename)):
Stage.removeFromStagingArea(filename)
print('the file has been removed from the staging area')
return
currentCommit = Commit()
currentCommit.getFromId(Branches.getCurrentCommitId())
if filename not in currentCommit.fileMaps.keys():
print("You have no reason to rm this file")
return
Stage.addNotInclude(filename)
os.remove(filename)
print("the file has been removed")
def status():
print('=== Branches ===')
currentBranch = Branches.getCurrentBranch()
for branch in Branches.getAllBranches():
if branch == currentBranch:
print(f"*{currentBranch}")
else:
print(branch)
print()
print('=== Staged Files ===')
for file in Stage.getStagedFiles():
print(file)
print()
print('=== Removed Files ===')
for file in Stage.getNotInclude():
print(file)
print()
currentCommitId = Branches.getCurrentCommitId()
currentCommit = Commit()
currentCommit.getFromId(currentCommitId)
print('=== Modifictations Not Staged for Commit ===')
for file in currentCommit.fileMaps:
if not os.path.exists(file):
print(f"{file} (deleted)")
else:
blobContent = Blob.getContent(currentCommit.fileMaps[file])
f = open(file, 'r')
content = f.readlines()
f.close()
if content != blobContent:
print(f'{file} (modified)')
print()
untrackedFiles = currentCommit.getUntrackedFiles()
print('=== Untracked Files ===')
for file in untrackedFiles:
print(file)
print()
def checkoutFile(filename):
print("trying to checkout")
currentCommitId = Branches.getCurrentCommitId()
currentCommit = Commit()
# adding some try/catch stuff here in the future
currentCommit.getFromId(currentCommitId)
Blob.writeFileFromBlob(currentCommit.fileMaps[filename], filename)
print("checkout is complete")
def checkoutFileByCommitId(filename, commitId):
commit = Commit()
commit.getFromId(commitId)
print("the commit file maps are")
print(commit.fileMaps)
Blob.writeFileFromBlob(commit.fileMaps[filename], filename)
print('checkout is complete')
def checkoutBranch(branchName):
if branchName == "head" or not os.path.exists(
os.path.join('.gitlet/data', branchName)):
print('the branch does not exist')
exit()
# old commit
oldId = Branches.getCurrentCommitId()
oldCommit = Commit()
oldCommit.getFromId(oldId)
oldFileMaps = oldCommit.fileMaps
Branches.updateCurrentBranch(branchName)
commitId = Branches.getCurrentCommitId()
commit = Commit()
commit.getFromId(commitId)
fileMaps = commit.fileMaps
for file in fileMaps:
Blob.writeFileFromBlob(fileMaps[file], file)
for file in oldFileMaps:
if file not in fileMaps:
os.remove(file)
print('checked out the branch')
def reset(commitId):
currentCommitId = Branches.getCurrentCommitId()
currentCommit = Commit()
currentCommit.getFromId(currentCommitId)
currentFileMaps = currentCommit.fileMaps
resetCommit = Commit()
resetCommit.getFromId(commitId)
resetFileMaps = resetCommit.fileMaps
for file in resetFileMaps:
Blob.writeFileFromBlob(resetFileMaps[file], file)
for file in currentFileMaps:
if file not in resetFileMaps:
os.remove(file)
Branches.updateCurrentCommitId(commitId)
def merge(branchName):
# Failure Cases
if len(Stage.getStagedFiles()) > 0 or len(Stage.getNotInclude()) > 0:
print("You have uncommited changes")
return
currentBranchName = Branches.getCurrentBranch()
if currentBranchName == branchName:
print("Cannot merge a branch with itself")
return
if branchName not in Branches.getAllBranches():
print("Branch name not in branches")
return
# get current Commit
currentCommitId = Branches.getCurrentCommitId()
currentCommit = Commit()
currentCommit.getFromId(currentCommitId)
givenCommitId = Branches.getCommidIdByBranch(branchName)
givenCommit = Commit()
givenCommit.getFromId(givenCommitId)
splitPointId = Branches.getSplitPoint(currentCommit, givenCommit)
if splitPointId == givenCommitId:
print("Given branch is an ancestor of the current branch")
exit()
elif splitPointId == currentCommitId:
Branches.updateCurrentCommitId(givenCommitId)
checkoutBranch(currentBranchName)
print('Current Branch fast-forwarded')
exit()
splitPoint = Commit()
splitPoint.getFromId(splitPointId)
newCommit = Commit(merge=True)
newFiles = {}
merges = []
untracked = currentCommit.getUntrackedFiles()
# go through currentCommit
for file in currentCommit.fileMaps:
# in other branch
currentFileContent = Blob.getContent(currentCommit.fileMaps[file])
if file in givenCommit.fileMaps:
givenFileContent = Blob.getContent(givenCommit.fileMaps[file])
if currentFileContent == givenFileContent:
newFiles[file] = currentCommit.fileMaps[file]
else:
if file in splitPoint.fileMaps:
splitPointFileContent = Blob.getContent(
splitPoint.fileMaps[file])
if splitPointFileContent == currentFileContent:
newFiles[file] = givenCommit.fileMaps[file]
elif splitPointFileContent == givenFileContent:
newFiles[file] = currentCommit.fileMaps[file]
else:
mergeString = Blob.makeMergeString(
currentFileContent, givenFileContent)
merges.append({"file": file, "content": mergeString})
else:
mergeString = Blob.makeMergeString(
currentFileContent, givenFileContent)
merges.append({"file": file, "content": mergeString})
givenCommit.fileMaps.pop(file)
else:
if file not in splitPoint.fileMaps or Blob.getContent(
splitPoint.fileMaps[file]) != currentFileContent:
newFiles[file] = currentCommit.fileMaps[file]
# at this point all files left in given are not in current commit but may
# be untracked
for file in givenCommit.fileMaps:
givenFileContent = Blob.getContent(givenCommit.fileMaps[file])
if file in untracked:
f = open(file)
untrackedContent = f.readlines()
f.close()
if untrackedContent != givenFileContent:
print("There is an untracked file in the way; delete it or add it first")
exit()
if file not in splitPoint.fileMaps or Blob.getContent(
splitPoint.fileMaps[file]) != currentFileContent:
newFiles[file] = givenCommit.fileMaps[file]
# go through remainingCommit
newCommitId = uuid.uuid4().hex
newCommit.createNew(newCommitId,
[currentCommitId,
givenCommitId],
f'Merged {branchName} into {currentBranchName}',
newFiles,
merge=True
)
newCommit.writeCommit()
Branches.updateCurrentCommitId(newCommitId)
for file in newFiles:
Blob.writeFileFromBlob(newFiles[file], file)
for merge in merges:
file = open(merge['file'], 'w')
file.write(merge['content'])
file.close()
Stage.addToStagingArea(merge['file'])
print(f"Merge conflict exists in: {merge['file']}")
print('Merge complete')
if len(args) == 0:
print('no command provided')
exit()
if args[0] == "init":
init()
exit()
if not os.path.exists(".gitlet"):
print("Error: A gitlet directory does not exists, create one with 'gitlet init'")
elif args[0] == "add":
try:
filename = args[1]
except BaseException:
print("add requires an argument")
exit()
add(filename)
elif args[0] == "rm":
try:
filename = args[1]
except BaseException:
print("rm takes an argument")
exit()
rm(filename)
elif args[0] == "commit":
try:
message = args[1]
except BaseException:
message = ''
commit(message)
elif args[0] == "log":
log()
elif args[0] == "branch":
try:
newBranchName = args[1]
except BaseException:
print("branch requires an argument")
exit()
branch(newBranchName)
elif args[0] == "status":
status()
elif args[0] == "checkout":
if len(args) == 4:
commitId = args[1]
fileName = args[3]
checkoutFileByCommitId(fileName, commitId)
elif len(args) >= 1:
if args[1] == "--":
try:
filename = args[2]
except BaseException:
print('checkout does not recognize the provided arguments')
exit()
checkoutFile(filename)
else:
try:
branchName = args[1]
except BaseException:
print('checkout does not recognize the provided arguments')
exit()
checkoutBranch(branchName)
else:
print("checkout does not recognize the provided arguments")
elif args[0] == "reset":
try:
commitId = args[1]
except BaseException:
print("reset takes an argument")
exit()
reset(commitId)
elif args[0] == "merge":
try:
mergeBranch = args[1]
except BaseException:
print("merge takes an argument")
exit()
merge(mergeBranch)
elif args[0] == "branch-rm":
try:
branch = args[1]
except BaseException:
print("branch takes an argument")
exit()
rmBranch(branch)
else:
print("This is not one of the recognized arguments")