Skip to content

Commit b82bebd

Browse files
Add GitStatusChecker to detect if branch is behind upstream.Show current branch status and also check 'main' if not on it (#48)
1 parent ab7086d commit b82bebd

4 files changed

Lines changed: 73 additions & 5 deletions

File tree

anodet/general.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import os
33
import matplotlib.pyplot as plt
44
import torch
5+
import subprocess
6+
import sys
57

68

79

@@ -148,4 +150,67 @@ def get_avg_time_ms(self, num_operations):
148150
"""
149151
if num_operations > 0:
150152
return (self.accumulated_time / num_operations) * 1000
151-
return 0.0
153+
return 0.0
154+
155+
156+
class GitStatusChecker:
157+
def __init__(self, branch: str = "HEAD"):
158+
self.branch = branch
159+
160+
def _run(self, cmd: list[str]) -> str:
161+
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
162+
return result.stdout.strip()
163+
164+
def is_repo(self) -> bool:
165+
try:
166+
out = self._run(["git", "rev-parse", "--is-inside-work-tree"])
167+
return out == "true"
168+
except subprocess.CalledProcessError:
169+
return False
170+
171+
def get_current_branch(self) -> str:
172+
try:
173+
return self._run(["git", "rev-parse", "--abbrev-ref", "HEAD"])
174+
except subprocess.CalledProcessError:
175+
return "UNKNOWN"
176+
177+
def check_branch_status(self, branch_name: str) -> None:
178+
print(f"\n🌿 Checking branch: {branch_name}")
179+
180+
# Make sure branch exists locally
181+
try:
182+
self._run(["git", "rev-parse", "--verify", branch_name])
183+
except subprocess.CalledProcessError:
184+
print(f"⚠️ Branch '{branch_name}' does not exist locally.")
185+
return
186+
187+
# Fetch latest changes from remote
188+
self._run(["git", "fetch"])
189+
190+
# Compare with upstream
191+
try:
192+
counts = self._run(["git", "rev-list", "--left-right", "--count", f"{branch_name}...{branch_name}@{{u}}"])
193+
ahead, behind = map(int, counts.split())
194+
except subprocess.CalledProcessError:
195+
print(f"⚠️ No upstream set for '{branch_name}'. Use:")
196+
print(f" git branch --set-upstream-to origin/{branch_name} {branch_name}")
197+
return
198+
199+
if behind > 0:
200+
print(f"⬇️ Branch '{branch_name}' is behind by {behind} commit(s). Run `git pull`.")
201+
elif ahead > 0:
202+
print(f"⬆️ Branch '{branch_name}' is ahead by {ahead} commit(s). Run `git push`.")
203+
else:
204+
print(f"✅ Branch '{branch_name}' is up-to-date with the remote.")
205+
206+
def check_status(self) -> None:
207+
if not self.is_repo():
208+
print("❌ Not a git repository. Navigate to a repo and try again.")
209+
sys.exit(1)
210+
211+
current_branch = self.get_current_branch()
212+
self.check_branch_status(current_branch)
213+
214+
# If not on main, also check main
215+
if current_branch != "main":
216+
self.check_branch_status("main")

detect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ def parse_args():
8383
# Visualization parameters
8484
parser.add_argument(
8585
"--enable_visualization",
86-
action="store_true",
86+
action="store_false",
8787
help="Enable visualization of results.",
8888
)
8989
parser.add_argument(
9090
"--save_visualizations",
91-
action="store_true",
91+
action="store_false",
9292
help="Save visualization images to disk.",
9393
)
9494
parser.add_argument(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "AnomaVision"
3-
version = "2.0.37"
3+
version = "2.0.38"
44
description = "Deep learnIng Anomaly Detection EnvironMent [AnomaVision] is a deep learning library that aims to collect state-of-the-art anomaly detection algorithms for benchmarking on both public and private datasets. PaDimOpti provides several ready-to-use implementations of anomaly detection algorithms described in the recent literature, as well as a set of tools that facilitate the development and implementation of custom models. The library has a strong focus on image-based anomaly detection, where the goal of the algorithm is to identify anomalous images, or anomalous pixel regions within images in a dataset. PaDimOpti is constantly being updated with new algorithms and training/inference extensions, so stay tuned!!"
55
authors = ["Deep Knowledge <Deepp.Knowledge@gmail.com>"]
66
readme = "README.md"

train.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
import anodet
1313
from anodet.utils import get_logger, save_args_to_yaml, setup_logging
14-
from anodet.general import increment_path
14+
from anodet.general import increment_path, GitStatusChecker
1515

1616
# pre-commit run trailing-whitespace --files .\anodet\utils.py
1717

18+
checker = GitStatusChecker()
19+
checker.check_status()
20+
1821
def parse_args():
1922
parser = argparse.ArgumentParser(
2023
description="Train a PaDiM model for anomaly detection."

0 commit comments

Comments
 (0)