Blob Blame History Raw
#!/bin/bash
#
#############################################################################
# Copyright (c) 2009 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#############################################################################
#
# print the mainline / stable commit id this kernel is based on

usage()
{
	echo "Usage: $0 -g <mainline kernel git> [-v] [-f] version"
}

die()
{
	echo "$@" >&2
	exit 1
}

git()
{
	(cd "$git"; command git "$@")
}

rev_parse()
{
	git rev-parse --verify "$@"
}

git=
quiet=-q
force=false
options=$(getopt -n "$0" -o g:vfh --long git:,verbose,force,help -- "$@")
if test $? != 0; then
	usage >&2
	exit 1
fi
eval set -- "$options"
while test $# -gt 0; do
	case "$1" in
	-g | --git)
		git=$2
		shift 2
		;;
	-v | --verbose)
		quiet=
		shift
		;;
	-f | --force)
		force=true
		shift
		;;
	--)
		shift
		break
		;;
	esac
done
if test -z "$git" -o $# -ne 1; then
	usage >&2
	exit 1
fi
base=$1

sanity_check()
{
	if ! $force && ! rev_parse 1da177e4c3^0 >/dev/null; then
		die "$git does not look like Linus' kernel repository"
	fi
}

# hardcoded list of a few -bk snapshots used in the suse kernels,
# generated by the bk-commit script
hardcoded_ids=(
	2.6.8-rc4-bk1   783707a1a3fd16d70938a53dd598f6dcd4a6acd2
	2.6.8-rc4-bk2   da58de4bc4757780df70c71195ced7f4611102ca
	2.6.10-rc3-bk10 9d53e4dd111c4bcb1f55961fce39823e340016f6
	2.6.10-rc3-bk14 0233f53fa39d43e2f923c3485502b0461cecc0d7
	2.6.11-rc1-bk8  f21ec4092851e6d7939d7c2a49ac94947840f789
	2.6.11-rc2-bk1  dea9f2aa018246b995d582b8670b362c9cdc69f4
	2.6.11-rc2-bk2  b82f54f7ca17c0ee66bbbc6216275727beef4645
	2.6.11-rc2-bk3  31d881c779c6b98a306c2b0b6063827fb4076102
	2.6.11-rc2-bk4  0bfe221f282f6050b0e999efa9840f67b4fc4d86
	2.6.11-rc2-bk5  c4eadeb71803d271a4b1c09f976824190e4f1f60
	2.6.11-rc2-bk6  c3dfb72cd40074699d7c0940bb0dab976d764952
	2.6.11-rc2-bk7  c923dd8bf3a6002c27ffc23bd11c5f9d86a779ca
	2.6.11-rc2-bk8  31ae6999d2d740b826610547a0fc72382c8c8985
	2.6.11-rc2-bk9  2db1b65b7049001d1451ab1d678da6136b6bbf1c
	2.6.11-rc2-bk10 b389f3821f284617ea17a11ca750d78d1e1cbfc5
	2.6.11-rc3-bk1  b8a743970378fc8e3206b1b4fcdbecc5f37732f5
	2.6.11-rc3-bk2  f69229f575d96cbd0ba28927b734e11fdfe3b51c
	2.6.11-rc3-bk3  1581782eada791b39ffb9de30b8e88b235d87af7
	2.6.11-rc3-bk4  72aa1a6208693f23aec38d8156f60323c2ef61d4
	2.6.11-rc3-bk5  26815161adef49875c03f19536c2c176d638c151
	2.6.11-rc3-bk6  3d23006057f7aaaf01b53305327d542a72626fe5
	2.6.11-rc3-bk7  f24ad951c5114bcfb587b4f348969c002215a2b4
	2.6.11-rc3-bk8  eeca3f3459cae2a1446b1a9b9b1a2c0805471b21
	2.6.11-rc3-bk9  d007c49cc9d6d0e8542320549e74bdd8bf82ccb4
	2.6.11-rc4-bk2  d14d4021344ded14661bf0507098ac14a6c78051
	2.6.11-rc4-bk3  547b39b5be57eee77e574ce55aef1ea3d9151e54
	2.6.11-rc4-bk4  5ff974d98118d600159788c9637d013df8361fa7
	2.6.11-rc4-bk5  9a7a579e7532dc9c8ca995c0bd247bd8f0c9f696
	2.6.11-rc4-bk6  28ad2df3a4f2471f8de501ec9e1eefca42b08f93
	2.6.11-rc4-bk7  175df1ee8b945ea85682f82112766c029327c9d8
	2.6.11-rc4-bk8  fca717c5fe8afad05b62bd84b1b88076e50df89d
	2.6.11-rc4-bk9  c0d51c67f9c398279a95c5a7df387f2d9a586c98
	2.6.11-rc4-bk10 21e99435aee5b96f9db89bc7b03be98ec3be4fbc
	2.6.11-rc4-bk11 9f459dee9e73e90a0a357e5bf53b4bc56282c1da
	2.6.11-rc5-bk1  6101e24910aee642d8bafe2c8dda3ef739353542
	2.6.11-rc5-bk2  8ad4c6043159b2c80e8a44d45cc134af31bb4b01
	2.6.11-rc5-bk3  54077d3c29a1afba9fe5a0c91f4a24d34083e764
	2.6.11-rc5-bk4  a847f56464f9737ddb11a7e41f63b142b237b776

	2.6.11          1274fcd685ec15f10261f48dc990367c2180d589
)

