Skip to contentSkip to conversation

checkpatch: add --branch option to check commits in current branch

PATCH New [email protected] 1 participant 2 messages Feb 4, 2025 Checkpatch

lore
Share & tools
lore
#0 lore raw

cover checkpatch: add --branch option to check commits in current branch

From: Carlos Bilbao <[email protected]>

I thought it would help to add an option --branch in checkpatch.pl to check multiple commits within a branch. In my experience, after creating a new experimental branch, I often need to generate multiple patches and run the script on each one individually. If there's an issue, I've to go back, which IMHO is little tedious. That's why I think this option may help reduce manual work. Below are some tests on the expected behavior.

No new commits since upstream:

$ git checkout -b up-to-date
$ git branch --set-upstream-to=check_branch up-to-date
$ ./scripts/checkpatch.pl --branch
Using as upstream branch: check_branch
No commits found between check_branch and up-to-date. Exiting.

With two new commits:

$ git commit --allow-empty -m "Commit 1"
$ git commit --allow-empty -m "Commit 2"
$ ./scripts/checkpatch.pl --branch
Using as upstream branch: check_branch
Checking 2 commits...
ERROR: Does not appear to be a unified-diff format patch

As edge case, with unconfigured upstream:

18:54 [linux][check_branch ?]$ git branch
* check_branch
docs-next
18:54 [linux][check_branch ?]$ ./scripts/checkpatch.pl --branch No explicit upstream branch found in reflog, trying git branch -vv. No upstream branch found. Would you like to specify one? [y/n] n Using as upstream branch: master
No commits found between master and check_branch. Exiting.

but if the user is willing to help:

18:54 [linux][check_branch ?]$ ./scripts/checkpatch.pl --branch No explicit upstream branch found in reflog, trying git branch -vv. No upstream branch found. Would you like to specify one? [y/n] y Please enter: docs-next
Using as upstream branch: docs-next
Checking 1 commits...
WARNING: Missing commit description - Add an appropriate one

ERROR: Missing Signed-off-by: line(s)

total: 1 errors, 1 warnings, 89 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

Commit 90dd5899dfd3 ("test") has style problems, please review.

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
18:54 [linux][check_branch ?]$

Carlos:
checkpatch: add --branch option to check commits in current branch

#1 lore raw

PATCH 1/1 checkpatch: add --branch option to check commits in current branch

From: Carlos Bilbao <[email protected]>

Add a new option --branch in checkpatch.pl that checks all commits made in the current branch since it diverged from its upstream branch. If no explicit branch is found, it prompts the user to specify, defaulting to 'master' otherwise.

---
 scripts/checkpatch.pl | 68 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
scripts/checkpatch.pl +68 -0
index f7087dda9ac9..1f929fcf298d 100755
@@ -73,6 +73,7 @@ my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANC
 # git output parsing needs US English output, so first set backtick child process LANGUAGE
 my $git_command ='export LANGUAGE=en_US.UTF-8; git';
 my $tabsize = 8;
+my $branch;
 my ${CONFIG_} = "CONFIG_";
 
 my %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h
@@ -142,6 +143,10 @@ Options:
                              is a terminal ('auto'). Default is 'auto'.
   --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
                              ${CONFIG_})
+  --branch                   This option checks all commits made in the
+                             current branch since it diverged from its
+                             parent branch. If no explicit upstream branch
+                             is found, it will try with 'master'.
   -h, --help, --version      display this help and exit
 
 When FILE is - read standard input.
@@ -329,10 +334,73 @@ GetOptions(
 	'no-color'	=> \$color,	#keep old behaviors of -nocolor
 	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
 	'kconfig-prefix=s'	=> \${CONFIG_},
+	'branch'	=> \$branch,
 	'h|help'	=> \$help,
 	'version'	=> \$help
 ) or $help = 2;
 
+if ($branch) {
+
+	my $result = 0;
+	$branch = `git rev-parse --abbrev-ref HEAD 2>/dev/null`;
+
+	my $upstream_branch = `git rev-parse --abbrev-ref --symbolic-full-name \@{u} 2>/dev/null`;
+
+	chomp($branch);
+	chomp($upstream_branch);
+
+	if (!$upstream_branch) {
+		print "No explicit upstream branch found in reflog, trying git branch -vv.\n";
+		my @branches = `git branch -vv`;
+
+		foreach my $branch_info (@branches) {
+			if ($branch_info =~ /\s*$branch\s+\S+\s+\[.*\]$/) {
+				if ($branch_info =~ /\[([^\]]+)\]/) {
+					$upstream_branch = $1;
+					last;
+				}
+			}
+		}
+
+		if (!$upstream_branch) {
+			print "No upstream branch found. Would you like to specify one? [y/n] ";
+			my $response = <STDIN>;
+			chomp($response);
+
+			if (lc($response) eq 'n'){
+				$upstream_branch = 'master';
+			}
+			else {
+				print "Please enter: ";
+				$upstream_branch = <STDIN>;
+				chomp($upstream_branch);
+			}
+		}
+	}
+
+	print "Using as upstream branch: $upstream_branch\n";
+
+	my @commits = `git rev-list $upstream_branch..$branch 2>/dev/null`;
+
+	if (!$commits[0]) {
+		print "No commits found between $upstream_branch and $branch. Exiting.\n";
+		exit(0);
+	}
+
+	print "Checking " . scalar(@commits) . " commits...\n";
+
+	foreach my $commit (@commits) {
+		chomp($commit);
+		$result = system("perl $0 --git $commit @ARGV");
+
+		if ($result != 0) {
+			exit($result);
+		}
+	}
+
+	exit($result);
+}
+
 if ($user_codespellfile) {
 	# Use the user provided codespell file unconditionally
 	$codespellfile = $user_codespellfile;
Signature · 1 line
2.43.5