[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

@@ -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: