Skip to content

Commit 490b66e

Browse files
authored
Merge pull request #189 from sysprog21/sanity-checks
Validate Change-Id usage via test driver
2 parents 41c6c1d + 3fcfcde commit 490b66e

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

qtest.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ bool commit_exists(const char *commit_hash)
12521252
close(pipefd[1]);
12531253

12541254
FILE *stream = fdopen(pipefd[0], "r");
1255-
if (stream == NULL) {
1255+
if (!stream) {
12561256
/* Error converting file descriptor to stream */
12571257
perror("fdopen");
12581258
return false;
@@ -1276,6 +1276,26 @@ bool commit_exists(const char *commit_hash)
12761276
return found;
12771277
}
12781278

1279+
static bool check_commitlog(void)
1280+
{
1281+
pid_t pid;
1282+
int status;
1283+
char *script_path = "scripts/check-commitlog.sh";
1284+
char *argv[] = {script_path, NULL};
1285+
1286+
int spawn_ret = posix_spawnp(&pid, script_path, NULL, NULL, argv, environ);
1287+
if (spawn_ret != 0)
1288+
return false;
1289+
1290+
if (waitpid(pid, &status, 0) == -1)
1291+
return false;
1292+
1293+
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1294+
return false;
1295+
1296+
return true;
1297+
}
1298+
12791299
#define GIT_HOOK ".git/hooks/"
12801300
static bool sanity_check()
12811301
{
@@ -1314,6 +1334,14 @@ static bool sanity_check()
13141334
"FATAL: The repository is outdated. Please update properly.\n");
13151335
return false;
13161336
}
1337+
if (!check_commitlog()) {
1338+
fprintf(stderr, "FATAL: The git commit history is chaotic.\n");
1339+
fprintf(stderr,
1340+
"Please install the required git hooks per the assignment "
1341+
"instructions and make your commits from the terminal "
1342+
"instead of using the GitHub web interface.\n");
1343+
return false;
1344+
}
13171345
}
13181346

13191347
return true;

scripts/check-commitlog.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
# Check that every non-merge commit after the specified base commit has a commit
4+
# message ending with a valid Change-Id line. A valid Change-Id line must be the
5+
# last non-empty line of the commit message and follow the format:
6+
#
7+
# Change-Id: I<hexadecimal_hash>
8+
#
9+
# Merge commits are excluded from this check.
10+
11+
# Base commit from which to start checking.
12+
BASE_COMMIT="0b8be2c15160c216e8b6ec82c99a000e81c0e429"
13+
14+
# Get a list of non-merge commit hashes after BASE_COMMIT.
15+
commits=$(git rev-list --no-merges "${BASE_COMMIT}"..HEAD)
16+
17+
failed=0
18+
19+
for commit in $commits; do
20+
# Retrieve the commit message for the given commit.
21+
commit_msg=$(git log -1 --format=%B "${commit}")
22+
23+
# Extract the last non-empty line from the commit message.
24+
last_line=$(echo "$commit_msg" | awk 'NF {line=$0} END {print line}')
25+
26+
# Check if the last line matches the expected Change-Id format.
27+
if [[ ! $last_line =~ ^Change-Id:\ I[0-9a-fA-F]+$ ]]; then
28+
subject=$(git log -1 --format=%s "${commit}")
29+
short_hash=$(git rev-parse --short "${commit}")
30+
echo "Commit ${short_hash} with subject '$subject' does not end with a valid Change-Id."
31+
failed=1
32+
fi
33+
done
34+
35+
if [ $failed -ne 0 ]; then
36+
echo
37+
echo "Some commits are missing a valid Change-Id. Please amend the commit messages accordingly."
38+
echo
39+
exit 1
40+
fi
41+
42+
exit 0

scripts/checksums

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
db6784ff3917888db4d1dceaa0570d99ed40e762 queue.h
22
bfb6df45d64356868c86a7173455d39970bd0270 list.h
3+
3bb0192cee08d165fd597a9f6fbb404533e28fcf scripts/check-commitlog.sh

0 commit comments

Comments
 (0)