-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
575 lines (459 loc) · 21.7 KB
/
gui.py
File metadata and controls
575 lines (459 loc) · 21.7 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#Imported modules required
import tkinter as tk
from tkinter import messagebox, simpledialog
import myvcs
import re
conn = None
#Connection to database
def connect_db():
"""Connect to MySQL database."""
global conn
input_window = tk.Toplevel(root)
input_window.title("Database Connection")
input_window.geometry("300x220")
tk.Label(input_window, text="Enter database name:").pack(pady=5)
db_name_entry = tk.Entry(input_window, width=30)
db_name_entry.pack(pady=5)
tk.Label(input_window, text="Enter database password:").pack(pady=5)
db_password_entry = tk.Entry(input_window, show='*', width=30)
db_password_entry.pack(pady=5)
#Conditions for connection
def on_connect():
"""Handle the connection when the button is pressed."""
global conn
db_name = db_name_entry.get()
db_password = db_password_entry.get()
if not db_name:
messagebox.showerror("Error", "Database name cannot be empty.")
return
conn = myvcs.create_connection("localhost", "root", db_password, db_name)
if conn:
messagebox.showinfo("Success", "Connected to Database")
input_window.destroy()
else:
messagebox.showerror("Error", "Failed to Connect")
connect_button = tk.Button(input_window, text="Connect", command=on_connect)
connect_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Addition of files
def add_file_ui():
"""Add a file to the VCS."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Add File")
input_window.geometry("300x290")
tk.Label(input_window, text="Enter file hash:").pack(pady=5)
file_hash_entry = tk.Entry(input_window, width=30)
file_hash_entry.pack(pady=5)
tk.Label(input_window, text="Enter file content:").pack(pady=5)
content_entry = tk.Text(input_window, width=30, height=5)
content_entry.pack(pady=5)
#Conditions for addition of files
def on_add_file():
"""Handle the addition of the file when the button is pressed."""
file_hash = file_hash_entry.get()
content = content_entry.get("1.0", tk.END).strip()
if not file_hash or not content:
messagebox.showerror("Error", "File hash and content cannot be empty.")
return
try:
success = myvcs.add_file(conn, file_hash, content)
if success:
messagebox.showinfo("Success", "File added successfully!")
input_window.destroy()
else:
messagebox.showerror("Error", "File with this hash already exists.")
except Exception as e:
messagebox.showerror("Error", f"Failed to add file: {e}")
add_button = tk.Button(input_window, text="Add File", command=on_add_file)
add_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Creation of commits
def create_commit_ui():
"""Create a commit in the VCS."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Create Commit")
input_window.geometry("400x450")
tk.Label(input_window, text="Enter commit hash:").pack(pady=5)
commit_hash_entry = tk.Entry(input_window, width=40)
commit_hash_entry.pack(pady=5)
tk.Label(input_window, text="Enter commit message:").pack(pady=5)
message_entry = tk.Text(input_window, width=40, height=5)
message_entry.pack(pady=5)
tk.Label(input_window, text="Enter parent commit (or leave blank):").pack(pady=5)
parent_commit_entry = tk.Entry(input_window, width=40)
parent_commit_entry.pack(pady=5)
tk.Label(input_window, text="Enter branch name:").pack(pady=5)
branch_name_entry = tk.Entry(input_window, width=40)
branch_name_entry.pack(pady=5)
#Conditions for creation of commits
def on_create_commit():
"""Handle the creation of the commit when the button is pressed."""
commit_hash = commit_hash_entry.get()
message = message_entry.get("1.0", tk.END).strip()
parent_commit = parent_commit_entry.get()
branch_name = branch_name_entry.get()
if not commit_hash or not message or not branch_name:
messagebox.showerror("Error", "Commit hash, message, and branch name cannot be empty.")
return
branch_info = myvcs.get_branch_info(conn, branch_name)
if not branch_info:
messagebox.showerror("Error", f"Branch '{branch_name}' does not exist.")
return
try:
success = myvcs.create_commit(conn, commit_hash, message, parent_commit or None, branch_name)
if success:
messagebox.showinfo("Success", "Commit created successfully!")
input_window.destroy()
else:
messagebox.showerror("Error", "Commit with this hash already exists.")
except Exception as e:
messagebox.showerror("Error", f"Failed to create commit: {e}")
create_button = tk.Button(input_window, text="Create Commit", command=on_create_commit)
create_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Creation of branches
def create_branch_ui():
"""Create a new branch."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Create Branch")
input_window.geometry("400x250")
tk.Label(input_window, text="Enter branch name:").pack(pady=5)
branch_name_entry = tk.Entry(input_window, width=40)
branch_name_entry.pack(pady=5)
tk.Label(input_window, text="Enter latest commit hash (or leave blank):").pack(pady=5)
latest_commit_entry = tk.Entry(input_window, width=40)
latest_commit_entry.pack(pady=5)
#Conditions for creation of branches
def on_create_branch():
"""Handle the creation of the branch when the button is pressed."""
branch_name = branch_name_entry.get()
latest_commit = latest_commit_entry.get().strip()
if not branch_name:
messagebox.showerror("Error", "Branch name cannot be empty.")
return
existing_branch_info = myvcs.get_branch_info(conn, branch_name)
if existing_branch_info:
messagebox.showerror("Error", f"Branch '{branch_name}' already exists.")
return
if latest_commit:
commit_info = myvcs.get_commit_info(conn, latest_commit)
if not commit_info:
messagebox.showerror("Error", f"Latest commit '{latest_commit}' does not exist.")
return
try:
success = myvcs.create_branch(conn, branch_name, latest_commit)
if success:
messagebox.showinfo("Success", "Branch created successfully!")
input_window.destroy()
else:
messagebox.showerror("Error", "Failed to create branch.")
except Exception as e:
messagebox.showerror("Error", f"Failed to create branch: {e}")
create_button = tk.Button(input_window, text="Create Branch", command=on_create_branch)
create_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Retrieval of file by hash
def get_file_by_hash_ui():
"""Retrieve a file from the VCS by its hash and display it in a new window."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Retrieve File by Hash")
input_window.geometry("400x150")
tk.Label(input_window, text="Enter file hash:").pack(pady=5)
file_hash_entry = tk.Entry(input_window, width=40)
file_hash_entry.pack(pady=5)
#Conditions for file by hash fetching
def on_retrieve_file():
"""Handle the retrieval of the file when the button is pressed."""
file_hash = file_hash_entry.get().strip()
if not file_hash:
messagebox.showerror("Error", "File hash cannot be empty.")
return
try:
content = myvcs.get_file_by_hash(conn, file_hash)
if content:
file_content = content[1]
file_window = tk.Toplevel(root)
file_window.title(f"File Content: {file_hash}")
file_window.geometry("600x400")
text_widget = tk.Text(file_window, wrap=tk.WORD)
text_widget.insert(tk.END, file_content)
text_widget.pack(expand=True, fill=tk.BOTH)
text_widget.config(state=tk.DISABLED)
else:
messagebox.showerror("Error", "File not found")
except Exception as e:
messagebox.showerror("Error", f"Failed to retrieve file: {e}")
retrieve_button = tk.Button(input_window, text="Retrieve File", command=on_retrieve_file)
retrieve_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Retrieval of commit history
def get_commit_history_ui():
"""Get commit history of a branch and display it in a pop-up window."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Get Commit History")
input_window.geometry("400x150")
tk.Label(input_window, text="Enter branch name:").pack(pady=5)
branch_name_entry = tk.Entry(input_window, width=40)
branch_name_entry.pack(pady=5)
#Conditions for commit history retrieval
def on_get_commit_history():
"""Handle the retrieval of commit history when the button is pressed."""
branch_name = branch_name_entry.get().strip()
if not branch_name:
messagebox.showerror("Error", "Branch name cannot be empty.")
return
try:
history = myvcs.get_commit_history(conn, branch_name)
if history:
history_str = "\n".join(f"Commit Hash: {commit[0]}, Message: {commit[1]}, Timestamp: {commit[2]}" for commit in history)
history_window = tk.Toplevel(root)
history_window.title(f"Commit History: {branch_name}")
history_window.geometry("600x400")
text_widget = tk.Text(history_window, wrap=tk.WORD)
text_widget.insert(tk.END, history_str)
text_widget.pack(expand=True, fill=tk.BOTH)
text_widget.config(state=tk.DISABLED)
else:
messagebox.showinfo("Commit History", "No commits found for this branch")
except Exception as e:
messagebox.showerror("Error", f"Failed to get commit history: {e}")
get_history_button = tk.Button(input_window, text="Get Commit History", command=on_get_commit_history)
get_history_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Retrieval of branch info
def get_branch_info_ui():
"""Get details about a branch and display it in a pop-up window."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Get Branch Info")
input_window.geometry("400x150")
tk.Label(input_window, text="Enter branch name:").pack(pady=5)
branch_name_entry = tk.Entry(input_window, width=40)
branch_name_entry.pack(pady=5)
#Conditions for retrieval of branch info
def on_get_branch_info():
"""Handle the retrieval of branch info when the button is pressed."""
branch_name = branch_name_entry.get().strip()
if not branch_name:
messagebox.showerror("Error", "Branch name cannot be empty.")
return
try:
info = myvcs.get_branch_info(conn, branch_name)
if info:
info_str = f"Branch Name: {info[0]}\nLatest Commit: {info[1]}"
info_window = tk.Toplevel(root)
info_window.title(f"Branch Info: {branch_name}")
info_window.geometry("500x300")
text_widget = tk.Text(info_window, wrap=tk.WORD)
text_widget.insert(tk.END, info_str)
text_widget.pack(expand=True, fill=tk.BOTH)
text_widget.config(state=tk.DISABLED)
else:
messagebox.showinfo("Branch Info", "No branch found with that name")
except Exception as e:
messagebox.showerror("Error", f"Failed to get branch info: {e}")
get_info_button = tk.Button(input_window, text="Get Branch Info", command=on_get_branch_info)
get_info_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Deletion of files
def delete_file_ui():
"""Delete a file from the VCS."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Delete File")
input_window.geometry("400x150")
tk.Label(input_window, text="Enter file hash to delete:").pack(pady=5)
file_hash_entry = tk.Entry(input_window, width=40)
file_hash_entry.pack(pady=5)
#Conditions for deletion of files
def on_delete_file():
"""Handle the deletion of the file when the button is pressed."""
file_hash = file_hash_entry.get().strip()
if not file_hash:
messagebox.showerror("Error", "File hash cannot be empty.")
return
try:
success = myvcs.delete_file(conn, file_hash)
if success:
messagebox.showinfo("Success", "File deleted successfully!")
input_window.destroy()
else:
messagebox.showerror("Error", "File not found.")
except Exception as e:
messagebox.showerror("Error", f"Failed to delete file: {e}")
delete_button = tk.Button(input_window, text="Delete File", command=on_delete_file)
delete_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Deletion of commits
def delete_commit_ui():
"""Delete a commit from the VCS."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Delete Commit")
input_window.geometry("400x150")
tk.Label(input_window, text="Enter commit hash to delete:").pack(pady=5)
commit_hash_entry = tk.Entry(input_window, width=40)
commit_hash_entry.pack(pady=5)
#Conditions for deletion of commits
def on_delete_commit():
"""Handle the deletion of the commit when the button is pressed."""
commit_hash = commit_hash_entry.get().strip()
if not commit_hash:
messagebox.showerror("Error", "Commit hash cannot be empty.")
return
try:
success = myvcs.delete_commit(conn, commit_hash)
if success:
messagebox.showinfo("Success", "Commit deleted successfully!")
input_window.destroy()
else:
messagebox.showerror("Error", "Commit not found.")
except Exception as e:
messagebox.showerror("Error", f"Failed to delete commit: {e}")
delete_button = tk.Button(input_window, text="Delete Commit", command=on_delete_commit)
delete_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Deletion of branches
def delete_branch_ui():
"""Delete a branch from the VCS."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Delete Branch")
input_window.geometry("400x150")
tk.Label(input_window, text="Enter branch name to delete:").pack(pady=5)
branch_name_entry = tk.Entry(input_window, width=40)
branch_name_entry.pack(pady=5)
#Conditions for deletions of branches
def on_delete_branch():
"""Handle the deletion of the branch when the button is pressed."""
branch_name = branch_name_entry.get().strip()
if not branch_name:
messagebox.showerror("Error", "Branch name cannot be empty.")
return
try:
success = myvcs.delete_branch(conn, branch_name)
if success:
messagebox.showinfo("Success", f"Branch '{branch_name}' deleted successfully!")
input_window.destroy()
else:
messagebox.showerror("Error", f"Branch '{branch_name}' not found or has associated commits.")
except Exception as e:
messagebox.showerror("Error", f"Failed to delete branch: {e}")
delete_button = tk.Button(input_window, text="Delete Branch", command=on_delete_branch)
delete_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Checking for valid branch names
def is_valid_branch_name(branch_name):
"""Check if the branch name is valid (letters, digits, underscores, and hyphens)."""
return re.match(r'^[a-zA-Z0-9_-]+$', branch_name) is not None
#Merging of branches
def merge_branches_ui():
"""Merge two branches in the VCS after validating their existence."""
if conn is None:
messagebox.showerror("Error", "No database connection")
return
input_window = tk.Toplevel(root)
input_window.title("Merge Branches")
input_window.geometry("400x200")
tk.Label(input_window, text="Enter source branch name:").pack(pady=5)
source_branch_entry = tk.Entry(input_window, width=40)
source_branch_entry.pack(pady=5)
tk.Label(input_window, text="Enter target branch name:").pack(pady=5)
target_branch_entry = tk.Entry(input_window, width=40)
target_branch_entry.pack(pady=5)
#Conditions for merging of branches
def on_merge_branches():
"""Handle the merging of branches when the button is pressed."""
source_branch = source_branch_entry.get().strip()
target_branch = target_branch_entry.get().strip()
if not source_branch or not target_branch:
messagebox.showerror("Error", "Both source and target branch names cannot be empty.")
return
if not is_valid_branch_name(source_branch) or not is_valid_branch_name(target_branch):
messagebox.showerror("Error", "Branch names must be valid identifiers (letters, digits, underscores, and hyphens).")
return
try:
source_info = myvcs.get_branch_info(conn, source_branch)
target_info = myvcs.get_branch_info(conn, target_branch)
if not source_info:
messagebox.showerror("Error", f"Source branch '{source_branch}' not found")
return
if not target_info:
messagebox.showerror("Error", f"Target branch '{target_branch}' not found")
return
merge_success = myvcs.merge_branches(conn, source_branch, target_branch)
if merge_success:
messagebox.showinfo("Success", "Branches merged successfully!")
source_branch_entry.delete(0, tk.END)
target_branch_entry.delete(0, tk.END)
else:
messagebox.showwarning("Warning", "Merge resulted in conflicts. Please resolve them manually.")
input_window.destroy()
except Exception as e:
messagebox.showerror("Error", f"Failed to merge branches: {str(e)}")
merge_button = tk.Button(input_window, text="Merge Branches", command=on_merge_branches)
merge_button.pack(pady=10)
cancel_button = tk.Button(input_window, text="Cancel", command=input_window.destroy)
cancel_button.pack(pady=5)
#Exiting the application
def close_app():
"""Close the application."""
if conn:
conn.close()
root.quit()
#Defining the window size
root = tk.Tk()
root.title("MyVCS GUI")
root.geometry("400x500")
root.resizable(True, True)
#Defining the frame for the app
frame = tk.Frame(root, padx=20, pady=10)
frame.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
#Buttons for all the options which can be used
tk.Button(frame, text="Connect to Database", command=connect_db).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Add File", command=add_file_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Create Commit", command=create_commit_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Create Branch", command=create_branch_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Get File by Hash", command=get_file_by_hash_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Get Commit History", command=get_commit_history_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Get Branch Info", command=get_branch_info_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Merge Branches", command=merge_branches_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Delete File", command=delete_file_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Delete Commit", command=delete_commit_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Delete Branch", command=delete_branch_ui).pack(fill=tk.X, pady=5)
tk.Button(frame, text="Exit", command=close_app).pack(fill=tk.X, pady=5)
root.mainloop()
#Ending the code