Home Home > GIT Browse
summaryrefslogtreecommitdiff
blob: c44704959e82b89d8013967745426ef4571a6111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl -w
use strict;
use File::Basename qq(basename);
use Git;
use Storable qw(store);
use Term::ANSIColor qw(colored);

if (@ARGV < 2) {
	print "Usage: $0 suse_machine stable_version\n";
	exit 1;
}

my $machine = shift;
my $stable_ver = shift;
my $old_version;
my $new_version;
my $bnc;

my %bnc_map = (
	'3.12' => '1012620',
	'4.1' => '1023711',
	'4.4' => '1012382',
	'4.12' => '1060662',
	'other' => '1012628',
);

if ($stable_ver =~ /^v?(([3-9]\.[0-9]+)\.([0-9]+))$/) {
	$new_version = $1;
	$old_version = $2;
	$bnc = $bnc_map{$old_version};
	if ($3 ne 1) {
		$old_version .= '.' . ($3 - 1);
	}
} else {
	die "cannot understand stable version $stable_ver";
}

if (!defined $bnc) {
	print colored("Kernel version not found in the map, assuming Tumbleweed\n", 'red');
	$bnc = $bnc_map{'other'};
}

my $patchpar = '/dev/shm';
my $patchdir = "patches-$new_version";
my $patchpath = "$patchpar/$patchdir";
my $idsfile = "$patchpath/ids";
if (!mkdir $patchpath) {
	die "$patchpath already exists";
}

my $range = "v$old_version..v$new_version";
my $repo = Git->repository();
my @revs = $repo->command('rev-list', '--reverse', $range);
my %ids;
my $counter = 1;
my @to_delete;
my $sha_re = qr/[0-9a-f]{40}/;

foreach my $rev (@revs) {
	my ($filename, @commit_log) = $repo->command('show', '--no-patch',
		'--format=%f%n%B', $rev);

	my $cont = 0;
	my @shas;
	my @unmatched_shas;

	foreach (@commit_log) {
		if ($cont) {
			if (/^\s+($sha_re)\s*[\])]$/) {
				push @shas, $1;
				$cont = 0;
				next;
			}
		}
		if (/^commit ($sha_re) upstream\.?$/ ||
			/^[\[(]\s*[Uu]pstream commit ($sha_re)\s*[\])]$/ ||
			/^[uU]pstream commit ($sha_re)\.$/ ||
			/^This is a backport of ($sha_re)$/ ||
			/^\(cherry picked from commit ($sha_re)\)$/) {
			push @shas, $1;
		} elsif (/^[\[(]\s*[Uu]pstream commits ($sha_re)\s+and\s*$/) {
			push @shas, $1;
			$cont = 1;
		} elsif (/^(Fixes:|This reverts commit) $sha_re(?: \(".*)?\.?$/ ||
			/^Link: .*lkml/) {
			# ignore
		} elsif (/\b$sha_re\b/) {
			push @unmatched_shas, $_;
		}
	}

	# better than nothing
	if (!scalar @shas) {
		push @shas, $rev;
	}

	my @patch = $repo->command('format-patch', '--stdout',
		'--signoff', '--no-renames',
		'-1', $rev,
		'--add-header', "References: bnc#$bnc",
		'--add-header', "Patch-mainline: $new_version",
		(map { ('--add-header', "Git-commit: $_") } @shas));

	# drop From
	shift(@patch) =~ /^From/ or die "From line is not the first one?";

	my $newname = sprintf("$new_version-%03d-%s", $counter, $filename);
	# 57 is what git-format-patch uses
	$newname =~ s/^(.{1,57}).*$/$1.patch/;
	my $newpath = "$patchpath/$newname";

	open(PATCH, ">$newpath") or die "cannot output to $newpath";
	print PATCH join "\n", @patch;
	print PATCH "\n";
	close PATCH;

	$ids{$newname} = [ @shas ];

	$rev =~ /.{12}/;
	print colored($&, "yellow"), " -> $newname\n";
	foreach (@shas) {
		/.{12}/;
		print "\tUpstream SHA: ", colored("$&\n", "yellow");
	}
	foreach (@unmatched_shas) {
		print colored("\tUNMATCHED SHA:", 'bold red'), " $_\n";
	}

	push @to_delete, $newpath;
	$counter++;
}

store(\%ids, $idsfile) or die "cannot write $idsfile";
push @to_delete, $idsfile;

system("tar -cC $patchpar $patchdir|ssh -C $machine -o StrictHostKeyChecking=no 'tar -xC $patchpar'") == 0 ||
	die "ssh didn't start";
print "Written patches and ids to $machine:$patchpath\n";

unlink(@to_delete) or print STDERR "cannot delete some temp files\n";
rmdir("$patchpath") or print STDERR "cannot remove $patchpath\n";

0;