Skip to content
Snippets Groups Projects
dim 44.6 KiB
Newer Older
Simona Vetter's avatar
Simona Vetter committed
#!/bin/bash

# Copyright © 2012-2016 Intel Corporation
Simona Vetter's avatar
Simona Vetter committed
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# Authors:
#    Daniel Vetter <daniel.vetter@ffwll.ch>
#    Jani Nikula <jani.nikula@intel.com>
Simona Vetter's avatar
Simona Vetter committed

# drm-intel-next maintainer script

# fail on any goof-up
Simona Vetter's avatar
Simona Vetter committed

# User configuration. Set in environment or configuration file. See
# dimrc.sample for an example.
#

# dim configuration file
DIM_CONFIG=${DIM_CONFIG:-$HOME/.dimrc}
if [ -r $DIM_CONFIG ]; then
	# shellcheck source=/dev/null
	. $DIM_CONFIG
fi

# prefix for repo directories
DIM_PREFIX=${DIM_PREFIX:-$HOME/linux}

# main maintainer repo under $DIM_PREFIX
DIM_DRM_INTEL=${DIM_DRM_INTEL:-src}

# name of the $drm_intel_ssh remote within $DIM_DRM_INTEL
Simona Vetter's avatar
Simona Vetter committed
DIM_DRM_INTEL_REMOTE=${DIM_DRM_INTEL_REMOTE:-drm-intel}
# mail user agent. must support a subset of mutt(1) command line options:
# usage: $DIM_MUA [-s subject] [-i file] [-c cc-addr] to-addr [...]
DIM_MUA=${DIM_MUA:-mutt}

# make options (not used for C=1)
DIM_MAKE_OPTIONS=${DIM_MAKE_OPTIONS:--j20}

# command to run after dim apply
DIM_POST_APPLY_ACTION=${DIM_POST_APPLY_ACTION:-}

# greetings pull request template
DIM_TEMPLATE_HELLO=${DIM_TEMPLATE_HELLO:-$HOME/.dim.template.hello}

# signature pull request template
DIM_TEMPLATE_SIGNATURE=${DIM_TEMPLATE_SIGNATURE:-$HOME/.dim.template.signature}

# dim pull-request tag summary template
DIM_TEMPLATE_TAG_SUMMARY=${DIM_TEMPLATE_TAG_SUMMARY:-$HOME/.dim.template.tagsummary}

today=$(date +%Y-%m-%d)
Simona Vetter's avatar
Simona Vetter committed

drm_intel_ssh=ssh://git.freedesktop.org/git/drm-intel
drm_tip_ssh=ssh://git.freedesktop.org/git/drm-tip
drm_upstream_git=git://people.freedesktop.org/~airlied/linux
linux_upstream_git=git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Simona Vetter's avatar
Simona Vetter committed

# email aliases
addr_drm_maintainer="Dave Airlie <airlied@gmail.com>"
addr_intel_gfx_maintainer1="Daniel Vetter <daniel.vetter@ffwll.ch>"
addr_intel_gfx_maintainer2="Jani Nikula <jani.nikula@linux.intel.com>"
addr_intel_gfx="intel-gfx@lists.freedesktop.org"
addr_dri_devel="dri-devel@lists.freedesktop.org"
addr_intel_qa="\"Christophe Prigent\" <christophe.prigent@intel.com>"
# integration configuration
integration_config=nightly.conf

function read_integration_config
{
	# clear everything first to allow configuration reload
	unset drm_tip_repos drm_tip_config
	declare -g -A drm_tip_repos
	declare -g -a drm_tip_config

	if [ -r $DIM_PREFIX/drm-rerere/$integration_config ]; then
		# shellcheck source=/dev/null
		source $DIM_PREFIX/drm-rerere/$integration_config
	fi

	dim_branches=
	for conf in "${drm_tip_config[@]}"; do
		local repo branch override
Jani Nikula's avatar
Jani Nikula committed
		read -r repo branch override <<< $conf
		if [[ "$repo" = "drm-intel" || "$repo" = "drm-misc" ]]; then
			dim_branches="$dim_branches $branch"
		fi
	done
}
read_integration_config

	echo "$dim: $*" >&2
