Skip to content

Commit 7e1870e

Browse files
committed
tools/vcs_tag: Add support for reading .git
There is still no support for depfiles, so that would need to be implemented. But it's a start
1 parent 4a3de17 commit 7e1870e

File tree

8 files changed

+146
-3
lines changed

8 files changed

+146
-3
lines changed

src/tools/vcs_tag.cpp

+46-1
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,59 @@
22
// Copyright © 2024 Intel Corporation
33

44
#include "vcs_tag.hpp"
5+
#include "util/process.hpp"
56
#include "util/utils.hpp"
67

78
#include <cassert>
89
#include <fstream>
910
#include <iostream>
11+
#include <optional>
1012
#include <sstream>
13+
#include <vector>
1114

1215
namespace Tools {
1316

1417
namespace fs = std::filesystem;
1518

19+
namespace {
20+
21+
struct VCSData {
22+
std::vector<std::string> command;
23+
fs::path dep;
24+
};
25+
26+
std::optional<VCSData> find_vcs(const fs::path & source_dir) {
27+
// TODO: HG, Subversion, bazaar
28+
const fs::path gitdir = source_dir / ".git";
29+
30+
if (fs::is_directory(gitdir)) {
31+
return VCSData{
32+
.command = {"git", "-C", source_dir, "describe", "--dirty=+", "--always"},
33+
.dep = gitdir / "logs" / "HEAD", // TODO: This doesn't work for git work trees
34+
};
35+
}
36+
37+
return std::nullopt;
38+
}
39+
40+
std::string get_version(const std::optional<VCSData> & vcs_o, std::string_view fallback) {
41+
if (!vcs_o) {
42+
return std::string{fallback};
43+
}
44+
45+
const VCSData & vcs = vcs_o.value();
46+
auto [rc, out, err] = Util::process(vcs.command);
47+
if (rc != 0) {
48+
throw std::runtime_error{"TODO: " + err};
49+
}
50+
out.pop_back();
51+
return out;
52+
}
53+
54+
} // namespace
55+
1656
int generate_vcs_tag(const std::filesystem::path & infile, const std::filesystem::path & outfile,
17-
std::string_view version, std::string_view replacement,
57+
std::string_view fallback, std::string_view replacement,
1858
const std::filesystem::path & source_dir) {
1959
// We assume that the infile exists and has been validated by the
2060
// transpiler, but for debug builds we can assert here.
@@ -23,6 +63,9 @@ int generate_vcs_tag(const std::filesystem::path & infile, const std::filesystem
2363
std::ifstream istream{infile};
2464
std::stringstream ostream{};
2565

66+
const std::optional<VCSData> vcs = find_vcs(source_dir);
67+
const std::string version = get_version(vcs, fallback);
68+
2669
for (std::string line; std::getline(istream, line);) {
2770
ostream << Util::replace(line, replacement, version) << std::endl;
2871
}
@@ -57,6 +100,8 @@ int generate_vcs_tag(const std::filesystem::path & infile, const std::filesystem
57100
}
58101
}
59102

103+
// TODO: emit depfile for git
104+
60105
std::ofstream out{outfile};
61106
out << ostream.str();
62107
return 0;

tests/dsl/13 vcs_tag/compare.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66
import argparse
7+
import difflib
78
import filecmp
89
import sys
910
import typing as T
@@ -20,8 +21,14 @@ def main() -> int:
2021
parser.add_argument('expected')
2122
args: Arguments = parser.parse_args()
2223

23-
same = filecmp.cmp(args.result, args.expected)
24-
return 0 if same else 1
24+
if filecmp.cmp(args.result, args.expected):
25+
return 0
26+
27+
with open(args.result, 'r') as fr, open(args.expected, 'r') as fe:
28+
for line in difflib.ndiff(fe.readlines(), fr.readlines()):
29+
print(line, file=sys.stderr, end='')
30+
31+
return 1
2532

2633

2734
if __name__ == "__main__":

tests/dsl/14 vcs_tag git/basic.in

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is a @VCS_TAG@ and @VCS_TAG@
2+
@VCS_TAG@ @@VCS_TAG@ @VCS_TAG

tests/dsl/14 vcs_tag git/compare.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright © 2024 Intel Corporation
4+
5+
from __future__ import annotations
6+
import argparse
7+
import difflib
8+
import os
9+
import subprocess
10+
import sys
11+
import typing as T
12+
13+
if T.TYPE_CHECKING:
14+
class Arguments(T.Protocol):
15+
result: str
16+
template: str
17+
18+
19+
def main() -> int:
20+
parser = argparse.ArgumentParser()
21+
parser.add_argument('result')
22+
parser.add_argument('template')
23+
args: Arguments = parser.parse_args()
24+
25+
with open(args.result, 'r') as f:
26+
got = f.read()
27+
28+
with open(args.template, 'r') as f:
29+
tmpl = f.read()
30+
31+
# TODO: Cheating a bit here because Meson++ doesn't have the meson object
32+
# implemented yet.
33+
source_dir = os.path.dirname(args.template)
34+
35+
v = subprocess.run(
36+
["git", "-C", source_dir, "describe", "--dirty=+", "--always"],
37+
stdout=subprocess.PIPE,
38+
stderr=subprocess.PIPE,
39+
universal_newlines=True,
40+
)
41+
if v.returncode != 0:
42+
print(v.stderr, file=sys.stderr)
43+
return 2
44+
ever = v.stdout.strip()
45+
expected = tmpl.replace('@VCS_TAG@', ever)
46+
47+
if got == expected:
48+
return 0
49+
50+
for line in difflib.ndiff(got.splitlines(), expected.splitlines()):
51+
print(line, file=sys.stderr)
52+
53+
return 1
54+
55+
56+
if __name__ == "__main__":
57+
sys.exit(main())

tests/dsl/14 vcs_tag git/meson.build

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# Copyright © 2024 Intel Corporation
3+
4+
project(
5+
'vcs_tag (git) test',
6+
version: '1.0.0',
7+
)
8+
9+
python = find_program('python')
10+
tester = files('compare.py')
11+
12+
# TODO: this will not work correctly whenever we implement support for VCs.
13+
x = vcs_tag(input: 'basic.in', output: 'basic.out')
14+
test('git', python, args: [tester, x, files('basic.in')])

tests/dsl/14 vcs_tag git/setup.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
SOURCEDIR="${1}"
5+
6+
pushd "${SOURCEDIR}"
7+
git init .
8+
git add .
9+
git commit -a -m "test commit"
10+
git tag 1
11+
popd

tests/dsl/14 vcs_tag git/teardown.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
SOURCEDIR="${1}"
5+
6+
rm -rf "${SOURCEDIR}/.git"

tests/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ test_cases = {
1818
'11 test types',
1919
'12 add arguments',
2020
'13 vcs_tag',
21+
'14 vcs_tag git',
2122
],
2223
}
2324

0 commit comments

Comments
 (0)