From 7875529fb67f1485cf8ad796e1ea94388fbcd0df Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Fri, 27 May 2022 11:39:08 -0300 Subject: [PATCH] [arch] refactoring: git log call --- bauh/gems/arch/controller.py | 15 ++++++---- bauh/gems/arch/git.py | 57 ++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/bauh/gems/arch/controller.py b/bauh/gems/arch/controller.py index 428db069..b404ab57 100644 --- a/bauh/gems/arch/controller.py +++ b/bauh/gems/arch/controller.py @@ -674,7 +674,7 @@ class ArchManager(SoftwareManager, SettingsController): context.project_dir = clone_dir srcinfo_path = f'{clone_dir}/.SRCINFO' - logs = git.log_shas_and_timestamps(clone_dir) + logs = git.list_commits(clone_dir) context.watcher.change_progress(40) if not logs or len(logs) == 1: @@ -1699,7 +1699,7 @@ class ArchManager(SoftwareManager, SettingsController): if not os.path.exists(srcinfo_path): return PackageHistory.empyt(pkg) - logs = git.log_shas_and_timestamps(clone_dir) + logs = git.list_commits(clone_dir) if logs: srcfields = {'epoch', 'pkgver', 'pkgrel'} @@ -2083,11 +2083,14 @@ class ArchManager(SoftwareManager, SettingsController): if pkgbuilt: self.__fill_aur_output_files(context) - self.logger.info("Reading '{}' cloned repository current commit".format(context.name)) - context.commit = git.get_current_commit(context.project_dir) + self.logger.info(f"Reading '{context.name}' cloned repository current commit") + commits = git.list_commits(context.project_dir, limit=1) - if not context.commit: - self.logger.error("Could not read '{}' cloned repository current commit".format(context.name)) + if commits: + context.commit = commits[0][0] + + else: + self.logger.error(f"Could not read '{context.name}' cloned repository current commit") if self._install(context=context): self._save_pkgbuild(context) diff --git a/bauh/gems/arch/git.py b/bauh/gems/arch/git.py index 8320002d..632752d8 100644 --- a/bauh/gems/arch/git.py +++ b/bauh/gems/arch/git.py @@ -1,56 +1,49 @@ import shutil -from datetime import datetime +from io import StringIO +from logging import Logger from typing import List, Tuple, Optional from bauh.commons import system -from bauh.commons.system import new_subprocess, SimpleProcess +from bauh.commons.system import SimpleProcess def is_installed() -> bool: return bool(shutil.which('git')) -def list_commits(proj_dir: str) -> List[dict]: - logs = new_subprocess(['git', 'log', '--date=iso'], cwd=proj_dir).stdout +def list_commits(proj_dir: str, limit: int = -1, logger: Optional[Logger] = None) -> Optional[List[Tuple[str, int]]]: + if limit == 0: + return - commits, commit = [], {} - for out in new_subprocess(['grep', '-E', 'commit|Date:'], stdin=logs).stdout: - if out: - line = out.decode() - if line.startswith('commit'): - commit['commit'] = line.split(' ')[1].strip() - elif line.startswith('Date'): - commit['date'] = datetime.fromisoformat(line.split(':')[1].strip()) - commits.append(commit) - commit = {} + cmd = StringIO() + cmd.write('git log --format="%H %ct"') - return commits + if limit > 0: + cmd.write(f' -{limit}') + code, output = system.execute(cmd.getvalue(), cwd=proj_dir, shell=True) -def get_current_commit(repo_path: str) -> Optional[str]: - code, output = system.execute(cmd='git log -1 --format=%H', shell=True, cwd=repo_path) - - if code == 0: - for line in output.strip().split('\n'): + if code == 0 and output: + commits = [] + for line in output.split('\n'): line_strip = line.strip() if line_strip: - return line_strip + line_split = line_strip.split(' ', 1) + if len(line_split) == 2: + commit_sha = line_split[0].strip() + try: + commit_date = int(line_split[1].strip()) + except ValueError: + commit_date = None -def log_shas_and_timestamps(repo_path: str) -> Optional[List[Tuple[str, int]]]: - code, output = system.execute(cmd='git log --format="%H %at"', shell=True, cwd=repo_path) + if logger: + logger.error(f"Could not parse commit date {line_split[1]}") - if code == 0: - logs = [] - for line in output.strip().split('\n'): - line_strip = line.strip() + commits.append((commit_sha, commit_date)) - if line_strip: - line_split = line_strip.split(' ') - logs.append((line_split[0].strip(), int(line_split[1].strip()))) - - return logs + return commits def clone(url: str, target_dir: Optional[str], depth: int = -1, custom_user: Optional[str] = None) -> SimpleProcess: