Skip to content

Commit 70261b6

Browse files
Refactor update menu functionality in ESP32 manager; streamline Git checks and enhance user feedback for update status
1 parent 93c7587 commit 70261b6

File tree

2 files changed

+58
-80
lines changed

2 files changed

+58
-80
lines changed

.idea/workspace.xml

Lines changed: 43 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flasher.py

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -73,65 +73,19 @@ def main_menu(self) -> None:
7373
else:
7474
tprint.warning("Invalid selection. Try again.")
7575

76-
@staticmethod
77-
def __update_menu() -> None:
76+
def __update_menu(self) -> None:
7877
print()
7978
separator("ESP32 Ultra Manager - Update")
8079
print()
81-
errors = []
8280

8381
try:
84-
# Check git presence
85-
if shutil.which("git") is None:
86-
tprint.warning("Git is not installed on this system.")
87-
errors.append("Missing git.")
88-
else:
89-
tprint.debug("Git is installed.")
90-
91-
# Check if current directory is a Git repo
92-
try:
93-
subprocess.check_output(["git", "rev-parse", "--is-inside-work-tree"], stderr=subprocess.DEVNULL)
94-
git_available = True
95-
except subprocess.CalledProcessError:
96-
tprint.warning("This project is not a Git repository.")
97-
errors.append("Not a git repo.")
98-
git_available = False
99-
100-
if git_available:
101-
# Check for uncommitted changes
102-
try:
103-
status = subprocess.check_output(["git", "status", "--porcelain"]).decode().strip()
104-
if status:
105-
tprint.warning("Uncommitted changes detected. Please commit or stash your changes before updating.")
106-
errors.append("Uncommitted changes.")
107-
except Exception:
108-
tprint.error("Unable to check for uncommitted changes.")
109-
errors.append("Git status check failed.")
110-
111-
# Check if local is ahead of remote
112-
try:
113-
branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip()
114-
local_commit = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
115-
remote_commit = subprocess.check_output(["git", "rev-parse", f"origin/{branch}"]).decode().strip()
116-
if subprocess.check_output(["git", "rev-list", "--left-right", f"{remote_commit}..{local_commit}"]).strip():
117-
tprint.warning("Your local branch is ahead of the remote. Push your changes before updating.")
118-
errors.append("Local branch ahead of remote.")
119-
except Exception:
120-
tprint.error("Unable to check if local branch is ahead of remote.")
121-
errors.append("Git ahead check failed.")
122-
123-
# Check GitHub connectivity
124-
ping_result = os.system(
125-
"ping -n 1 github.com > nul" if os.name == "nt" else "ping -c 1 github.com > /dev/null")
126-
if ping_result != 0:
127-
tprint.warning("Cannot reach github.com. Please check your internet connection.")
128-
errors.append("Offline.")
129-
else:
130-
tprint.debug("github.com is reachable.")
131-
132-
if errors:
133-
tprint.error("Update check failed due to the issues above.")
134-
tprint.debug("Errors: " + ", ".join(errors))
82+
update_status, update_color = self.check.update_status()
83+
if update_status not in ["Up-to-date", "Uncommitted Changes", "Ahead of Main", "Update Available"]:
84+
tprint.warning(f"{update_status}. Please resolve the issues before updating.")
85+
return
86+
if update_status in ["Uncommitted Changes", "Ahead of Main"]:
87+
tprint.warning("You have uncommitted changes or are ahead of the main branch.")
88+
tprint.warning("Please commit or stash your changes before updating.")
13589
return
13690

13791
# Fetch remote and check for updates
@@ -571,9 +525,8 @@ def update_status() -> tuple[str, str]:
571525
except Exception:
572526
return "Unknown Branch", "\033[91m" # Red
573527

574-
# Check GitHub connectivity
575-
ping_result = os.system(
576-
"ping -n 1 github.com > nul" if os.name == "nt" else "ping -c 1 github.com > /dev/null")
528+
# Check GitHub connectivity (Windows only)
529+
ping_result = subprocess.run(["ping", "-n", "1", "github.com"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
577530
if ping_result != 0:
578531
return "Offline", "\033[91m" # Red
579532

@@ -582,7 +535,11 @@ def update_status() -> tuple[str, str]:
582535
local_commit = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
583536
remote_commit = subprocess.check_output(["git", "rev-parse", f"origin/{branch}"]).decode().strip()
584537

585-
if local_commit == remote_commit:
538+
if subprocess.check_output(["git", "status", "--porcelain"]).strip():
539+
return "Uncommitted Changes", "\033[93m" # Yellow
540+
elif subprocess.check_output(["git", "rev-list", "--left-right", f"{branch}..origin/{branch}"]).strip():
541+
return "Ahead of Remote", "\033[93m" # Yellow
542+
elif local_commit == remote_commit:
586543
return "Up-to-date", "\033[97m" # White
587544
else:
588545
return "Update Available", "\033[92m" # Green
@@ -659,4 +616,3 @@ def main():
659616

660617
if __name__ == "__main__":
661618
main()
662-

0 commit comments

Comments
 (0)