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]: 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. Gets the number of commits the local branch is ahead and behind its upstream counterpart.
Output of rev-list is <behind_count> <ahead_count>.
Args: Returns tuple in the order (ahead_count, behind_count).
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.
""" """
func_name = "get_ahead_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) 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}"] cmd = ["git", "rev-list", "--count", "--left-right", f"{local_branch}...{upstream_branch}"]
try: try:
# Esegui catturando output, nascondendo console. check=False.
result = self.log_and_execute( result = self.log_and_execute(
cmd, working_directory, check=False, capture=True, hide_console=True, log_output_level=logging.DEBUG cmd, working_directory, check=False, capture=True, hide_console=True, log_output_level=logging.DEBUG
) )
if result.returncode == 0 and result.stdout: if result.returncode == 0 and result.stdout:
output = result.stdout.strip() output = result.stdout.strip()
parts = output.split() # Divide per spazio o tab parts = output.split()
if len(parts) == 2: if len(parts) == 2:
try: try:
behind_count = int(parts[0]) # Primo numero è 'behind' # ---<<< ASSEGNAZIONE CORRETTA DALL'OUTPUT >>>---
ahead_count = int(parts[1]) # Secondo numero è 'ahead' # parts[0] è behind, parts[1] è ahead
log_handler.log_info(f"Ahead/Behind for '{local_branch}': Ahead={ahead_count}, Behind={behind_count}", func_name=func_name) behind_count_from_git = int(parts[0])
# Restituisci la tupla nell'ordine (ahead, behind) come atteso dal resto del codice ahead_count_from_git = int(parts[1])
return ahead_count, behind_count # ---<<< 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: except ValueError:
log_handler.log_error(f"Failed to parse rev-list count output: '{output}'", func_name=func_name) log_handler.log_error(f"Failed to parse rev-list count output: '{output}'", func_name=func_name)
return None, None 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) log_handler.log_error(f"Unexpected output format from rev-list count: '{output}'", func_name=func_name)
return None, None return None, None
else: else:
# Comando fallito
log_handler.log_warning( 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'}", 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 func_name=func_name
) )
return None, None # Segnala fallimento return None, None
except Exception as e: except Exception as e:
log_handler.log_exception(f"Unexpected error getting ahead/behind count: {e}", func_name=func_name) log_handler.log_exception(f"Unexpected error getting ahead/behind count: {e}", func_name=func_name)