Simona Vetter's avatar
Simona Vetter committed
function warn_or_fail
{
	if [[ $FORCE ]] ; then
		echoerr "WARNING: $1, but continuing"
Simona Vetter's avatar
Simona Vetter committed
	else
		echoerr "ERROR: $1, aborting"
function pause
{
	read -rsp "Press any key to continue..." -n1 key2
	echo
}

#
# Command line options.
#

DRY_RUN=
INTERACTIVE=
DRY=
FORCE=
HELP=

while getopts hdfis opt; do
	case "$opt" in
		d)
			DRY_RUN=--dry-run
			DRY=echo
			;;
Simona Vetter's avatar
Simona Vetter committed
		i)
Simona Vetter's avatar
Simona Vetter committed
			;;
		s)
			# FIXME: transitional, do unconditionally at the top
			# when there are no more errors about unbound variables
			set -u
			;;
			echoerr "See '$dim help' for more information."
shift $((OPTIND - 1))

# first positional argument is the subcommand
if [ -n "$HELP" ] || [ "$#" = "0" ]; then
else
    subcommand="$1"
    shift
fi
# generic usage to be used for ${1:?$usage} style argument references
usage="Missing arguments(s) for '$dim $subcommand'. See '$dim help' for usage."

# Make sure we use 'dim_foo' within dim instead of 'dim foo'.
if [[ -n "${__dim_running:-}" ]]; then
	echoerr "INTERNAL ERROR: do not recurse back to dim"
	exit 1
fi
export __dim_running=1

if [ "$subcommand" != "setup" ] && [ "$subcommand" != "help" ] && [ "$subcommand" != "usage" ]; then
	for d in $DIM_PREFIX $DIM_PREFIX/$DIM_DRM_INTEL $DIM_PREFIX/drm-rerere $DIM_PREFIX/drm-tip; do
		if [ ! -d $d ]; then
			echoerr "$d is missing, please check your configuration and/or run dim setup"
#
# Only function and alias definitions until the subcommand handling at the end.
#

#
# Variable naming convetion:
#
# repo:
#	symbolic git repository name from $integration_config
# remote:
#	local remote name in the git repository for the current path
# branch:
#	git branch name - dim assumes that the remote and local name match
# url:
#	url to a repo, using ssh:// protocol
# git_url:
#	url to a repo, but using anonymous git:// protocol
#
# The below functions map between these.
#

function url_to_remote # url
	if [[ -z "$url" ]]; then
		echoerr "$0 without url"
	remote=$(git remote -v | grep -m 1 "$url" | cut -f 1)
		git_url=$(echo $url | sed -e 's/git\./anongit./' -e 's/ssh:/git:/')
Simona Vetter's avatar
Simona Vetter committed
		remote=$(git remote -v | grep -m 1 "$git_url" | cut -f 1)

		if [[ -z "$remote" ]]; then
			echoerr "No git remote for url $url or $git_url found in $(pwd)"
			echoerr "Please set it up using:"
			echoerr "    $ git remote add <name> $url"
			echoerr "with a name of your choice."
			return 1
		fi
