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

From: carlos . bilbao
Date: Mon Feb 03 2025 - 20:29:24 EST


From: Carlos Bilbao <carlos.bilbao@xxxxxxxxxx>

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.

Signed-off-by: Carlos Bilbao <carlos.bilbao@xxxxxxxxxx>
---
scripts/checkpatch.pl | 68 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index f7087dda9ac9..1f929fcf298d 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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;
--
2.43.5