From patchwork Wed Feb 7 01:16:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliott Mitchell X-Patchwork-Id: 1896327 X-Patchwork-Delegate: ynezz@true.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=GSpDd4bw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TVWyp5s1gz23gM for ; Thu, 8 Feb 2024 07:33:06 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References: Subject:Date:From:Message-Id:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=uGva/GqpaJs58xalt9GUxbjIXddqelpWxVgAPHLghp0=; b=GSpDd4bwtQtOFN Xnc5HtLGp6qYEvmvxotrP4iV6wyqzlcFZjb1hlKt0bP/1fTXziNUryaSdNet3dKaly5Amjyt+PPZl v0b6A1iYl7Iv5WUb9F8tPapg1SiTMdqhxXmH0GRT/KAtBKowG/thoDP08EbvsNAR3hi2PDLX2t/zi tARJTlJTsu/ESYWGlJYAzN2awljQkm22XdiPa2ruL0tbCICruwIrzGiq2Yt0Ay5x99rJpXpXvkzDe OqF1vxjerJ535tyqFcZnElvNcsGVj2qVBzLeRJnygMc626C0Z2Xdjx2eUo8rKMU8ItQNjTwfJTkXr xny6VELEvbvPA5+9QOZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXoaH-0000000BkSb-2wLY; Wed, 07 Feb 2024 20:31:53 +0000 Received: from mailhost.m5p.com ([74.104.188.4]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXoaD-0000000BkPY-1fnD for openwrt-devel@lists.openwrt.org; Wed, 07 Feb 2024 20:31:51 +0000 Received: from m5p.com (mailhost.m5p.com [IPv6:2001:470:1f07:15ff:0:0:0:f7]) by mailhost.m5p.com (8.17.1/8.15.2) with ESMTPS id 417KVHwC041935 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 7 Feb 2024 15:31:23 -0500 (EST) (envelope-from ehem@m5p.com) Received: (from ehem@localhost) by m5p.com (8.17.1/8.15.2/Submit) id 417KVHf4041934; Wed, 7 Feb 2024 12:31:17 -0800 (PST) (envelope-from ehem) Message-Id: <414511fcb30b883565ae724aaf17141b104164ff.1707337693.git.ehem+openwrt@m5p.com> From: Elliott Mitchell Date: Tue, 6 Feb 2024 17:16:41 -0800 Subject: [PATCH v2] scripts: create kernel configuration upgrade script References: <4690477d-e1b7-48e5-83d4-39d8c8e93191@hauke-m.de> <41e9cda304814eca2066a87cf2477c83933358f7.1707269880.git.ehem+openwrt@m5p.com> In-Reply-To: To: Hauke Mehrtens , Jonas Gorski , Felix Baumann Cc: X-Spam-Status: No, score=0.8 required=10.0 tests=DATE_IN_PAST_12_24, KHOP_HELO_FCRDNS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-14) on mattapan.m5p.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240207_123149_686524_0009C197 X-CRM114-Status: GOOD ( 17.27 ) X-Spam-Score: 0.8 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Create a script for automating kernel version changes. This generates a pair of commits which cause history to remain attached to all versioned configuration files. Crucially this makes `git blame` work without needing --find-copies-harder, which is too slow for routine use. This also updates *everything*, which greatly simplifies rebasing patches which effect mu [...] Content analysis details: (0.8 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record 0.8 DATE_IN_PAST_12_24 Date: is 12 to 24 hours before Received: date -0.0 T_SCC_BODY_TEXT_LINE No description available. X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Create a script for automating kernel version changes. This generates a pair of commits which cause history to remain attached to all versioned configuration files. Crucially this makes `git blame` work without needing --find-copies-harder, which is too slow for routine use. This also updates *everything*, which greatly simplifies rebasing patches which effect multiple devices. Signed-off-by: Elliott Mitchell --- v2: Major tweaking. No longer try to do `git merge --ff-only `, but instead advise user to do so. Add usage message. Add statement about strategy. Adjust commit messages. Add advice to run `git bisect --skip` if someone ends up on that commit. --- scripts/kernel_upgrade.pl | 261 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100755 scripts/kernel_upgrade.pl diff --git a/scripts/kernel_upgrade.pl b/scripts/kernel_upgrade.pl new file mode 100755 index 0000000000..b9fe5882a3 --- /dev/null +++ b/scripts/kernel_upgrade.pl @@ -0,0 +1,261 @@ +#!/usr/bin/env perl +######################################################################### +# Copyright (C) 2024 Elliott Mitchell # +# # +# This is free software, licensed under the GNU General Public License # +# v3. See /LICENSE for more information. # +######################################################################### + +use warnings; +use strict; + +use feature 'state'; + +use Digest::SHA qw(sha1_hex); + +# +# Credit to the person who knew about this workable solution: +# +# +# Which originated from: +# +# +# Problem is copying a file in git causes the new file to be created +# without any history. Files can move around without losing their +# history, but that only leaves the history on the new location. +# +# As such this can be solved with two commits. The first commit moves +# files from their old name to their new name. The second merges the +# original commit with the rename commit. The merge commit then has +# files in both locations with the full history. +# +# +# Note, git handles discarded data by garbage collection. When doing +# development on this script, beware this script is an excellent +# garbage generator. Frequent use of `git gc` and `git prune` may be +# needed. +# + + +sub gethead() +{ + open(my $fd, '-|', 'git', 'rev-parse', 'HEAD'); + $_=<$fd>; + chop; + return $_; +} + +sub getlist($$) +{ + my ($target, $from)=@_; + my $ret=[]; + + local $/="\0"; + open(my $fd, '-| :raw :bytes', 'git', 'ls-tree', '-trz', '--full-name', +'--name-only', 'HEAD', '--', $target)||die("failed to read git tree"); + + while(<$fd>) { + chop($_); + push(@$ret, substr($_, 0, -length($from))) +if(substr($_, -length($from)) eq $from); + } + + @$ret=sort({length($b)-length($a)} @$ret); + + return $ret; +} + +# start of interface to git fast-import +my $gitpid; +my $gitfds=[undef, undef]; + +sub startgit() +{ + my $child=[]; + (pipe($child->[0], $gitfds->[0])&&pipe($gitfds->[1], $child->[1])) || +die("pipe() failed"); + binmode($gitfds->[0]); + binmode($gitfds->[1]); + + $gitpid=fork(); + if($gitpid==0) { + close($gitfds->[0]); + close($gitfds->[1]); + + open(STDIN, '<&', $child->[0]); + close($child->[0]); + + open(STDOUT, '>&', $child->[1]); + close($child->[1]); + + exec('git', 'fast-import', '--done'); + die('exec() of git failed'); + } elsif(!$gitpid) { + die('fork() failed'); + } + close($child->[0]); + close($child->[1]); + $gitfds->[0]->autoflush(1); + +} + +sub gitsend +{ + return print({$gitfds->[0]} @_); +} + +sub gitrecv() +{ + return $_=readline(${$gitfds}[1]); +} + +sub gitls($$) +{ + my ($commit, $name)=@_; + local $/="\n"; + + $commit.=' ' if($commit); + gitsend("ls $commit$name\n"); + gitrecv(); + + die('git ls failed') unless(/^([0-8]+)\s+[a-z]+\s+([0-9a-z]+)\s+.+$/); + + return [$1, $2]; +} + +sub gitcommit($$$) +{ + my ($dest, $message, $mark)=@_; + local $/="\n"; + local $|=1; + state $author=undef; + unless($author) { + $author=['', '']; + open(my $user, '-|', 'git', 'config', '--get', 'user.name'); + while(<$user>) { + chomp; + $author->[0].=$_; + } + $author->[0]=[split(/,/, [getpwuid($<)]->[6])]->[0] +unless($author->[0]); + + open(my $email, '-|', 'git', 'config', '--get', 'user.email'); + while(<$email>) { + chomp; + $author->[1].=$_; + } + $author->[1]='anonymous@example.com' unless($author->[1]); + + $author=$author->[0].' <'.$author->[1].'>'; + } + + $_=sha1_hex(time()); + gitsend("commit $_\n"); + gitsend("mark $mark\n"); + gitsend("committer $author ".time()." +0000\n"); + + $_=length($message); + gitsend("data $_\n"); + gitsend($message); + gitsend("from $dest\n"); +} + +sub gitdone() +{ + local $/="\n"; + gitsend("done\n"); + close($gitfds->[0]); + $gitfds->[0]=undef; + + 0 while(waitpid($gitpid, 0)!=$gitpid); + close($gitfds->[1]); + $gitfds->[1]=undef; + + print(STDERR <<~"__GIT_STATUS__") if($?); + WARNING: git returned error exit status: $? + + This likely means `git gc` needs to be run, but the return codes of + `git fast-import` are undocumented. + + __GIT_STATUS__ +} +# end of interface to git fast-import + + +die(<<"__USAGE__") if(@ARGV!=2); +Usage: $0 + +Copies all kernel configuration files and patches from the old version +to the new version. Git history is preserved on the copies by using a +move/merge strategy. Must be run while somewhere inside the git +repository directory, but it does not matter where. +__USAGE__ + +my ($from, $to)=@ARGV; + + +my $target='target/linux'; + +my $start=gethead(); + +my $list=getlist($target, $from); + +die("no files matching \"$from\" found") unless(@$list); + + +startgit(); + +gitcommit($start, <<"__TMP__", ':1'); +kernel: add configs and patches for $to + +This is a special tool-generated commit. + +Copy configuration and patches from $from to $to. + +If you see this commit during a `git bisect` session, the recommended +course of action is to run `git bisect --skip`. +__TMP__ + +foreach my $name (@$list) { + my $new=gitls($start, "$name$from"); + gitsend("M $new->[0] $new->[1] $name$to\n"); + gitsend("D $name$from\n"); +} +gitsend("\n"); + + +gitcommit(':1', <<"__TMP__", ':2'); +kernel: finish update from $from to $to + +This is a special tool-generated commit. + +Merge the add commit into HEAD to create all files with full history. +__TMP__ + +gitsend("merge $start\n"); + +foreach my $name (@$list) { + my $new=gitls($start, "$name$from"); + gitsend("M $new->[0] $new->[1] $name$from\n"); +} +gitsend("\n"); + + +gitsend("get-mark :2\n"); +my $result=gitrecv(); +chomp($result); + +gitdone(); + +print(<<"__END__"); +Result is commit $result. + +Depending on the setup of your development environment, you now likely +want to run one of two commands: + + `git merge --ff-only $result` +Or: + `git branch linux-$to $result` +__END__ + +exit(0);