-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_user_manager.py
416 lines (333 loc) · 18.9 KB
/
git_user_manager.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
import customtkinter as ctk
import tkinter as tk
from tkinter import messagebox
import os
import subprocess
import keyring
from keyrings.alt.file import PlaintextKeyring
import sys
import threading
SSH_ENABLED = False
# Set the keyring to PlaintextKeyring
keyring.set_keyring(PlaintextKeyring())
class GitManagerApp(ctk.CTk):
def __init__(self):
super().__init__()
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("green")
self.title("Git Manager")
self.geometry("800x600")
self.main_frame = ctk.CTkFrame(self)
self.main_frame.pack(fill="both", expand=True, padx=20, pady=20)
self.label = ctk.CTkLabel(self.main_frame, text="Git Manager", font=("Arial", 24))
self.label.pack(pady=20)
# Menu Section
self.menu_frame = ctk.CTkFrame(self.main_frame)
self.menu_frame.pack(fill="x", pady=10)
self.add_repo_button = ctk.CTkButton(self.menu_frame, text="Add Repository", command=self.open_add_repo_window)
self.add_repo_button.pack(side="left", padx=2)
self.edit_repo_button = ctk.CTkButton(self.menu_frame, text="Edit Repository", command=self.open_edit_repo_window)
self.edit_repo_button.pack(side="left", padx=2)
self.settings_button = ctk.CTkButton(self.menu_frame, text="Settings", command=self.open_settings_window)
self.settings_button.pack(side="left", padx=2)
self.init_button = ctk.CTkButton(self.menu_frame, text="Initialize", command=self.git_init_and_pull)
self.init_button.pack(side="left", padx=2)
self.sync_button = ctk.CTkButton(self.menu_frame, text="Sync", command=self.git_sync)
self.sync_button.pack(side="left", padx=2)
self.delete_button = ctk.CTkButton(self.menu_frame, text="Delete Selected", command=self.delete_selected_repos)
self.delete_button.pack(side="left", padx=2)
# Repository List Section
self.listbox_frame = ctk.CTkFrame(self.main_frame)
self.listbox_frame.pack(fill="both", expand=True)
self.repo_listbox = tk.Listbox(self.listbox_frame, selectmode=tk.SINGLE, font=("Arial", 12), bg="#2b2b2b", fg="white", selectbackground="#4a4a4a", selectforeground="white")
self.repo_listbox.pack(side="left", fill="both", expand=True)
self.scrollbar = ctk.CTkScrollbar(self.listbox_frame, orientation="vertical", command=self.repo_listbox.yview)
self.scrollbar.pack(side="right", fill="y")
self.repo_listbox.configure(yscrollcommand=self.scrollbar.set)
# Terminal Section
self.terminal_frame = ctk.CTkFrame(self.main_frame)
self.terminal_frame.pack(fill="both", expand=True)
self.terminal_label = ctk.CTkLabel(self.terminal_frame, text="Terminal Output", font=("Arial", 12))
self.terminal_label.pack(pady=5)
self.terminal_output = ctk.CTkTextbox(self.terminal_frame, font=("Courier", 12))
self.terminal_output.configure(fg_color="#1e1e1e", text_color="white")
self.terminal_output.pack(fill="both", expand=True)
self.conflict_resolution_var = tk.IntVar(value=1)
self.conflict_resolution_checkbox = ctk.CTkCheckBox(self.main_frame, text="Auto-resolve conflicts by keeping newer and larger files", variable=self.conflict_resolution_var)
self.conflict_resolution_checkbox.pack(pady=5)
self.rebase_var = tk.IntVar(value=0)
self.rebase_checkbox = ctk.CTkCheckBox(self.main_frame, text="Rebase instead of merge for pull", variable=self.rebase_var)
self.rebase_checkbox.pack(pady=5)
self.load_repos()
self.load_settings()
def open_add_repo_window(self):
self.create_repo_window("Add Repository", self.add_repo)
def open_edit_repo_window(self):
selected_repo = self.get_selected_repo_dir()
if not selected_repo:
return
local_dir, remote_url, branch = selected_repo
self.create_repo_window("Edit Repository", self.edit_repo, local_dir, remote_url, branch)
def create_repo_window(self, title, command, local_dir="", remote_url="", branch="main"):
repo_window = ctk.CTkToplevel(self)
repo_window.title(title)
repo_window.geometry("400x600")
dir_label = ctk.CTkLabel(repo_window, text="Local Directory")
dir_label.pack(pady=5)
dir_entry = ctk.CTkEntry(repo_window)
dir_entry.pack(pady=5)
dir_entry.insert(0, local_dir)
repo_label = ctk.CTkLabel(repo_window, text="Remote Repository URL")
repo_label.pack(pady=5)
repo_entry = ctk.CTkEntry(repo_window)
repo_entry.pack(pady=5)
repo_entry.insert(0, remote_url)
branch_label = ctk.CTkLabel(repo_window, text="Branch")
branch_label.pack(pady=5)
branch_entry = ctk.CTkEntry(repo_window)
branch_entry.pack(pady=5)
branch_entry.insert(0, branch)
user_label = ctk.CTkLabel(repo_window, text="Username")
user_label.pack(pady=5)
user_entry = ctk.CTkEntry(repo_window)
user_entry.pack(pady=5)
email_label = ctk.CTkLabel(repo_window, text="Email")
email_label.pack(pady=5)
email_entry = ctk.CTkEntry(repo_window)
email_entry.pack(pady=5)
pass_label = ctk.CTkLabel(repo_window, text="Password")
pass_label.pack(pady=5)
pass_entry = ctk.CTkEntry(repo_window, show="*")
pass_entry.pack(pady=5)
submit_button = ctk.CTkButton(repo_window, text="Submit", command=lambda: command(local_dir, dir_entry.get(), repo_entry.get(), branch_entry.get(), user_entry.get(), email_entry.get(), pass_entry.get(), repo_window))
submit_button.pack(pady=10)
def add_repo(self, old_local_dir, local_dir, remote_url, branch, username, email, password, window):
if local_dir and remote_url and branch and username and email and password:
keyring.set_password(remote_url, username, password)
# Initialize the local repository if it does not exist
if not os.path.exists(os.path.join(local_dir, '.git')):
init_command = ["git", "init"]
subprocess.run(init_command, cwd=local_dir)
# Set user information
self.set_git_user_info(local_dir, username, email)
# Configure the repository with the access credentials
self.configure_git_credentials(local_dir, remote_url, username, password)
# Check if the remote repository already exists
remote_check_command = ["git", "remote", "get-url", "origin"]
result = subprocess.run(remote_check_command, cwd=local_dir, capture_output=True, text=True)
if result.returncode == 0:
# If the remote already exists, update the URL
remote_command = ["git", "remote", "set-url", "origin", remote_url]
else:
# If the remote does not exist, add the remote repository URL
remote_command = ["git", "remote", "add", "origin", remote_url]
result = subprocess.run(remote_command, cwd=local_dir, capture_output=True, text=True)
if result.returncode != 0:
messagebox.showerror("Error", f"Failed to add or update remote URL:\n{result.stderr}")
return
self.repo_listbox.insert("end", f"{local_dir} -> {remote_url} [{branch}]")
self.save_repo(local_dir, remote_url, branch, username, email)
window.destroy()
else:
messagebox.showerror("Error", "All fields must be filled out")
def edit_repo(self, old_local_dir, local_dir, remote_url, branch, username, email, password, window):
if local_dir and remote_url and branch and username and email and password:
# Update the repository entry in the listbox and the file
for i in range(self.repo_listbox.size()):
entry = self.repo_listbox.get(i)
if old_local_dir in entry:
self.repo_listbox.delete(i)
self.repo_listbox.insert(i, f"{local_dir} -> {remote_url} [{branch}]")
break
# Update the keyring with new credentials
keyring.set_password(remote_url, username, password)
# Set user information
self.set_git_user_info(local_dir, username, email)
# Configure the repository with the new access credentials
self.configure_git_credentials(local_dir, remote_url, username, password)
# Update the remote repository URL
remote_command = ["git", "remote", "set-url", "origin", remote_url]
result = subprocess.run(remote_command, cwd=local_dir, capture_output=True, text=True)
if result.returncode != 0:
messagebox.showerror("Error", f"Failed to update remote URL:\n{result.stderr}")
return
self.save_repos_to_file()
window.destroy()
else:
messagebox.showerror("Error", "All fields must be filled out")
def set_git_user_info(self, local_dir, username, email):
user_name_command = ["git", "config", "user.name", username]
user_email_command = ["git", "config", "user.email", email]
subprocess.run(user_name_command, cwd=local_dir)
subprocess.run(user_email_command, cwd=local_dir)
def configure_git_credentials(self, local_dir, remote_url, username, password):
# Use credential helper to store credentials
with open(os.path.join(local_dir, ".git", "config"), "a") as git_config:
git_config.write(f"""
[credential]
helper = store
[url "{remote_url}"]
insteadOf = {remote_url}
""")
# Store the credentials in the git credential store
creds_command = ["git", "credential", "approve"]
creds_input = f"url={remote_url.replace('https://', 'https://'+username+':'+password+'@')}\n"
subprocess.run(creds_command, cwd=local_dir, input=creds_input.encode())
def save_repo(self, local_dir, remote_url, branch, username, email):
with open("repos.txt", "a") as file:
file.write(f"{local_dir},{remote_url},{branch},{username},{email}\n")
def load_repos(self):
if os.path.exists("repos.txt"):
with open("repos.txt", "r") as file:
for line in file:
parts = line.strip().split(",")
if len(parts) == 5:
local_dir, remote_url, branch, username, email = parts
self.repo_listbox.insert("end", f"{local_dir} -> {remote_url} [{branch}]")
else:
messagebox.showerror("Error", f"Invalid line in repos.txt: {line}")
def delete_selected_repos(self):
selected_indices = self.repo_listbox.curselection()
for index in reversed(selected_indices):
self.repo_listbox.delete(index)
self.save_repos_to_file()
def save_repos_to_file(self):
with open("repos.txt", "w") as file:
for i in range(self.repo_listbox.size()):
line = self.repo_listbox.get(i)
local_dir, rest = line.split(" -> ")
remote_url, branch = rest.strip().split(" [")
branch = branch[:-1]
username, email = "", ""
file.write(f"{local_dir},{remote_url},{branch},{username},{email}\n")
def open_settings_window(self):
settings_window = ctk.CTkToplevel(self)
settings_window.title("Settings")
settings_window.geometry("400x400")
settings_label = ctk.CTkLabel(settings_window, text="Settings", font=("Arial", 18))
settings_label.pack(pady=10)
# Add proxy settings
proxy_label = ctk.CTkLabel(settings_window, text="Proxy URL")
proxy_label.pack(pady=5)
proxy_entry = ctk.CTkEntry(settings_window)
proxy_entry.pack(pady=5)
# Add theme settings
theme_label = ctk.CTkLabel(settings_window, text="Theme")
theme_label.pack(pady=5)
theme_menu = ctk.CTkOptionMenu(settings_window, values=["dark", "light"], command=self.set_theme)
theme_menu.pack(pady=5)
# Add SSH Key Support setting
self.ssh_var = tk.IntVar()
ssh_checkbox = ctk.CTkCheckBox(settings_window, text="Enable SSH Key Support", variable=self.ssh_var, command=self.toggle_ssh_support)
ssh_checkbox.pack(pady=5)
save_button = ctk.CTkButton(settings_window, text="Save", command=lambda: self.save_settings(proxy_entry.get(), settings_window))
save_button.pack(pady=10)
def set_theme(self, choice):
if choice == "light":
ctk.set_appearance_mode(choice)
ctk.set_default_color_theme("blue") # Set a light color theme
else:
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("green")
def toggle_ssh_support(self):
global SSH_ENABLED
if self.ssh_var.get() == 1:
SSH_ENABLED = True
self.install_ssh_dependencies()
else:
SSH_ENABLED = False
def install_ssh_dependencies(self):
def install():
result = subprocess.run([sys.executable, '-m', 'pip', 'install', 'paramiko'], capture_output=True, text=True)
if result.returncode == 0:
messagebox.showinfo("SSH Key Support", "Dependencies installed. Please restart the application.")
else:
messagebox.showerror("SSH Key Support", f"Failed to install dependencies. Error: {result.stderr}")
threading.Thread(target=install).start()
def save_settings(self, proxy_url, window):
with open("settings.txt", "w") as file:
file.write(f"proxy={proxy_url}\n")
file.write(f"ssh={self.ssh_var.get()}\n")
window.destroy()
def load_settings(self):
if os.path.exists("settings.txt"):
with open("settings.txt", "r") as file:
for line in file:
if line.startswith("proxy="):
proxy_url = line.strip().split("=")[1]
os.environ["HTTP_PROXY"] = proxy_url
os.environ["HTTPS_PROXY"] = proxy_url
if line.startswith("ssh="):
ssh_value = line.strip().split("=")[1]
if ssh_value == '1':
global SSH_ENABLED
SSH_ENABLED = True
self.ssh_var.set(1)
def get_selected_repo_dir(self):
selected_indices = self.repo_listbox.curselection()
if not selected_indices:
messagebox.showerror("Error", "Please select a repository")
return None
selected_line = self.repo_listbox.get(selected_indices[0])
local_dir, rest = selected_line.split(" -> ")
remote_url, branch = rest.strip().split(" [")
branch = branch[:-1]
return local_dir.strip(), remote_url.strip(), branch.strip()
def git_init_and_pull(self):
local_dir, remote_url, branch = self.get_selected_repo_dir()
if local_dir:
# Initialize if not already initialized
if not os.path.exists(os.path.join(local_dir, '.git')):
self.run_git_command("git init", local_dir)
self.run_git_command(f"git remote add origin {remote_url}", local_dir)
self.run_git_command(f"git pull origin {branch}", local_dir)
def git_sync(self):
local_dir, remote_url, branch = self.get_selected_repo_dir()
if local_dir:
self.run_git_command("git add .", local_dir)
self.run_git_command("git commit -m 'Sync changes'", local_dir)
self.handle_git_errors(f"git pull origin {branch}", local_dir)
self.handle_git_errors(f"git push origin {branch}", local_dir)
def handle_git_errors(self, command, repo_path):
result = subprocess.run(command, cwd=repo_path, capture_output=True, text=True, shell=True)
if result.returncode == 0:
self.terminal_output.insert("end", f"Output:\n{result.stdout}\n")
else:
self.terminal_output.insert("end", f"Error:\n{result.stderr}\n")
self.terminal_output.see("end")
self.resolve_git_errors(result.stderr, repo_path)
def resolve_git_errors(self, error_message, repo_path):
if "fatal: cannot lock ref 'HEAD'" in error_message or "fatal: kann 'HEAD' nicht sperren" in error_message:
self.run_git_command("git reset --hard", repo_path)
elif "error: failed to push some refs" in error_message or "error: Fehler beim Versenden einiger Referenzen" in error_message:
self.run_git_command("git pull --rebase", repo_path)
elif "fatal: You have divergent branches and need to specify how to reconcile them" in error_message or "fatal: Es muss angegeben werden, wie mit abweichenden Branches umgegangen werden soll" in error_message:
if self.rebase_var.get() == 1:
self.run_git_command("git config pull.rebase true", repo_path)
else:
self.run_git_command("git config pull.rebase false", repo_path)
self.run_git_command(f"git pull origin {self.get_selected_repo_dir()[2]}", repo_path)
elif "fatal: refusing to merge unrelated histories" in error_message or "fatal: verweigere den Merge von nicht zusammenhängenden Historien" in error_message:
self.run_git_command(f"git pull origin {self.get_selected_repo_dir()[2]} --allow-unrelated-histories", repo_path)
elif "error: src refspec main does not match any" in error_message or "error: Src-Refspec main entspricht keiner Referenz" in error_message:
self.run_git_command(f"git push origin HEAD:{self.get_selected_repo_dir()[2]}", repo_path)
elif "Merge conflict" in error_message or "CONFLICT" in error_message:
self.resolve_merge_conflicts(repo_path)
elif "There is no tracking information for the current branch" in error_message or "Es gibt keine Tracking-Informationen für den aktuellen Branch" in error_message:
self.run_git_command(f"git branch --set-upstream-to=origin/{self.get_selected_repo_dir()[2]} master", repo_path)
self.run_git_command(f"git pull origin {self.get_selected_repo_dir()[2]}", repo_path)
def resolve_merge_conflicts(self, repo_path):
if self.conflict_resolution_var.get() == 1:
self.run_git_command("git merge --strategy-option=theirs", repo_path)
else:
self.run_git_command("git merge --strategy-option=ours", repo_path)
def run_git_command(self, command, repo_path):
result = subprocess.run(command, cwd=repo_path, capture_output=True, text=True, shell=True)
if result.returncode == 0:
self.terminal_output.insert("end", f"Output:\n{result.stdout}\n")
else:
self.terminal_output.insert("end", f"Error:\n{result.stderr}\n")
self.terminal_output.see("end")
if __name__ == "__main__":
app = GitManagerApp()
app.mainloop()