<commit_before>#!/bin/bash
#
# This script tries to merge OpenZFS commits to ZoL.
#
# Instruction:
# 
# Repository setup must be similar with openzfs-tracking.sh
# requirements.
#
# Repository setup for valid compilation check:
#	sh autogen.sh
#	./configure --enable-debug
#
# mandatory git settings:
#	[merge]
#   	renameLimit = 999999
#	[user]
#       email = <EMAIL>
#       name = <NAME>
#
# Copyright (c) 2016 <NAME>. All rights reserved.
#

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

REPOSITORY_PATH='.'
TMP_FILE='/tmp/gittmpmessage.txt'

# list with potential OpenZFS commits
UNPORTED_COMMITS_FILE=$SCRIPTDIR'/hashes.txt'

# Next files will generate automatically
# 	list with commits git can't merge automatically
EXCEPTIONS_GIT=$SCRIPTDIR'/exceptions.txt'
#	list with commits which can't be compiled without errors
EXCEPTIONS_COMPILE=$SCRIPTDIR'/uncompiled.txt'
#	list with commits which has cstyle error
EXCEPTIONS_CSTYLE=$SCRIPTDIR'/uncstyled.txt'
#	list with merged
EXCEPTIONS_MERGED=$SCRIPTDIR'/merged.txt'

LGREEN='\033[1;32m'		#  ${LGREEN}
NORMAL='\033[0m'		#  ${NORMAL} 
COUNT_MERGED=0
LIST_MERGED=
COUNT_COMPILE=0
LIST_COMPILE=
COUNT_CSTYLE=0
LIST_CSTYLE=
PUSH_NEW_BRANCH=0
FAIL_STYLE=0


usage() {
cat << EOF
USAGE:
$0 [-hp] [-d directory] [-i commits.txt] [-c commit hash] [-g commit hash]

DESCRIPTION:
	Auto merge OpenZFS commits to ZFS on Linux git
	repositories.
	Result - git branch with name 'autoport-oz#issue'

OPTIONS:
	-h		Show this message
	-d directory	Git repo with openzfs and zfsonlinux remotes
	-i commits.txt	File with OpenZFS commit hashes to merge
	(one hash per row)
	-c commit hash	Prepare branch and try to merge this commit by hash,
	leaves branch for manual merge if not successfull
	-g commit hash	Generate commit description for existing commit in
	branch generated by -i parameter
	-p		force push new branch to Github
	-s		Fail if style checks do not succeed after merge

EOF
}

clean_unmerged() {
	git cherry-pick --abort
	git checkout master
	git branch -D "autoport-oz$OPENZFS_ISSUE" > /dev/null
	rm "$TMP_FILE"
}

prepare_git() {
	cd "$REPOSITORY_PATH"
	rm "$TMP_FILE"
	git checkout master
	git fetch --all
	git log --remotes=openzfs/master --format=%B -n 1 $OPENZFS_COMMIT > "$TMP_FILE"
	OPENZFS_ISSUE=$(grep -oP '^[^0-9]*\K[0-9]+' -m 1 "$TMP_FILE")
}

push_to_github() {
	if [ $PUSH_NEW_BRANCH -ne 0 ]; then
		git push origin autoport-oz$OPENZFS_ISSUE -f
	fi
}

generate_desc() {
	USER_NAME=$(git config user.name)
	USER_MAIL=$(git config user.email)
	OPENZFS_COMMIT_AUTHOR=$(git log --format="%aN <%aE>" --remotes=openzfs/master -n 1 $OPENZFS_COMMIT)
	sed -i '/^$/d' "$TMP_FILE"
	sed -i "1s/^$OPENZFS_ISSUE/OpenZFS $OPENZFS_ISSUE -/" "$TMP_FILE"
	sed -i "1 a Authored by: $OPENZFS_COMMIT_AUTHOR" "$TMP_FILE"
	
	echo 'Ported-by: '$USER_NAME' <'$USER_MAIL'>' >> "$TMP_FILE"
	echo '' >> "$TMP_FILE"
	echo 'OpenZFS-issue: https://www.illumos.org/issues/'$OPENZFS_ISSUE >> "$TMP_FILE"
	echo 'OpenZFS-commit: https://github.com/openzfs/openzfs/commit/'$OPENZFS_COMMIT >> "$TMP_FILE"
}

#add description to commit
add_desc_to_commit() {
	git commit --amend -F "$TMP_FILE"
}