fetch_from()
{
	local url="git://git.kernel.org/pub/scm/linux/kernel/git/$1"
	local version=$2 refspec
	local name=${1%.git}

	name=${name##**/}
	case "$version" in
	*.* )
		version=v${version}
	esac
	base_id=$(rev_parse "$version^{commit}")
	if test -n "$base_id"; then
		return 0
	fi
	refspec=($(git ls-remote "$url" | \
		sed -rn -e 's@.*(refs/tags/v[1-9][^^]*)$@\1:\1@p' -e \
			"s@.*(refs/heads/master)\$@\\1:upstream/$name@p"))
	case "$version" in
	v*)
		if ! echo "${refspec[@]}" | grep -q "/$version:"; then
			# if we are looking for a v* tag and it is not there,
			# do not bother fetching anything
			return 1
		fi
	esac
	git fetch $quiet "$url" "${refspec[@]}"
	base_id=$(rev_parse "$version^{commit}")
	test -n "$base_id"
}

kernel_version()
{
	local version=$1 major minor micro

	version=${version%%[^0-9.]*}
	local IFS=.
	set -- $version
	major=$1
	minor=$2
	micro=$3
	expr "$major" '*' 65536 '+' "0$minor" '*' 256 + "0$micro"
}

get_base_id()
{
	local base=$1

	base_id=
	for ((i = 0; i < ${#hardcoded_ids[@]}; i += 2)); do
		if test "$base" = "${hardcoded_ids[i]}"; then
			base_id=${hardcoded_ids[i+1]}
			fetch_from "tglx/history.git" "$base_id"
			return
		fi
	done
	if test "$base" = "2.6.12git9"; then
		base=2.6.12-git9
	fi
	case "$base" in
	2.6.*-git*)
		local url="http://www.kernel.org/pub/linux/kernel/v2.6/snapshots"
		base_id=$(curl -f -s "$url/patch-$base.id")
		if test -z "$base_id"; then
			base_id=$(curl -f -s "$url/old/patch-$base.id")
		fi
		if test -n "$base_id"; then
			fetch_from "torvalds/linux-2.6.git" "$base_id"
			return
		fi
	esac
	if test $(kernel_version "$base") -le $(kernel_version 2.6.11); then
		fetch_from "tglx/history.git" "$base"
		if test -n "$base_id"; then
			return
		fi
	fi
	local is_stable=false
	case "$base" in
	[3-9].*.0-rc* | [1-9][0-9].*.0-rc*)
		base=${base/.0-/-}
		;;
	[3-9].*.0 | [1-9][0-9].*.0)
		base=${base%.0}
	esac
	case "$base" in
	2.6.11.*)
		# The history tree has 2.6.11.y up to 2.6.11.5; but there 
		# is no tree for the rest and I don't plan to create any
		base_id=0
		;;
	2.6.*-rt*)
		fetch_from "tip/linux-2.6-tip.git"
		;;
	2.6.*.* | [3-9].*.* | [1-9][0-9].*.* )
		is_stable=true
		if ! fetch_from "stable/linux-stable.git" "$base"; then
			fetch_from "longterm/linux-${base%.*}.y.git" "$base"
		fi
		;;
	2.6.* | [3-9].* | [1-9][0-9].*)
		fetch_from "torvalds/linux.git" "$base"
		;;
	*)
		die "Unexpected kernel version $base"
	esac
	if test -z "$base_id"; then
		case "$base" in
		2.6.*-rt*)
			# not all rt releases are tagged in tip
			base_id=0
			;;
		*-rc* )
			if $is_stable; then
				# RCs for stable are typically not in git
				base_id=0
			fi
			;;
		esac
	fi
}

sanity_check >&2
get_base_id "$base" >&2
if test -z "$base_id"; then
	die "Cannot match $base to commit id"
fi
if test "$base_id" = 0; then
	echo "Warning: no id for $base" >&2
elif ! rev_parse "$base_id^{commit}" >/dev/null; then
	die "internal error: got bogus id $base_id for $base"
fi
echo "$base_id"