function branch_to_remote # branch
{
	local branch remote
	branch=$1
	remote=$(git rev-parse --abbrev-ref --symbolic-full-name "$branch@{upstream}")
	remote=${remote%%/*}

	echo $remote
}

function repo_to_remote # repo
{
	url_to_remote ${drm_tip_repos[$1]}
}

function branch_to_repo # branch
{
	for conf in "${drm_tip_config[@]}"; do
		local repo branch override
Jani Nikula's avatar
Jani Nikula committed
		read -r repo branch override <<< $conf
		if [[ "$branch" == "$1" ]] ; then
function dim_uptodate
{
	local using

	using="${BASH_SOURCE[0]}"

	if [[ ! -e "$using" ]]; then
		echoerr "could not figure out the version being used ($using)."
	fi

	if [[ ! -e "$DIM_PREFIX/maintainer-tools/.git" ]]; then
		echoerr "could not find the upstream repo for $dim."
	if ! git --git-dir=$DIM_PREFIX/maintainer-tools/.git show "@{upstream}:dim" |\
			diff "$using" - >& /dev/null; then
		echoerr "not running upstream version of the script."
Simona Vetter's avatar
Simona Vetter committed
function git_fetch_helper # remote
{
	local remote

	remote=$1

	if ! git fetch --prune -q $remote ; then
Simona Vetter's avatar
Simona Vetter committed
		# old git versions returned 128 if there was nothing to fetch
		if [[ $? -ne "128" ]] ; then
			echoerr "Failed to fetch $remote"
			return 1
		fi
	fi
}

function git_is_current_branch # branch
{
	git branch --list $1 | grep -q '\*'
}

function git_branch_exists # branch
{
	if [[ "$(git branch --list $1)" == "" ]] ; then
function git_committer_email
{
	if ! committer_email=$(git config --get user.email) ; then
		committer_email=$EMAIL
	fi

	echo $committer_email
}

# get message id from file
# $1 = file
message_get_id ()
{
	python <<EOF
from email.parser import Parser
headers = Parser().parse(open('$1', 'r'))
message_id = headers['message-id']
if message_id is not None:
    print(message_id.strip('<>'))
EOF
}

message_print_body ()
{
	python2 <<EOF
import email

def print_part(part):
    mtype = part.get_content_maintype()
    if mtype == 'text':
        print(part.get_payload(decode=True))

def print_msg(file):
    msg = email.message_from_file(file)
    if msg.is_multipart():
        for part in msg.get_payload():
            print_part(part)
    else:
        print_part(msg)

print_msg(open('$1', 'r'))
EOF
}

# append all arguments as tags at the end of the commit message of HEAD
function dim_commit_add_tag
	for arg; do
		# the first sed deletes all trailing blank lines at the end
		git log -1 --pretty=%B | \
			sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' | \
			sed "\$a${arg}" | \
			git commit --amend -F-
	done
# $1: branch [optional]
function git_find_tip
{
	git log $1 -1 --format=%H --grep="^drm-tip: .* integration manifest$"
}

# $1: branch [optional]
function dim_retip
{
	local branch upstream remote

	branch="$1"

	if [ -n "$branch" ] ; then
		shift
	else
		branch=$(git symbolic-ref --short HEAD)
	fi

	remote=$(url_to_remote $drm_tip_ssh)
	upstream=$(git_find_tip "$branch")

	if [[ -z "$upstream" ]]; then
		echoerr "$branch is not based on drm-tip"
		return 1
	fi

	git rebase --onto $remote/drm-tip $upstream $branch "$@"
}

# update for-linux-next and for-linux-next-fixes branches
function update_linux_next # branch next next-fixes fixes
Simona Vetter's avatar
Simona Vetter committed
{
	local branch linux_next linux_next_fixes linux_fixes repo remote

	cd $DIM_PREFIX/drm-tip
	branch=$1
	linux_next=$2
	linux_next_fixes=$3
	linux_fixes=$4
	repo=$(branch_to_repo $branch)
	if [[ $repo != $(branch_to_repo $linux_next) ]] ; then
	remote=$(repo_to_remote $repo)
Simona Vetter's avatar
Simona Vetter committed
	git_fetch_helper $remote
	# always update drm-intel-fixes
	echo -n "Pushing $linux_fixes to for-linux-next-fixes... "
	git push $DRY_RUN $remote +$remote/$linux_fixes:for-linux-next-fixes # >& /dev/null
	if git merge-base --is-ancestor $remote/$linux_next_fixes $remote/$linux_fixes ; then
		# -fixes has caught up to dinf, i.e. we're out of the merge
		# window. Push the next queue.
		echo -n "Out of merge window. Pushing $linux_next to for-linux-next... "
		git push $DRY_RUN $remote +$remote/$linux_next:for-linux-next >& /dev/null
Simona Vetter's avatar
Simona Vetter committed
	else
		# dinf is ahead of -fixes, i.e. drm-next has already closed for
		# the next merge window and we've started to gather new fixes
		# for the current -next cycle. Push dinf
		echo -n "Pushing $linux_next_fixes to for-linux-next... "
		git push $DRY_RUN $remote +$remote/$linux_next_fixes:for-linux-next >& /dev/null
Simona Vetter's avatar
Simona Vetter committed
	fi
}

function check_conflicts # tree
Simona Vetter's avatar
Simona Vetter committed
{
Simona Vetter's avatar
Simona Vetter committed
	if git diff | grep -q '\(<<<<<<<\|=======\|>>>>>>>\||||||||\)' ; then
		# we need an empty line to make it look pretty
		echoerr ""
		echoerr "FAILURE: Could not merge $1"
		echoerr "See the section \"Resolving Conflicts when Rebuilding drm-tip\""
		echoerr "in the drm-intel.rst documentation for how to handle this situation."
Simona Vetter's avatar
Simona Vetter committed
		exit 1
	fi
	true
}

function rr_cache_dir
{
Simona Vetter's avatar
Simona Vetter committed
	if [ -d $DIM_PREFIX/drm-tip/.git/ ] ; then
		echo $DIM_PREFIX/drm-tip/.git/rr-cache/
	else
		echo $DIM_PREFIX/$DIM_DRM_INTEL/.git/rr-cache/
	fi
}

function update_rerere_cache
{
	echo -n "Updating rerere cache... "

	mkdir $(rr_cache_dir) &> /dev/null || true
	cp rr-cache/* $(rr_cache_dir) -r --preserve=timestamps
	cd - > /dev/null

	echo "Done."
}

function commit_rerere_cache
{
	echo -n "Updating rerere cache... "

	cd $DIM_PREFIX/drm-rerere/
	if git_is_current_branch rerere-cache ; then
		remote=$(branch_to_remote rerere-cache)

		git pull >& /dev/null
		rm $(rr_cache_dir)/rr-cache -Rf &> /dev/null || true
		cp $(rr_cache_dir)/* rr-cache -r --preserve=timestamps
		git add ./*.patch >& /dev/null || true
		for file  in $(git ls-files); do
			if ! git log --since="60 days ago" --name-only -- $file | grep $file &> /dev/null; then
				git rm $file &> /dev/null
				echo deleting $file
			fi
		done
		find rr-cache/ -ctime -1 -type f -print0 | xargs -0 git add > /dev/null
		git rm rr-cache/rr-cache &> /dev/null || true
		if git commit -m "$time: $integration_branch rerere cache update" >& /dev/null; then
			echo -n "New commit. "
		else
			echo -n "Nothing changed. "
		fi
		echo -n "Pushing rerere cache... "
		git push $DRY_RUN $remote HEAD >& /dev/null && echo "Done."
	else
		echo "Fail: Branch setup for the rerere-cache is borked."
		exit 1
	fi
Simona Vetter's avatar
Simona Vetter committed
function dim_revert_rerere
{
	cd $DIM_PREFIX/drm-rerere/
	rm $(rr_cache_dir)/* -Rf
dim_alias_rebuild_nightly=rebuild-tip
function dim_rebuild_tip
Simona Vetter's avatar
Simona Vetter committed
{
	local integration_branch specfile time first rerere repo url remote
Simona Vetter's avatar
Simona Vetter committed

	integration_branch=drm-tip
	specfile=$(mktemp)
	time="$(date --utc +%Yy-%mm-%dd-%Hh-%Mm-%Ss) UTC"
	first=1

	rerere=$DIM_PREFIX/drm-rerere
	if git status --porcelain | grep -q -v "^[ ?][ ?]"; then
		warn_or_fail "integration configuration file $integration_config not commited"
	update_rerere_cache
	echo -n "Reloading $integration_config... "
	echo "Done."
	cd $DIM_PREFIX/$integration_branch
	if ! git_is_current_branch $integration_branch ; then
		echo "Branch setup for the integration repo is borked"
		exit 1
	fi

	remote=$(url_to_remote $drm_tip_ssh)
	echo -n "Fetching drm-tip (local remote $remote)... "
Simona Vetter's avatar
Simona Vetter committed
	git_fetch_helper $remote
	for repo in "${!drm_tip_repos[@]}"; do
		url=${drm_tip_repos[$repo]}
		remote=$(url_to_remote $url)
		echo -n "Fetching $repo (local remote $remote)... "
Simona Vetter's avatar
Simona Vetter committed
		git_fetch_helper $remote
Simona Vetter's avatar
Simona Vetter committed
	# merge -fixes
	for conf in "${drm_tip_config[@]}"; do
		local branch override sha1 fixup_file

Jani Nikula's avatar
Jani Nikula committed
		read -r repo branch override <<< $conf
		url=${drm_tip_repos[$repo]}
		remote=$(url_to_remote $url)
		sha1=$remote/$branch
		echo -n "Merging $repo (local remote $remote) $branch... "
		if [[ -n "$override" ]]; then
			sha1=$override
			echo -n "Using override sha1: $sha1... "
Simona Vetter's avatar
Simona Vetter committed
		if [ $first == 1 ] ; then
			git reset --hard $sha1 &> /dev/null
			echo "Reset. Done."
Simona Vetter's avatar
Simona Vetter committed
			first=0
		elif git merge --rerere-autoupdate --ff-only $sha1 >& /dev/null ; then
			# nothing to do if just fast-forward
			echo "Fast-forward. Done."
Simona Vetter's avatar
Simona Vetter committed
		else
			fixup_file=$rerere/$repo-${branch//\//-}-fixup.patch
Simona Vetter's avatar
Simona Vetter committed
			echo $fixup_file > .fixup_file_path

			git merge --rerere-autoupdate --no-commit $sha1 >& /dev/null || true
			# normalize conflict markers
			if git grep -l '^>>>>>>> ' &> /dev/null ; then
				git grep -l '^>>>>>>> ' | xargs sed -e "s|^>>>>>>> .*$|>>>>>>> $repo\/$branch|" -i
Simona Vetter's avatar
Simona Vetter committed
			if [ -f $fixup_file ] ; then
				echo -n "Applying manual fixup patch for $integration_branch merge... "
Simona Vetter's avatar
Simona Vetter committed
				patch -p1 -i $fixup_file
Simona Vetter's avatar
Simona Vetter committed
			fi
			check_conflicts "$repo/$branch"
Simona Vetter's avatar
Simona Vetter committed
			git add -u

			# because we filter out fast-forward merges there will
			# always be something to commit
			git commit --no-edit --quiet
			echo "Done."
Simona Vetter's avatar
Simona Vetter committed
		fi

		echo -e "$repo $branch $(git rev-parse $sha1)\n\t$(git log -1 $sha1 --pretty=format:%s)" >> $specfile
Simona Vetter's avatar
Simona Vetter committed

		$INTERACTIVE
Simona Vetter's avatar
Simona Vetter committed
	done

	echo -n "Adding integration manifest $integration_branch: $time... "
Simona Vetter's avatar
Simona Vetter committed
	mv $specfile integration-manifest
	git add integration-manifest
	git commit --quiet -m "$integration_branch: $time integration manifest"
	echo "Done."
Simona Vetter's avatar
Simona Vetter committed

	remote=$(url_to_remote $drm_tip_ssh)
	echo -n "Pushing $integration_branch... "
	git push $DRY_RUN $remote +HEAD >& /dev/null && echo "Done."
Simona Vetter's avatar
Simona Vetter committed

	commit_rerere_cache
Simona Vetter's avatar
Simona Vetter committed
}

# additional patch checks before pushing, e.g. for r-b tags
function checkpatch_commit_push
{
	local sha1

	sha1=$1

	# check for a-b/r-b tag
	if git show -s $sha1 | grep -qi '\(reviewed\|acked\)\S*-by:'  ; then
		return
	fi

	# check for committer != author
	if [[ $(git show -s $sha1 --format="format:%ce") != $(git show -s $sha1 --format="format:%ae") ]]; then
		return
	fi

	warn_or_fail "$sha1 is lacking mandatory review"
}

# push branch $1, rebuild drm-tip. the rest of the arguments are passed to git
function dim_push_branch
	local branch remote committer_email
	remote=$(branch_to_remote $branch)
	committer_email=$(git_committer_email)

	for sha1 in $(git rev-list $branch@{u}..$branch --committer="$committer_email" --no-merges) ; do
		checkpatch_commit_push $sha1
	done

	git push $DRY_RUN $remote $branch "$@"
	update_linux_next $branch drm-intel-next-queued drm-intel-next-fixes drm-intel-fixes
	update_linux_next $branch drm-misc-next drm-misc-next-fixes drm-misc-fixes

	dim_rebuild_tip
Jani Nikula's avatar
Jani Nikula committed
dim_alias_pq=push-queued
function dim_push_queued
{
	dim_push_branch drm-intel-next-queued "$@"
}

dim_alias_pnf=push-next-fixes
function dim_push_next_fixes
{
	dim_push_branch drm-intel-next-fixes "$@"
}

dim_alias_pf=push-fixes
function dim_push_fixes
{
	dim_push_branch drm-intel-fixes "$@"
}

# ensure we're on branch $1, and apply patches. the rest of the arguments are
# passed to git am.
dim_alias_ab=apply-branch
dim_alias_sob=apply-branch
function dim_apply_branch
	local branch file message_id committer_email patch_from sob rv
	file=$(mktemp)
	message_id=$(message_get_id $file)
	committer_email=$(git_committer_email)
	patch_from=$(grep "From:" "$file" | head -1)
	if [[ "$patch_from" != *"$committer_email"* ]] ; then
	git am --scissors -3 $sob "$@" $file
	if [ -n "$message_id" ]; then
		dim_commit_add_tag "Link: https://patchwork.freedesktop.org/patch/msgid/$message_id"
		echoerr "WARNING: No message-id found in the patch file."
		rv=1
	if ! checkpatch_commit HEAD; then
		rv=1
	fi
	if ! check_maintainer $branch HEAD; then
		rv=1
	fi
function dim_apply_pull
{
	local branch file message_id pull_branch rv

	branch=${1:?$usage}
	file=$(mktemp)

	assert_branch $branch
	assert_repo_clean

	cat > $file

	pull_branch=$(sed -e '0,/git repository at:$/d' $file | head -n 2 | tail -n 1)

	echo $pull_branch

	git pull $pull_branch

	message_id=$(message_get_id $file)

	git commit --amend -s --no-edit
	if [ -n "$message_id" ]; then
		dim_commit_add_tag "Link: https://patchwork.freedesktop.org/patch/msgid/$message_id"
	else
		echoerr "WARNING: No message-id found in the patch file."
		rv=1
	fi


	eval $DRY $DIM_POST_APPLY_ACTION
Simona Vetter's avatar
Simona Vetter committed
function dim_backmerge
{
	local branch upstream patch_file

	branch=${1:?$usage}
	upstream=${2:?$usage}

	cd $DIM_PREFIX/drm-tip
	tip_remote=$(url_to_remote $drm_tip_ssh)
	git fetch -q $tip_remote || true

	if ! git merge-base --is-ancestor $upstream $tip_remote/drm-tip ; then
		echoerr "Upstream $upstream not merged into drm-tip, aborting."
		echoerr "Please make sure any backmerge is tested in drm-tip,"
		echoerr "to give all the CI bots some time to find bugs."
		exit 1
	fi

	assert_branch $branch
	assert_repo_clean

	git merge --rerere-autoupdate --no-commit $upstream >& /dev/null || true

	if [[ -d .git ]]; then
		patch_file=".git"
	else
		patch_file=$(cut -d ' ' -f 2 .git)
	fi
	patch_file=$patch_file/MERGE_MSG


	cat > $patch_file <<-HERE
		Merge $upstream into $branch

		<Explain here why you've done the backmerge, e.g. which patches
		 or which driver pull request you need to be able to merge
		 \$feature_work from \$author.>

		HERE

	if git diff | grep -q '\(<<<<<<<\|=======\|>>>>>>>\||||||||\)' ; then
		echoerr "Conflicts found while merging $upstream into $branch."
		echoerr "This should only happen when git rerere gets confused"
		echoerr "or if there's a manual fixup patch in drm-rerere."
		echoerr "Please proceed with extreme caution."
		echoerr "Once the conflict is resolved, commit it with"
		echoerr "   git commit -a"
	fi

	git add -u
	git commit -s
}

function dim_add_link
{
	local branch file message_id

	shift
	file=$(mktemp)

	assert_branch $branch
	assert_repo_clean

	cat > $file

	message_id=$(message_get_id $file)

	rm -f $file

	if [[ -n "$message_id" ]]; then
		dim_commit_add_tag "Link: https://patchwork.freedesktop.org/patch/msgid/$message_id"
	else
		echoerr "No message-id found in the patch file."
	fi
}

function dim_add_link_queued
{
	dim_add_link drm-intel-next-queued "$@"
}

function dim_add_link_fixes
{
	dim_add_link drm-intel-fixes "$@"
}

function dim_add_link_next_fixes
{
	dim_add_link drm-intel-next-fixes "$@"
}

dim_alias_aq=apply-queued
function dim_apply_queued
{
	dim_apply_branch drm-intel-next-queued "$@"
}

dim_alias_af=apply-fixes
function dim_apply_fixes
{
	dim_apply_branch drm-intel-fixes "$@"
}

dim_alias_anf=apply-next-fixes
function dim_apply_next_fixes
{
	dim_apply_branch drm-intel-next-fixes "$@"
}

function commit_list_references
	local commit remote log
	cd $DIM_PREFIX/drm-tip
	remote=$(url_to_remote $drm_tip_ssh)
	git fetch -q $remote || true

	commit="$1"
	log=$(mktemp)

	git log --regexp-ignore-case --grep="${commit:0:8}" --oneline \
	    $commit..$remote/drm-tip > $log

	if [ "$(cat $log)" != "" ]; then
		echo "Commit ${commit:0:8} is referenced by later commits:"
		sed 's/^/\t/' < $log
	fi

	rm -f $log

	cd - >/dev/null
}

function dim_cherry_pick
{
	local commit
	commit=$(git rev-parse ${1:?$usage})
Simona Vetter's avatar
Simona Vetter committed
	git_fetch_helper $remote
	$DRY git cherry-pick -s -x -e $commit
function git_list_fixes
{
	git log --reverse --format=format:%H --regexp-ignore-case \
	    --grep="^Cc:.*drm-intel-fixes@lists\.freedesktop\.org" \
	    --grep="^Cc:.*stable@vger\.kernel\.org" \
	    --grep="^Fixes: " \
	    "$@"
}

function dim_cherry_pick_branch
{
	local branch log fail_log needed have_fixes

	log=$(mktemp)
	fail_log=$(mktemp)

	# Look for commits in dinq tagged as fixes.
	for commit in $(git_list_fixes $DIM_DRM_INTEL_REMOTE/$branch..$DIM_DRM_INTEL_REMOTE/drm-intel-next-queued -- drivers/gpu/drm/i915); do
		echo -n "Considering $(dim_cite $commit)... "

		# Look at history for already cherry-picked fixes.
		# Note: use *local* branches to account for unpushed commits.
		git log drm-intel-fixes --format=format:%h --after=6months \
		    --grep="cherry picked .* $commit" > $log
		if [ "$(cat $log)" = "" ]; then
			git log drm-intel-next-fixes --format=format:%h --after=6months \
			    --grep="cherry picked .* $commit" > $log
		fi

		if [ "$(cat $log)" != "" ]; then
			echo "Already backported as $(tr "\n" " " < $log). OK."
			continue
		fi

		have_fixes=
		needed=
		for fixes in $(git show -s $commit | grep -i "^    Fixes: *[0-9a-fA-F]" | sed 's/^ *[Ff]ixes: *\([0-9a-fA-F]\+\).*/\1/'); do
			have_fixes=1
			fixes=$(git log -1 --format=format:%H $fixes 2>/dev/null || true)
			if [[ -z "$fixes" ]]; then
				continue

			# FIXME: see if the commit to be fixed has been
			# backported!
			echo -n "Fixes: $(dim_cite $fixes). "
			if [[ "$(git merge-base $branch $fixes)" = "$fixes" ]]; then
				needed=1
			fi