merge() {
	ERR=0
	
	prepare_git
	
	echo -e "${LGREEN} - OpenZFS issue #$OPENZFS_ISSUE $OPENZFS_COMMIT ${NORMAL}"
	echo -e "${LGREEN} Checkout new branch ${NORMAL}"
	git branch -D "autoport-oz$OPENZFS_ISSUE"
	git checkout -b "autoport-oz$OPENZFS_ISSUE"
	
	echo -e "${LGREEN} Cherry-pick... ${NORMAL}"
	if ! git cherry-pick $OPENZFS_COMMIT; then
		printf 'cherry-pick failed' >&2
		echo $OPENZFS_COMMIT >> "$EXCEPTIONS_GIT"
		return 1
	fi
	
	echo -e "${LGREEN} Compile... ${NORMAL}"
	if ! make -s -j$(nproc); then
		printf 'compilation failed' >&2
		echo $OPENZFS_COMMIT >> "$EXCEPTIONS_COMPILE"<commit_msg>Silence git branch -D when merging a patch (#62)<commit_after>#!/bin/bash
#
# This script tries to merge OpenZFS commits to ZoL.
#
# Instruction:
# 
# Repository setup must be similar with openzfs-tracking.sh
# requirements.
#
# Repository setup for valid compilation check:
#	sh autogen.sh
#	./configure --enable-debug
#
# mandatory git settings:
#	[merge]
#   	renameLimit = 999999
#	[user]
#       email = <EMAIL>
#       name = <NAME>
#
# Copyright (c) 2016 <NAME>. All rights reserved.
#

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

REPOSITORY_PATH='.'
TMP_FILE='/tmp/gittmpmessage.txt'

# list with potential OpenZFS commits
UNPORTED_COMMITS_FILE=$SCRIPTDIR'/hashes.txt'

# Next files will generate automatically
# 	list with commits git can't merge automatically
EXCEPTIONS_GIT=$SCRIPTDIR'/exceptions.txt'
#	list with commits which can't be compiled without errors
EXCEPTIONS_COMPILE=$SCRIPTDIR'/uncompiled.txt'
#	list with commits which has cstyle error
EXCEPTIONS_CSTYLE=$SCRIPTDIR'/uncstyled.txt'
#	list with merged
EXCEPTIONS_MERGED=$SCRIPTDIR'/merged.txt'

LGREEN='\033[1;32m'		#  ${LGREEN}
NORMAL='\033[0m'		#  ${NORMAL} 
COUNT_MERGED=0
LIST_MERGED=
COUNT_COMPILE=0
LIST_COMPILE=
COUNT_CSTYLE=0
LIST_CSTYLE=
PUSH_NEW_BRANCH=0
FAIL_STYLE=0


usage() {
cat << EOF
USAGE:
$0 [-hp] [-d directory] [-i commits.txt] [-c commit hash] [-g commit hash]

DESCRIPTION:
	Auto merge OpenZFS commits to ZFS on Linux git
	repositories.
	Result - git branch with name 'autoport-oz#issue'

OPTIONS:
	-h		Show this message
	-d directory	Git repo with openzfs and zfsonlinux remotes
	-i commits.txt	File with OpenZFS commit hashes to merge
	(one hash per row)
	-c commit hash	Prepare branch and try to merge this commit by hash,
	leaves branch for manual merge if not successfull
	-g commit hash	Generate commit description for existing commit in
	branch generated by -i parameter
	-p		force push new branch to Github
	-s		Fail if style checks do not succeed after merge

EOF
}

clean_unmerged() {
	git cherry-pick --abort
	git checkout master
	git branch -D "autoport-oz$OPENZFS_ISSUE" > /dev/null
	rm "$TMP_FILE"
}

prepare_git() {
	cd "$REPOSITORY_PATH"
	rm "$TMP_FILE"
	git checkout master
	git fetch --all
	git log --remotes=openzfs/master --format=%B -n 1 $OPENZFS_COMMIT > "$TMP_FILE"
	OPENZFS_ISSUE=$(grep -oP '^[^0-9]*\K[0-9]+' -m 1 "$TMP_FILE")
}

push_to_github() {
	if [ $PUSH_NEW_BRANCH -ne 0 ]; then
		git push origin autoport-oz$OPENZFS_ISSUE -f
	fi
}

generate_desc() {
	USER_NAME=$(git config user.name)
	USER_MAIL=$(git config user.email)
	OPENZFS_COMMIT_AUTHOR=$(git log --format="%aN <%aE>" --remotes=openzfs/master -n 1 $OPENZFS_COMMIT)
	sed -i '/^$/d' "$TMP_FILE"
	sed -i "1s/^$OPENZFS_ISSUE/OpenZFS $OPENZFS_ISSUE -/" "$TMP_FILE"
	sed -i "1 a Authored by: $OPENZFS_COMMIT_AUTHOR" "$TMP_FILE"
	
	echo 'Ported-by: '$USER_NAME' <'$USER_MAIL'>' >> "$TMP_FILE"
	echo '' >> "$TMP_FILE"
	echo 'OpenZFS-issue: https://www.illumos.org/issues/'$OPENZFS_ISSUE >> "$TMP_FILE"
	echo 'OpenZFS-commit: https://github.com/openzfs/openzfs/commit/'$OPENZFS_COMMIT >> "$TMP_FILE"
}

#add description to commit
add_desc_to_commit() {
	git commit --amend -F "$TMP_FILE"
}

merge() {
	ERR=0
	
	prepare_git
	
	echo -e "${LGREEN} - OpenZFS issue #$OPENZFS_ISSUE $OPENZFS_COMMIT ${NORMAL}"
	echo -e "${LGREEN} Checkout new branch ${NORMAL}"
	git branch -D "autoport-oz$OPENZFS_ISSUE" > /dev/null
	git checkout -b "autoport-oz$OPENZFS_ISSUE"
	
	echo -e "${LGREEN} Cherry-pick... ${NORMAL}"
	if ! git cherry-pick $OPENZFS_COMMIT; then
		printf 'cherry-pick failed' >&2
		echo $OPENZFS_COMMIT >> "$EXCEPTIONS_GIT"
		return 1
	fi
	
	echo -e "${LGREEN} Compile... ${NORMAL}"
	if ! make -s -j$(nproc); then
		printf 'compilation failed' >&2
		echo $OPENZFS_COMMIT >> "$EXCEPTIONS_COMPILE"
{'fraction_non_alphanumeric': 0.1341026661360923, 'fraction_numerical': 0.011142061281337047, 'mean_word_length': 4.378031383737518, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 46, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '323492', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2946, 'n_words': 792}