-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwizard_steps.py
More file actions
728 lines (635 loc) · 30.1 KB
/
wizard_steps.py
File metadata and controls
728 lines (635 loc) · 30.1 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
"""
Wizard Steps Functions Module
This module contains all the setup wizard step functions that were previously in Ogresync.py
to reduce the main file's complexity.
These functions handle:
- Finding Obsidian installation paths
- Vault directory selection
- Git installation checking
- SSH connection testing and key generation
- Initial repository setup and commits
"""
import os
import sys
import shutil
import subprocess
import threading
import time
import platform
import webbrowser
import pyperclip
from typing import Optional
# Dependency injection pattern - these will be set by the main module
_ui_elements = None
_config_data = None
_save_config_func = None
_safe_update_log_func = None
_run_command_func = None
def set_dependencies(ui_elements=None, config_data=None, save_config_func=None,
safe_update_log_func=None, run_command_func=None):
"""Set the dependencies from the main module"""
global _ui_elements, _config_data, _save_config_func, _safe_update_log_func, _run_command_func
_ui_elements = ui_elements
_config_data = config_data
_save_config_func = save_config_func
_safe_update_log_func = safe_update_log_func
_run_command_func = run_command_func
def safe_update_log(message, progress=None):
"""Log function that uses the injected dependency or falls back to print"""
if _safe_update_log_func:
_safe_update_log_func(message, progress)
else:
print(f"LOG: {message}")
def run_command(command, cwd=None, timeout=None):
"""Command execution function that uses the injected dependency or falls back to subprocess"""
if _run_command_func:
return _run_command_func(command, cwd, timeout)
else:
# Fallback implementation
try:
result = subprocess.run(
command,
cwd=cwd,
shell=True,
capture_output=True,
text=True,
timeout=timeout
)
return result.stdout.strip(), result.stderr.strip(), result.returncode
except subprocess.TimeoutExpired as e:
return "", str(e), 1
except Exception as e:
return "", str(e), 1
# ------------------------------------------------
# WIZARD STEPS FUNCTIONS
# ------------------------------------------------
def find_obsidian_path():
"""
Attempts to locate Obsidian's installation or launch command based on the OS.
Windows:
- Checks common installation directories for "Obsidian.exe".
Linux:
- Checks if 'obsidian' is in PATH.
- Checks common Flatpak paths.
- Checks common Snap path.
- As a fallback, returns the Flatpak command string.
macOS:
- Checks the default /Applications folder.
- Checks PATH.
If not found, it prompts the user to manually locate the executable.
Returns the path or command string to launch Obsidian, or None.
"""
ui_elements = _ui_elements
if sys.platform.startswith("win"):
possible_paths = [
os.path.expandvars(r"%LOCALAPPDATA%\Programs\Obsidian\Obsidian.exe"),
os.path.expandvars(r"%PROGRAMFILES%\Obsidian\Obsidian.exe"),
os.path.expandvars(r"%PROGRAMFILES(X86)%\Obsidian\Obsidian.exe")
]
for path in possible_paths:
if os.path.exists(path):
return path
if ui_elements:
response = ui_elements.ask_yes_no("Obsidian Not Found",
"Obsidian was not detected in standard locations.\n"
"Would you like to locate the Obsidian executable manually?")
if response:
selected_path = ui_elements.ask_file_dialog(
"Select Obsidian Executable",
[("Obsidian Executable", "*.exe")]
)
if selected_path:
return selected_path
else:
# User chose not to locate manually - offer download guidance
download_response = ui_elements.ask_yes_no(
"Download Obsidian",
"Obsidian is required for Ogresync to work.\n\n"
"Would you like to go to the Obsidian download page now?\n"
"After installation, you can restart the setup wizard."
)
if download_response:
import webbrowser
webbrowser.open("https://obsidian.md/download")
ui_elements.show_info_message(
"Download Started",
"The Obsidian download page has been opened in your browser.\n\n"
"Please install Obsidian and restart Ogresync to continue setup."
)
return None
elif sys.platform.startswith("linux"):
# Option 1: Check if 'obsidian' is in PATH.
obsidian_cmd = shutil.which("obsidian")
if obsidian_cmd:
return obsidian_cmd
# Option 2: Check common Flatpak paths.
flatpak_paths = [
os.path.expanduser("~/.local/share/flatpak/exports/bin/obsidian"),
"/var/lib/flatpak/exports/bin/obsidian"
]
for path in flatpak_paths:
if os.path.exists(path):
return path
# Option 3: Check Snap installation.
snap_path = "/snap/bin/obsidian"
if os.path.exists(snap_path):
return snap_path
# Option 4: Fallback to a command string.
return "flatpak run md.obsidian.Obsidian"
elif sys.platform.startswith("darwin"):
# macOS: Check default location in /Applications.
obsidian_app = "/Applications/Obsidian.app/Contents/MacOS/Obsidian"
if os.path.exists(obsidian_app):
return obsidian_app
# Option 2: Check if a command is available in PATH.
obsidian_cmd = shutil.which("obsidian")
if obsidian_cmd:
return obsidian_cmd
if ui_elements:
response = ui_elements.ask_yes_no("Obsidian Not Found",
"Obsidian was not detected in standard locations.\n"
"Would you like to locate the Obsidian application manually?")
if response:
selected_path = ui_elements.ask_file_dialog(
"Select Obsidian Application",
filetypes=[("Obsidian Application", "*.app")]
)
if selected_path:
return selected_path
return None
return None
def select_vault_path():
"""
Asks user to select Obsidian Vault folder. Returns path or None if canceled.
"""
ui_elements = _ui_elements
if ui_elements:
selected = ui_elements.ask_directory_dialog("Select Obsidian Vault Folder")
return selected if selected else None
return None
def is_git_installed():
"""
Returns True if Git is installed, else False.
"""
out, err, rc = run_command("git --version")
return rc == 0
def detect_git_path():
"""
Attempts to detect Git installation path with OS-specific fallbacks.
Returns the path to git executable if found, None otherwise.
"""
import platform
# First try to find git in PATH
git_cmd = shutil.which("git")
if git_cmd:
return git_cmd
# OS-specific common installation paths
if platform.system() == "Windows":
common_paths = [
"C:\\Program Files\\Git\\bin\\git.exe",
"C:\\Program Files (x86)\\Git\\bin\\git.exe",
"C:\\Users\\{}\\AppData\\Local\\Programs\\Git\\bin\\git.exe".format(os.getenv('USERNAME', '')),
"C:\\Program Files\\GitHub Desktop\\resources\\app\\git\\cmd\\git.exe",
"C:\\Program Files (x86)\\GitHub Desktop\\resources\\app\\git\\cmd\\git.exe"
]
for path in common_paths:
if os.path.exists(path):
return path
# Check if user wants to locate manually on Windows
if _ui_elements:
response = _ui_elements.ask_yes_no(
"Git Not Found",
"Git was not detected in standard locations.\n\n"
"Would you like to locate the Git executable manually?\n"
"(Look for git.exe in your Git installation folder)"
)
if response:
selected_path = _ui_elements.ask_file_dialog(
"Select Git Executable",
filetypes=[("Git Executable", "git.exe"), ("All Files", "*.*")]
)
if selected_path and os.path.exists(selected_path):
return selected_path
else:
# User chose not to locate manually - offer download guidance
download_response = _ui_elements.ask_yes_no(
"Download Git",
"Git is required for Ogresync to work with version control.\n\n"
"Would you like to go to the Git download page now?\n"
"After installation, you can restart the setup wizard."
)
if download_response:
import webbrowser
webbrowser.open("https://git-scm.com/download/win")
_ui_elements.show_info_message(
"Download Started",
"The Git download page has been opened in your browser.\n\n"
"Please install Git and restart Ogresync to continue setup.\n"
"Tip: Use the default installation options for best compatibility."
)
else:
# User chose not to download either - show final fallback message
_ui_elements.show_info_message(
"Git Required",
"Git is required for Ogresync to function properly.\n\n"
"The setup wizard cannot continue without Git installed.\n\n"
"Please install Git from https://git-scm.com/download/win\n"
"and restart Ogresync to continue."
)
return None
elif platform.system() == "Darwin": # macOS
common_paths = [
"/usr/bin/git",
"/usr/local/bin/git",
"/opt/homebrew/bin/git",
"/Applications/GitHub Desktop.app/Contents/Resources/app/git/bin/git"
]
for path in common_paths:
if os.path.exists(path):
return path
# Check if user wants to locate manually on macOS
if _ui_elements:
response = _ui_elements.ask_yes_no(
"Git Not Found",
"Git was not detected in standard locations.\n\n"
"Would you like to locate the Git executable manually?\n"
"(Usually found in /usr/bin/git or /usr/local/bin/git)"
)
if response:
selected_path = _ui_elements.ask_file_dialog(
"Select Git Executable",
filetypes=[("Git Executable", "git"), ("All Files", "*")]
)
if selected_path and os.path.exists(selected_path):
return selected_path
else:
# User chose not to locate manually - offer download guidance
download_response = _ui_elements.ask_yes_no(
"Install Git",
"Git is required for Ogresync to work with version control.\n\n"
"Would you like to install Git using Xcode Command Line Tools?\n"
"This is the recommended method on macOS."
)
if download_response:
_ui_elements.show_info_message(
"Install Git",
"To install Git on macOS:\n\n"
"1. Open Terminal (Cmd+Space, type 'Terminal')\n"
"2. Run: xcode-select --install\n"
"3. Follow the installation prompts\n"
"4. Restart Ogresync after installation\n\n"
"Alternative: Visit git-scm.com/download/mac for other options."
)
else:
# User chose not to install - show final fallback message
_ui_elements.show_info_message(
"Git Required",
"Git is required for Ogresync to function properly.\n\n"
"The setup wizard cannot continue without Git installed.\n\n"
"Please install Git and restart Ogresync to continue."
)
return None
else: # Linux and others
common_paths = [
"/usr/bin/git",
"/usr/local/bin/git",
"/bin/git"
]
for path in common_paths:
if os.path.exists(path):
return path
# Check if user wants to locate manually on Linux
if _ui_elements:
response = _ui_elements.ask_yes_no(
"Git Not Found",
"Git was not detected in standard locations.\n\n"
"Would you like to locate the Git executable manually?\n"
"(Usually found in /usr/bin/git)"
)
if response:
selected_path = _ui_elements.ask_file_dialog(
"Select Git Executable",
filetypes=[("Git Executable", "git"), ("All Files", "*")]
)
if selected_path and os.path.exists(selected_path):
return selected_path
else:
# User chose not to locate manually - offer installation guidance
download_response = _ui_elements.ask_yes_no(
"Install Git",
"Git is required for Ogresync to work with version control.\n\n"
"Would you like to see installation instructions for your system?"
)
if download_response:
_ui_elements.show_info_message(
"Install Git on Linux",
"To install Git on Linux, use your package manager:\n\n"
"• Ubuntu/Debian: sudo apt update && sudo apt install git\n"
"• Fedora: sudo dnf install git\n"
"• CentOS/RHEL: sudo yum install git\n"
"• Arch Linux: sudo pacman -S git\n"
"• openSUSE: sudo zypper install git\n\n"
"After installation, restart Ogresync to continue setup."
)
else:
# User chose not to view instructions - show final fallback message
_ui_elements.show_info_message(
"Git Required",
"Git is required for Ogresync to function properly.\n\n"
"The setup wizard cannot continue without Git installed.\n\n"
"Please install Git using your system's package manager\n"
"and restart Ogresync to continue."
)
return None
def test_ssh_connection_sync():
"""
Synchronously tests SSH to GitHub. Returns True if OK, False otherwise.
"""
out, err, rc = run_command("ssh -T git@github.com")
print("DEBUG: SSH OUT:", out)
print("DEBUG: SSH ERR:", err)
print("DEBUG: SSH RC:", rc)
if "successfully authenticated" in (out + err).lower():
return True
return False
def re_test_ssh():
"""
Re-tests the SSH connection in a background thread.
If successful, automatically performs an initial commit/push if none exists yet.
"""
config_data = _config_data
def _test_thread():
safe_update_log("Re-testing SSH connection to GitHub...", 35)
ensure_github_known_host() # ensures no prompt for 'yes/no'
if test_ssh_connection_sync():
safe_update_log("SSH connection successful!", 40)
# Perform the initial commit/push if there are no local commits yet
if config_data and config_data.get("VAULT_PATH"):
def on_commit_complete(success, message):
if success:
safe_update_log("✅ Initial setup completed successfully!", 90)
# Mark setup as done
if config_data:
config_data["SETUP_DONE"] = "1"
if _save_config_func:
_save_config_func()
safe_update_log("Setup complete! You can now close this window or start sync.", 100)
else:
safe_update_log(f"❌ Setup completion failed: {message}", 90)
# Use threaded version to prevent UI blocking
perform_initial_commit_and_push_threaded(config_data["VAULT_PATH"], on_commit_complete)
else:
safe_update_log("❌ Vault path not configured", 40)
else:
safe_update_log("SSH connection still failed. Check your GitHub key or generate a new one.", 40)
threading.Thread(target=_test_thread, daemon=True).start()
def ensure_github_known_host():
"""
Adds GitHub's RSA key to known_hosts if not already present.
This prevents the 'Are you sure you want to continue connecting?' prompt.
"""
# Check if GitHub is already in known_hosts
known_hosts_path = os.path.expanduser("~/.ssh/known_hosts")
if os.path.exists(known_hosts_path):
with open(known_hosts_path, "r", encoding="utf-8") as f:
if "github.com" in f.read():
return
safe_update_log("Adding GitHub to known hosts (ssh-keyscan)...", 32)
# Fetch GitHub's RSA key and append to known_hosts
scan_out, scan_err, rc = run_command("ssh-keyscan -t rsa github.com")
if rc == 0 and scan_out:
# Ensure .ssh folder exists
os.makedirs(os.path.expanduser("~/.ssh"), exist_ok=True)
with open(known_hosts_path, "a", encoding="utf-8") as f:
f.write(scan_out + "\n")
else:
# If this fails, we won't block the user; but we warn them.
safe_update_log("Warning: Could not fetch GitHub host key automatically.", 32)
def perform_initial_commit_and_push(vault_path):
"""
Checks if the local repository has any commits.
If not, creates an initial commit and pushes it to the remote 'origin' on the 'main' branch.
"""
out, err, rc = run_command("git rev-parse HEAD", cwd=vault_path)
if rc != 0:
# rc != 0 implies 'git rev-parse HEAD' failed => no commits (unborn branch)
safe_update_log("No local commits detected. Creating initial commit...", 50)
# Stage all files
run_command("git add .", cwd=vault_path)
# Commit
out_commit, err_commit, rc_commit = run_command('git commit -m "Initial commit"', cwd=vault_path)
if rc_commit == 0:
# Check if remote has commits before pushing
ls_out, ls_err, ls_rc = run_command("git ls-remote --heads origin main", cwd=vault_path)
if ls_out.strip():
# Remote main exists, try to pull first
safe_update_log("Remote 'main' branch exists. Pulling before push...", 55)
pull_out, pull_err, pull_rc = run_command("git pull origin main --allow-unrelated-histories", cwd=vault_path)
if pull_rc == 0:
safe_update_log("Successfully merged with remote. Pushing initial commit...", 60)
else:
safe_update_log(f"Pull failed: {pull_err}. Pushing anyway...", 60)
else:
safe_update_log("Remote 'main' branch does not exist. Creating it...", 55)
# Push to main
push_out, push_err, push_rc = run_command("git push -u origin main", cwd=vault_path)
if push_rc == 0:
safe_update_log("Initial commit pushed successfully to GitHub.", 70)
else:
safe_update_log(f"Push failed: {push_err}", 70)
else:
safe_update_log(f"Error committing files: {err_commit}", 60)
else:
# We already have at least one commit in this repo
safe_update_log("Local repository already has commits. Skipping initial commit step.", 50)
def generate_ssh_key():
"""
Prompts for the user's email and starts a background thread for SSH key generation.
"""
ui_elements = _ui_elements
if not ui_elements:
print("UI elements not available for SSH key generation")
return
user_email = ui_elements.ask_string_dialog(
"SSH Key Generation",
"Enter your email address for the SSH key:",
icon=getattr(ui_elements.Icons, 'GEAR', None) if hasattr(ui_elements, 'Icons') else None
)
if not user_email:
ui_elements.show_error_message("Email Required", "No email address provided. SSH key generation canceled.")
return
threading.Thread(target=generate_ssh_key_async, args=(user_email,), daemon=True).start()
def generate_ssh_key_async(user_email):
"""
Runs in a background thread to:
1) Generate an SSH key if it doesn't already exist.
2) Copy the public key to the clipboard.
3) Show an info dialog on the main thread.
4) After the user closes the dialog, open GitHub's SSH settings in the browser.
"""
ui_elements = _ui_elements
# Cross-platform SSH key paths
ssh_dir = os.path.expanduser(os.path.join("~", ".ssh"))
SSH_KEY_PATH = os.path.join(ssh_dir, "id_rsa.pub")
key_path_private = os.path.join(ssh_dir, "id_rsa")
# Ensure .ssh directory exists with proper permissions
if not os.path.exists(ssh_dir):
try:
os.makedirs(ssh_dir, mode=0o700)
safe_update_log("Created .ssh directory", 20)
except Exception as e:
safe_update_log(f"Failed to create .ssh directory: {e}", 25)
return
# 1) Generate key if it doesn't exist
if not os.path.exists(SSH_KEY_PATH):
safe_update_log("Generating SSH key...", 25)
# Cross-platform SSH key generation command
if platform.system() == "Windows":
# On Windows, ensure proper quote handling
ssh_cmd = f'ssh-keygen -t rsa -b 4096 -C "{user_email}" -f "{key_path_private}" -N ""'
else:
# On Unix-like systems, use proper escaping
ssh_cmd = f"ssh-keygen -t rsa -b 4096 -C '{user_email}' -f '{key_path_private}' -N ''"
out, err, rc = run_command(ssh_cmd)
if rc != 0:
safe_update_log(f"SSH key generation failed: {err}", 25)
return
safe_update_log("SSH key generated successfully.", 30)
else:
safe_update_log("SSH key already exists. Overwriting is not performed here.", 30)
# 2) Read the public key and attempt to copy to the clipboard
try:
with open(SSH_KEY_PATH, "r", encoding="utf-8") as key_file:
public_key = key_file.read().strip()
pyperclip.copy(public_key)
# Verify that clipboard contains the expected key
copied = pyperclip.paste().strip()
if copied != public_key:
raise Exception("Clipboard copy failed: content mismatch.")
safe_update_log("Public key successfully copied to clipboard.", 35)
except Exception as e:
safe_update_log(f"Error copying SSH key to clipboard: {e}", 35)
# 3) Fallback: show a dialog with manual instructions and the public key
if ui_elements:
ui_elements.show_info_message(
"Manual SSH Key Copy Required",
"Automatic copying of your SSH key failed.\n\n"
"Please open a terminal and run:\n\n"
" cat ~/.ssh/id_rsa.pub\n\n"
"Then copy the output manually and add it to your GitHub account."
)
# 4) Show final info dialog and open GitHub's SSH keys page
def show_dialog_then_open_browser():
if ui_elements:
ui_elements.show_info_message(
"SSH Key Generated",
"Your SSH key has been generated and copied to the clipboard (if successful).\n\n"
"If automatic copying failed, please manually copy the key as described.\n\n"
"Click OK to open GitHub's SSH keys page to add your key."
)
webbrowser.open("https://github.com/settings/keys")
# We need to get the root window from somewhere - this might need adjustment
# For now, we'll just call it directly
try:
show_dialog_then_open_browser()
except Exception as e:
safe_update_log(f"Error showing dialog: {e}", 35)
def copy_ssh_key():
"""
Copies the SSH key to clipboard and opens GitHub SSH settings.
"""
ui_elements = _ui_elements
SSH_KEY_PATH = os.path.expanduser(os.path.join("~", ".ssh", "id_rsa.pub"))
if os.path.exists(SSH_KEY_PATH):
with open(SSH_KEY_PATH, "r", encoding="utf-8") as key_file:
ssh_key = key_file.read().strip()
pyperclip.copy(ssh_key)
webbrowser.open("https://github.com/settings/keys")
if ui_elements:
ui_elements.show_info_message("SSH Key Copied",
"Your SSH key has been copied to the clipboard.\n"
"Paste it into GitHub.")
else:
if ui_elements:
ui_elements.show_error_message("Error", "No SSH key found. Generate one first.")
def run_command_threaded(command, cwd=None, timeout=None, progress_callback=None, completion_callback=None):
"""
Runs a command in a background thread to prevent UI blocking.
Args:
command: Command to execute
cwd: Working directory
timeout: Command timeout
progress_callback: Function to call with progress updates (message, progress)
completion_callback: Function to call when complete (stdout, stderr, returncode)
"""
def _command_thread():
try:
if progress_callback:
progress_callback(f"Executing: {command[:50]}...", None)
result = run_command(command, cwd, timeout)
if completion_callback:
completion_callback(*result)
except Exception as e:
if completion_callback:
completion_callback("", str(e), 1)
thread = threading.Thread(target=_command_thread, daemon=True)
thread.start()
return thread
def perform_initial_commit_and_push_threaded(vault_path, completion_callback=None):
"""
Threaded version of perform_initial_commit_and_push to prevent UI blocking.
"""
def _commit_push_thread():
try:
safe_update_log("Checking for existing commits...", 45)
out, err, rc = run_command("git rev-parse HEAD", cwd=vault_path)
if rc != 0:
# No commits exist, create initial commit
safe_update_log("No local commits detected. Creating initial commit...", 50)
# Stage all files
safe_update_log("Staging files...", 52)
run_command("git add .", cwd=vault_path)
# Commit
safe_update_log("Creating initial commit...", 55)
out_commit, err_commit, rc_commit = run_command('git commit -m "Initial commit"', cwd=vault_path)
if rc_commit == 0:
# Check if remote has commits before pushing
safe_update_log("Checking remote repository...", 60)
ls_out, ls_err, ls_rc = run_command("git ls-remote --heads origin main", cwd=vault_path)
if ls_out.strip():
# Remote main exists, try to pull first
safe_update_log("Remote 'main' branch exists. Pulling before push...", 65)
pull_out, pull_err, pull_rc = run_command("git pull origin main --allow-unrelated-histories", cwd=vault_path)
if pull_rc == 0:
safe_update_log("Successfully merged with remote.", 70)
else:
safe_update_log(f"Pull failed: {pull_err}. Continuing...", 70)
else:
safe_update_log("Remote 'main' branch does not exist. Creating it...", 65)
# Push to main
safe_update_log("Pushing initial commit to GitHub...", 75)
push_out, push_err, push_rc = run_command("git push -u origin main", cwd=vault_path)
if push_rc == 0:
safe_update_log("Initial commit pushed successfully to GitHub.", 80)
if completion_callback:
completion_callback(True, "Initial commit and push completed successfully")
else:
safe_update_log(f"Push failed: {push_err}", 80)
if completion_callback:
completion_callback(False, f"Push failed: {push_err}")
else:
safe_update_log(f"Error committing files: {err_commit}", 60)
if completion_callback:
completion_callback(False, f"Commit failed: {err_commit}")
else:
# We already have at least one commit
safe_update_log("Local repository already has commits. Skipping initial commit step.", 50)
if completion_callback:
completion_callback(True, "Repository already has commits")
except Exception as e:
safe_update_log(f"Error in commit/push operation: {e}", None)
if completion_callback:
completion_callback(False, f"Error: {e}")
thread = threading.Thread(target=_commit_push_thread, daemon=True)
thread.start()
return thread