public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: carlos.bilbao@kernel.org
To: apw@canonical.com, joe@perches.com, dwaipayanray1@gmail.com,
	lukas.bulwahn@gmail.com
Cc: bilbao@vt.edu, linux-kernel@vger.kernel.org,
	Carlos Bilbao <carlos.bilbao@kernel.org>
Subject: [PATCH 1/1] checkpatch: add --branch option to check commits in current branch
Date: Mon,  3 Feb 2025 19:28:22 -0600	[thread overview]
Message-ID: <20250204012822.1379477-2-carlos.bilbao@kernel.org> (raw)
In-Reply-To: <20250204012822.1379477-1-carlos.bilbao@kernel.org>

From: Carlos Bilbao <carlos.bilbao@kernel.org>

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@kernel.org>
---
 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


      reply	other threads:[~2025-02-04  1:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-04  1:28 [PATCH 0/1] checkpatch: add --branch option to check commits in current branch carlos.bilbao
2025-02-04  1:28 ` carlos.bilbao [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250204012822.1379477-2-carlos.bilbao@kernel.org \
    --to=carlos.bilbao@kernel.org \
    --cc=apw@canonical.com \
    --cc=bilbao@vt.edu \
    --cc=dwaipayanray1@gmail.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas.bulwahn@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox