summaryrefslogtreecommitdiff
path: root/tools/ci/git_utils.py
blob: cf1f8d4b4d13f1f3f27021a4902ea24d8368cdab (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# Copyright (c) 2006-2026, RT-Thread Development Team
#
# SPDX-License-Identifier: Apache-2.0
#

import locale
import subprocess


def _default_encoding():
    return locale.getpreferredencoding()


def _decode_output(data, encoding=None):
    if data is None:
        return ""

    if encoding is None:
        encoding = _default_encoding()

    return data.decode(encoding, errors="replace")


def _run_git(args, encoding=None):
    result = subprocess.run(
        ["git"] + args,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        check=False,
    )
    stdout = _decode_output(getattr(result, "stdout", b""), encoding)
    stderr = _decode_output(getattr(result, "stderr", b""), encoding)
    returncode = getattr(result, "returncode", 0)

    if returncode != 0:
        raise RuntimeError(
            "git {} failed with code {}: {}".format(
                " ".join(args),
                returncode,
                stderr.strip(),
            )
        )

    return stdout


def split_nonempty_lines(output):
    return [line.strip() for line in output.splitlines() if line.strip()]


def get_changed_files(base, head, diff_filter="ACMR", no_renames=True, full_index=True, encoding=None):
    args = [
        "diff",
        "--name-only",
        base,
        head,
        "--diff-filter={}".format(diff_filter),
    ]

    if no_renames:
        args.append("--no-renames")
    if full_index:
        args.append("--full-index")

    output = _run_git(args, encoding=encoding)
    return split_nonempty_lines(output)


def get_name_status_lines(base, head="HEAD", encoding=None):
    output = _run_git(
        ["diff", "--name-status", base, head],
        encoding=encoding,
    )
    return split_nonempty_lines(output)


def get_merge_base(target_branch, head="HEAD", encoding=None):
    output = _run_git(
        ["merge-base", target_branch, head],
        encoding=encoding,
    )
    return output.strip()


def _parse_ls_tree_size(output, encoding=None):
    text = _decode_output(output, encoding).strip()
    if not text:
        return 0

    fields = text.split(None, 4)
    if len(fields) < 4:
        return 0

    try:
        return int(fields[3])
    except ValueError:
        return 0


def get_blob_size(ref, path, encoding=None):
    result = subprocess.run(
        ["git", "ls-tree", "-l", ref, "--", path],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        check=False,
    )

    if result.returncode != 0:
        return 0

    return _parse_ls_tree_size(result.stdout, encoding)