change function get_ahead_behind_count

This commit is contained in:
VALLONGOL 2025-04-23 10:57:20 +02:00
parent ecbd987289
commit 5d8e030923

View File

@ -1659,58 +1659,71 @@ class GitCommands:
def get_ahead_behind_count(self, working_directory: str, local_branch: str, upstream_branch: str) -> Tuple[int | None, int | None]:
"""
Gets the number of commits the local branch is ahead and behind its upstream counterpart.
Output of rev-list is <behind_count> <ahead_count>.
Returns tuple in the order (ahead_count, behind_count).
Gets the number of commits the local branch is ahead and behind its upstream counterpart
using two separate 'git rev-list --count' commands.
Args:
working_directory (str): Path to the repository.
local_branch (str): The name of the local branch (implicitly HEAD when checked out).
upstream_branch (str): The full name of the upstream branch (e.g., 'origin/main').
Returns:
Tuple[int | None, int | None]: A tuple containing (ahead_count, behind_count).
Returns (None, None) if any command fails.
"""
func_name = "get_ahead_behind_count"
log_handler.log_debug(f"Getting ahead/behind count for '{local_branch}'...'{upstream_branch}' in '{working_directory}'", func_name=func_name)
log_handler.log_debug(f"Getting ahead/behind count for '{local_branch}' vs '{upstream_branch}' using separate counts.", func_name=func_name)
cmd = ["git", "rev-list", "--count", "--left-right", f"{local_branch}...{upstream_branch}"]
ahead_count = None
behind_count = None
try:
result = self.log_and_execute(
cmd, working_directory, check=False, capture=True, hide_console=True, log_output_level=logging.DEBUG
# --- Get Ahead Count ---
# Conta i commit in locale non presenti nell'upstream
ahead_cmd = ["git", "rev-list", "--count", f"{upstream_branch}..{local_branch}"]
log_handler.log_debug(f"Executing ahead count command: {' '.join(ahead_cmd)}", func_name=func_name)
ahead_result = self.log_and_execute(
ahead_cmd, working_directory, check=False, capture=True, hide_console=True, log_output_level=logging.DEBUG
)
if result.returncode == 0 and result.stdout:
output = result.stdout.strip()
parts = output.split()
if len(parts) == 2:
if ahead_result.returncode == 0 and ahead_result.stdout:
try:
# ---<<< ASSEGNAZIONE CORRETTA DALL'OUTPUT >>>---
# parts[0] è behind, parts[1] è ahead
behind_count_from_git = int(parts[0])
ahead_count_from_git = int(parts[1])
# ---<<< FINE ASSEGNAZIONE >>>---
log_handler.log_info(
f"Git Output Parsed: Behind={behind_count_from_git}, Ahead={ahead_count_from_git} "
f"(Raw: '{output}')", # Log più chiaro
func_name=func_name
)
# ---<<< RETURN CORRETTO: (ahead, behind) >>>---
# Restituisci la tupla nell'ordine che il resto del codice si aspetta: (ahead, behind)
return ahead_count_from_git, behind_count_from_git
# ---<<< FINE RETURN >>>---
ahead_count = int(ahead_result.stdout.strip())
log_handler.log_debug(f"Ahead count: {ahead_count}", func_name=func_name)
except ValueError:
log_handler.log_error(f"Failed to parse rev-list count output: '{output}'", func_name=func_name)
return None, None
log_handler.log_error(f"Failed to parse ahead count output: '{ahead_result.stdout.strip()}'", func_name=func_name)
return None, None # Errore parsing
else:
log_handler.log_error(f"Unexpected output format from rev-list count: '{output}'", func_name=func_name)
return None, None
else:
log_handler.log_warning(
f"Failed to get ahead/behind count (RC={result.returncode}). Maybe invalid branches or no common history? Stderr: {result.stderr.strip() if result.stderr else 'N/A'}",
func_name=func_name
log_handler.log_warning(f"Ahead count command failed (RC={ahead_result.returncode}). Stderr: {ahead_result.stderr.strip() if ahead_result.stderr else 'N/A'}", func_name=func_name)
return None, None # Comando fallito
# --- Get Behind Count ---
# Conta i commit nell'upstream non presenti in locale
behind_cmd = ["git", "rev-list", "--count", f"{local_branch}..{upstream_branch}"]
log_handler.log_debug(f"Executing behind count command: {' '.join(behind_cmd)}", func_name=func_name)
behind_result = self.log_and_execute(
behind_cmd, working_directory, check=False, capture=True, hide_console=True, log_output_level=logging.DEBUG
)
return None, None
if behind_result.returncode == 0 and behind_result.stdout:
try:
behind_count = int(behind_result.stdout.strip())
log_handler.log_debug(f"Behind count: {behind_count}", func_name=func_name)
except ValueError:
log_handler.log_error(f"Failed to parse behind count output: '{behind_result.stdout.strip()}'", func_name=func_name)
return None, None # Errore parsing
else:
log_handler.log_warning(f"Behind count command failed (RC={behind_result.returncode}). Stderr: {behind_result.stderr.strip() if behind_result.stderr else 'N/A'}", func_name=func_name)
return None, None # Comando fallito
# --- Return Result ---
log_handler.log_info(f"Ahead/Behind for '{local_branch}': Ahead={ahead_count}, Behind={behind_count}", func_name=func_name)
# Restituisce la tupla nell'ordine (ahead, behind)
return ahead_count, behind_count
except Exception as e:
log_handler.log_exception(f"Unexpected error getting ahead/behind count: {e}", func_name=func_name)
return None, None # Segnala fallimento # Segnala fallimento
return None, None # Segnala fallimento generico # Segnala fallimento # Segnala fallimento
# --- END OF FILE git_commands.py ---