Files
scripts/susefy-package.sh
Egbert Eich 199ba0de24 Initial import
Signed-off-by: Egbert Eich <eich@suse.com>
2025-09-01 12:49:11 +02:00

141 lines
3.7 KiB
Bash
Executable File

#! /usr/bin/bash
commit=upstream/rawhide
target_branch=main
# intermediate_branch=work
#set -x
shopt -s nullglob
init_git() {
/usr/bin/obs-git-init
# clean_files sources README.md _buildconfig\* _buildinfo\* debian .osc
}
download_packages() {
# Download spec files
osc service run download_files
for i in _service:download_files:*; do
j=${i#_service:download_files:}
[ -e $j ] && { rm $i; continue; }
mv $i $j
done
}
clean_files() {
while [ -n "$1" ] ; do
echo "$1" >> .gitignore
[ -d "$1" ] && rm -rf "$1" || rm -f "$1"
shift
done
}
susefy() {
local specfile=$1
local tmpfile=$(mktemp specfile-XXXXX)
# Generate changes file
rm -f .gitignore
clean_files sources README.md _buildconfig\* _buildinfo\* debian .osc changelog
cat ${specfile} | /usr/lib/build/spec2changelog | \
awk "BEGIN{p=1}/^\s*$/{ if (p) print; p=0; next; }/.*/{ p=1; print; }" |\
awk '/^[^-<>]* [[:digit:]]{4} - [^<>]* <[^<>]*> .*$/{gsub("[^<>]*$","");print;}{print;next;}' > ${specfile%.spec}.changes
# Strip changes
cat ${specfile} | \
awk "/^%changelog/{ print; nextfile; }{ print; }" > $tmpfile
mv $tmpfile ${specfile}
# Fix format SUSE style
osc service run format_spec_file
mv _service:format_spec_file:${specfile} ${specfile}
}
setup_merge_branch() {
local target_branch=$1
if ! $(git branch | grep -q $target_branch); then
if $(git branch -a | grep -q origin/$target_branch); then
git branch $target_branch origin/$target_branch
else
git branch $target_branch
fi
fi
}
git_add_commit() {
local msg="$1"
OFS=$IFS
IFS="
"
git status --porcelain | while read line; do
file=${line##* };
[[ $line =~ ^\?\?\ .\\* ]] && { git add $file; continue; }
[[ $line =~ ^.[DM]\ .\\* ]] && { git add $file; continue; }
[[ $line =~ ^.[DU]\ .\\* ]] && { git rm $file; continue; }
done
IFS=$OFS
git commit -s -m "$msg"
}
merge_branches() {
local line file
local source=$1
local target_branch=$2
git merge -s ours -m "Merge updates from ${source} into ${target_branch} branch" ${target_branch}
git branch -D ${target_branch}
git checkout -b ${target_branch}
git branch -D tmp
}
[ -x /usr/bin/obs-git-init -a -x /usr/bin/osc -a -x /usr/lib/build/spec2changelog ] \
|| { echo "make sure packages \'obs-git-init\', \'osc\' " \
"and \'obs-service_format_spec_file\' are installed"; exit 1; }
while [ -n "$1" ]; do
case $1 in
-r) shift; commit=$1; shift ;;
-t) shift; target_branch=$1; shift ;;
-i) shift; intermediate_branch=$1; shift ;;
-*) echo "$unknown option $1"; exit 1 ;;
*) specfile=$1; shift ;;
esac
done
id1=$(git show-ref $commit | cut -f 1 -d' ')
id2=$(git merge-base $commit $target_branch)
if [ "$id1" = "$id2" ]; then
echo "Nothing to do!"
exit 0
fi
git checkout -b tmp $commit
if [ -z "$specfile" ]; then
s=(*.spec)
[ ${#s[*]} -eq 1 ] || \
{ echo "Cannot uniquely determine specfile. Specify as argument" >&2;
exit 1; }
specfile=${s[0]}
fi
[ -n "$specfile" ] || \
{ echo "Specify specfile" >&2; exit 1; }
[[ $specfile =~ .\\*\.spec ]] || specfile=${specfile}.spec
[ -e ${specfile} ] || \
{ echo "$specfile: incorrect spec file name" >&2; exit 1; }
# Initialize git
init_git
download_packages
git_add_commit "Add/update .git* files, add sources & remove unneeded files"
if [ -n "${intermediate_branch}" ]; then
setup_merge_branch ${intermediate_branch}
merge_branches $commit ${intermediate_branch}
git checkout -b tmp ${intermediate_branch}
fi
susefy $specfile
git_add_commit "Convert to SUSE Format"
setup_merge_branch $target_branch
if [ -n "${intermediate_branch}" ]; then
merge_branches ${intermediate_branch} $target_branch
else
merge_branches $commit $target_branch
fi