2021-10-24 15:08:50 -05:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import List
|
|
|
|
from typing import Optional
|
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
from .util import system
|
|
|
|
|
|
|
|
|
|
|
|
def git_changed_files(
|
2021-11-08 19:34:14 -06:00
|
|
|
git_path: Optional[Path] = None, base: Optional[str] = None, paths: Optional[List[Path]] = None
|
2021-10-24 15:08:50 -05:00
|
|
|
) -> Tuple[List[Path], List[Path], List[Path]]:
|
2021-11-08 19:34:14 -06:00
|
|
|
"""Returns lists of created, deleted and modified files based on diff stats related to a base commit
|
2021-10-24 15:08:50 -05:00
|
|
|
and optional paths.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
git_path: Path to the git repository, current directory by default
|
|
|
|
base: Optional base rev or current index by default
|
|
|
|
paths: Optional list of paths to take into account, unfiltered by default
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2021-11-08 19:34:14 -06:00
|
|
|
Lists of created, deleted and modified paths
|
2021-10-24 15:08:50 -05:00
|
|
|
"""
|
|
|
|
cmd = ["git"]
|
|
|
|
if git_path:
|
|
|
|
cmd += ["-C", str(git_path)]
|
|
|
|
cmd += ["--no-pager", "diff", "--color=never", "--summary", "--numstat"]
|
|
|
|
if base:
|
|
|
|
cmd += [base]
|
|
|
|
if paths:
|
|
|
|
cmd += ["--"]
|
|
|
|
cmd += [str(path) for path in paths]
|
|
|
|
|
|
|
|
result: str = system(cmd)
|
|
|
|
|
|
|
|
created: List[Path] = []
|
|
|
|
deleted: List[Path] = []
|
2021-11-08 19:34:14 -06:00
|
|
|
modified: List[Path] = []
|
2021-10-24 15:08:50 -05:00
|
|
|
|
|
|
|
for line in result.splitlines():
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith("create"):
|
|
|
|
created.append(Path(line.split(maxsplit=3)[3]))
|
|
|
|
continue
|
|
|
|
if line.startswith("delete"):
|
|
|
|
deleted.append(Path(line.split(maxsplit=3)[3]))
|
|
|
|
continue
|
2021-11-08 19:34:14 -06:00
|
|
|
modified.append(Path(line.split(maxsplit=2)[2]))
|
2021-10-24 15:08:50 -05:00
|
|
|
|
2021-11-08 19:34:14 -06:00
|
|
|
modified = [path for path in modified if path not in created and path not in deleted]
|
2021-10-24 15:08:50 -05:00
|
|
|
|
2021-11-08 19:34:14 -06:00
|
|
|
return created, deleted, modified
|