File: utils.py

package info (click to toggle)
python-changelogd 0.1.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 448 kB
  • sloc: python: 1,921; makefile: 21
file content (33 lines) | stat: -rw-r--r-- 953 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import logging
import subprocess
import typing
from pathlib import Path


def get_git_data() -> typing.Optional[typing.Tuple[str, str]]:
    try:
        git_data = subprocess.check_output(["git", "config", "--list"])
    except subprocess.CalledProcessError:
        logging.info("Cannot read git data.")
        return None

    data = {
        key: value
        for key, value in (
            line.split("=", maxsplit=1)
            for line in git_data.decode().splitlines()
            if "=" in line
        )
    }
    return data.get("user.name", ""), data.get("user.email", "")


def add_to_git(path: typing.Union[Path, str]) -> None:
    process = subprocess.Popen(
        ["git", "add", str(path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )
    _, err = process.communicate()
    if process.returncode == 0:
        logging.info(f"Added to git: {path}")
    else:
        logging.error(f"Failed to add to git: {err.decode()}")