Commit b7b7c77684 for qemu.org

commit b7b7c77684a2f4bd12c408dec1a52d849b7382a5
Author: Alex Bennée <alex.bennee@linaro.org>
Date:   Fri Oct 30 18:30:54 2026 +0000

    scripts/checkpatch: validate fixes against upstream repo

    The current check for a referenced fix commit relies on the developer
    keeping their master branch up to date which is not always guaranteed.
    If you create your branches directly from the upstream repo this will
    keep failing. Besides the local master could be anything.

    Improve the logic to divine which remote is pointing at the upstream
    repo and use the remote master to check the fixes.

    checkpatch complains about the line lengths but this seems to be
    standard for the perl script so I didn't try and fix it.

    Acked-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
    Message-ID: <20261030183054.2875292-1-alex.bennee@linaro.org>
    Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 82f62f6021..e7514a4a9f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -586,6 +586,37 @@ sub git_commit_info {
 	return ($id, $desc);
 }

+my $git_upstream_branch_checked = 0;
+my $git_upstream_branch_cached = undef;
+
+sub git_upstream_branch {
+	return $git_upstream_branch_cached if ($git_upstream_branch_checked);
+	$git_upstream_branch_checked = 1;
+
+	return undef if ((which("git") eq "") || !(-e "$gitroot"));
+
+	my $url_regex =
+		qr{(?:gitlab\.com[/: ]qemu-project|github\.com[/: ]qemu)/qemu(?:\.git)?$}i;
+
+	# Scan configured remotes for the upstream repository
+	my $output = `git remote -v 2>/dev/null`;
+	for my $line (split(/\n/, $output)) {
+		if ($line =~ /^\s*(\S+)\s+(\S+)\s+\(fetch\)/) {
+			my ($remote, $url) = ($1, $2);
+			if ($url =~ $url_regex) {
+				my $ref = "$remote/master";
+				`git rev-parse --verify --quiet $ref 2>/dev/null`;
+				if ($? == 0) {
+					$git_upstream_branch_cached = $ref;
+					return $git_upstream_branch_cached;
+				}
+			}
+		}
+	}
+
+	return undef;
+}
+
 sub expand_tabs {
 	my ($str) = @_;

@@ -1913,9 +1944,12 @@ sub process {
 				}
 				if (which("git") ne "" && -e "$gitroot") {
 					my $hash = defined($cid) ? $cid : $orig_commit;
-					`git merge-base --is-ancestor $hash master 2>/dev/null`;
-					if ($? != 0) {
-						WARN("Fixes: commit $hash is not an ancestor of master\n" . $herecurr);
+					my $upstream = git_upstream_branch();
+					if (defined($upstream)) {
+						`git merge-base --is-ancestor $hash $upstream 2>/dev/null`;
+						if ($? != 0) {
+							WARN("Fixes: commit $hash is not an ancestor of $upstream\n" . $herecurr);
+						}
 					}
 				}
 			}