[arch] refactoring: git log call

This commit is contained in:
Vinicius Moreira
2022-05-27 11:39:08 -03:00
parent 95b0e5d45e
commit 7875529fb6
2 changed files with 34 additions and 38 deletions

View File

@@ -674,7 +674,7 @@ class ArchManager(SoftwareManager, SettingsController):
context.project_dir = clone_dir context.project_dir = clone_dir
srcinfo_path = f'{clone_dir}/.SRCINFO' 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) context.watcher.change_progress(40)
if not logs or len(logs) == 1: if not logs or len(logs) == 1:
@@ -1699,7 +1699,7 @@ class ArchManager(SoftwareManager, SettingsController):
if not os.path.exists(srcinfo_path): if not os.path.exists(srcinfo_path):
return PackageHistory.empyt(pkg) return PackageHistory.empyt(pkg)
logs = git.log_shas_and_timestamps(clone_dir) logs = git.list_commits(clone_dir)
if logs: if logs:
srcfields = {'epoch', 'pkgver', 'pkgrel'} srcfields = {'epoch', 'pkgver', 'pkgrel'}
@@ -2083,11 +2083,14 @@ class ArchManager(SoftwareManager, SettingsController):
if pkgbuilt: if pkgbuilt:
self.__fill_aur_output_files(context) self.__fill_aur_output_files(context)
self.logger.info("Reading '{}' cloned repository current commit".format(context.name)) self.logger.info(f"Reading '{context.name}' cloned repository current commit")
context.commit = git.get_current_commit(context.project_dir) commits = git.list_commits(context.project_dir, limit=1)
if not context.commit: if commits:
self.logger.error("Could not read '{}' cloned repository current commit".format(context.name)) context.commit = commits[0][0]
else:
self.logger.error(f"Could not read '{context.name}' cloned repository current commit")
if self._install(context=context): if self._install(context=context):
self._save_pkgbuild(context) self._save_pkgbuild(context)

View File

@@ -1,56 +1,49 @@
import shutil import shutil
from datetime import datetime from io import StringIO
from logging import Logger
from typing import List, Tuple, Optional from typing import List, Tuple, Optional
from bauh.commons import system from bauh.commons import system
from bauh.commons.system import new_subprocess, SimpleProcess from bauh.commons.system import SimpleProcess
def is_installed() -> bool: def is_installed() -> bool:
return bool(shutil.which('git')) return bool(shutil.which('git'))
def list_commits(proj_dir: str) -> List[dict]: def list_commits(proj_dir: str, limit: int = -1, logger: Optional[Logger] = None) -> Optional[List[Tuple[str, int]]]:
logs = new_subprocess(['git', 'log', '--date=iso'], cwd=proj_dir).stdout if limit == 0:
return
commits, commit = [], {} cmd = StringIO()
for out in new_subprocess(['grep', '-E', 'commit|Date:'], stdin=logs).stdout: cmd.write('git log --format="%H %ct"')
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 = {}
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]: if code == 0 and output:
code, output = system.execute(cmd='git log -1 --format=%H', shell=True, cwd=repo_path) commits = []
for line in output.split('\n'):
if code == 0:
for line in output.strip().split('\n'):
line_strip = line.strip() line_strip = line.strip()
if 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]]]: if logger:
code, output = system.execute(cmd='git log --format="%H %at"', shell=True, cwd=repo_path) logger.error(f"Could not parse commit date {line_split[1]}")
if code == 0: commits.append((commit_sha, commit_date))
logs = []
for line in output.strip().split('\n'):
line_strip = line.strip()
if line_strip: return commits
line_split = line_strip.split(' ')
logs.append((line_split[0].strip(), int(line_split[1].strip())))
return logs
def clone(url: str, target_dir: Optional[str], depth: int = -1, custom_user: Optional[str] = None) -> SimpleProcess: def clone(url: str, target_dir: Optional[str], depth: int = -1, custom_user: Optional[str] = None) -> SimpleProcess: