change ahead/behind

This commit is contained in:
VALLONGOL 2025-04-23 10:49:36 +02:00
parent dc5d133ebd
commit ecbd987289

View File

@ -1660,40 +1660,41 @@ 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.
Args:
working_directory (str): Path to the repository.
local_branch (str): The name of the local branch.
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 the command fails or
branches are invalid/unrelated.
Output of rev-list is <behind_count> <ahead_count>.
Returns tuple in the order (ahead_count, behind_count).
"""
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)
# Comando: git rev-list --count --left-right <local_branch>...<upstream_branch>
# Output: <behind_count>\t<ahead_count>
cmd = ["git", "rev-list", "--count", "--left-right", f"{local_branch}...{upstream_branch}"]
try:
# Esegui catturando output, nascondendo console. check=False.
result = self.log_and_execute(
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() # Divide per spazio o tab
parts = output.split()
if len(parts) == 2:
try:
behind_count = int(parts[0]) # Primo numero è 'behind'
ahead_count = int(parts[1]) # Secondo numero è 'ahead'
log_handler.log_info(f"Ahead/Behind for '{local_branch}': Ahead={ahead_count}, Behind={behind_count}", func_name=func_name)
# Restituisci la tupla nell'ordine (ahead, behind) come atteso dal resto del codice
return ahead_count, behind_count
# ---<<< 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 >>>---
except ValueError:
log_handler.log_error(f"Failed to parse rev-list count output: '{output}'", func_name=func_name)
return None, None
@ -1701,12 +1702,11 @@ class GitCommands:
log_handler.log_error(f"Unexpected output format from rev-list count: '{output}'", func_name=func_name)
return None, None
else:
# Comando fallito
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
)
return None, None # Segnala fallimento
return None, None
except Exception as e:
log_handler.log_exception(f"Unexpected error getting ahead/behind count: {e}", func_name=func_name)