nulib/bash/src/pman.sh
2025-02-28 20:28:12 +04:00

417 lines
12 KiB
Bash

# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
# configuration par défaut
# doit être identique au contenu de pman.conf
# les branches sont mergées dans cet ordre:
# upstream --> develop --> [release -->] main --> dist
# feature _/ hotfix _/
UPSTREAM=
DEVELOP=develop
FEATURE=wip/
RELEASE=release-
MAIN=master
TAG_PREFIX=
TAG_SUFFIX=
HOTFIX=hotfix-
DIST=
NOAUTO=
CONFIG_VARS=(
UPSTREAM DEVELOP FEATURE RELEASE MAIN TAG_PREFIX TAG_SUFFIX HOTFIX DIST NOAUTO
)
function _init_changelog() {
setx date=date +%d/%m/%Y-%H:%M
ac_set_tmpfile changelog
echo >"$changelog" "\
Vérifiez et complétez la liste des changements le cas échéant.
Un fichier vide annule l'opération
Ces lignes ne seront pas incluses dans le fichier destination
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"
}
function _filter_rel() {
# enlever les commits "techniques" générés par ce script
awk '
BEGIN { tech = 0 }
tech == 0 && $0 ~ /^\+.*<pman>/ { tech = 1; next }
tech == 1 && $0 ~ /^\|/ { next }
tech == 1 && $0 ~ /^\+/ { tech = 0 }
$0 !~ /<pman>/ { print }
'
}
function _filter_changes() {
# enlever les commits "inutiles" pour générer le fichier CHANGES.md
grep -vE '^([+|] )?[0-9a-f]+ modifs\.mineures sans commentaires$' |
grep -vE '^([+|] )?[0-9a-f]+ (cosmetic|typo|bug|fix|maj projet|maj deps)\$'
}
function _format_md() {
awk '
$0 == "" || $0 ~ /^#/ { print; next }
$1 == "+" {
$1 = "*"
$2 = "`" $2 "`"
print; next
}
$1 == "|" {
$1 = " *"
$2 = "`" $2 "`"
print; next
}
{
$1 = "* `" $1 "`"
print; next
}
'
}
function _list_commits() {
local source="${1:-$SrcBranch}" dest="${2:-$DestBranch}" mergebase
setx mergebase=git merge-base "$dest" "$source"
git log --oneline --graph --no-decorate "$mergebase..$source" |
grep -vF '|\' | grep -vF '|/' | sed -r 's/^(\| )+\* +/| /; s/^\* +/+ /' |
_filter_rel
}
function _scripte() {
echo >>"$script"
echo "$comment$(qvals "$@")" >>"$script"
}
function _scripta() {
[ $# -gt 0 ] && _scripte einfo "$*"
cat >>"$script"
}
function _script_push_branches() {
[ ${#push_branches[*]} -gt 0 ] || return
[ -n "$Origin" ] || Origin=origin
git_have_remote "$Origin" || return
local branch cmd remote rbranch
for branch in "${push_branches[@]}"; do
if [[ "$branch" == *:* ]]; then
cmd="$(qvals git push "$Origin" "$branch")"
else
setx remote=git_get_branch_remote "$branch"
if [ "$remote" == "$Origin" ]; then
setx rbranch=git_get_branch_merge "$branch"
if [ -n "$rbranch" ]; then
# pousser vers la branche distante existante
cmd="$(qvals git push "$Origin" "$branch:${rbranch#refs/heads/}")"
else
# pas de branche distante: pousser et traquer
cmd="$(qvals git push -u "$Origin" "$branch:$branch")"
fi
elif [ -n "$remote" ]; then
# pousser vers un remote différent
cmd="$(qvals git push "$Origin" "$branch:$branch")"
else
# pas de remote: pousser et traquer
cmd="$(qvals git push -u "$Origin" "$branch:$branch")"
fi
fi
_scripta <<<"$comment$cmd$or_die"
done
}
function _script_push_tags() {
local origin tag
_scripte einfo "push tags"
for tag in "${push_tags[@]}"; do
origin="$Origin"
[ -n "$origin" ] || origin=origin
_scripta <<EOF
$comment$(qvals git push --force "$origin" tag "$tag")$or_die
EOF
done
}
################################################################################
# Config
function ensure_gitdir() {
# commencer dans le répertoire indiqué
local chdir="$1"
if [ -n "$chdir" ]; then
cd "$chdir" || die || return
fi
# se mettre à la racine du dépôt git
local gitdir
git_ensure_gitvcs
setx gitdir=git_get_toplevel
cd "$gitdir" || die || return
}
function load_branches() {
local what="${1:-all}"; shift
case "$what" in
all)
setx CurrentBranch=git_get_branch
setx -a LocalBranches=git_list_branches
setx -a RemoteBranches=git_list_rbranches "$Origin"
setx -a AllBranches=git_list_pbranches "$Origin"
;;
current)
SrcBranch="$1"
[ -n "$SrcBranch" ] || SrcBranch="$CurrentBranch"
case "$SrcBranch" in
"$UPSTREAM") SrcType=upstream; DestBranch="$DEVELOP";;
"$FEATURE"*) SrcType=feature; DestBranch="$DEVELOP";;
"$DEVELOP") SrcType=develop; DestBranch="$MAIN";;
"$RELEASE"*) SrcType=release; DestBranch="$MAIN";;
"$HOTFIX"*) SrcType=hotfix; DestBranch="$MAIN";;
"$MAIN") SrcType=main; DestBranch="$DIST";;
"$DIST") SrcType=dist; DestBranch=;;
*) DestBranch=;;
esac
local branch
UpstreamBranch=
FeatureBranches=()
DevelopBranch=
ReleaseBranch=
HotfixBranch=
MainBranch=
Dist=Branch
for branch in "${AllBranches[@]}"; do
if [ "$branch" == "$UPSTREAM" ]; then
UpstreamBranch="$branch"
elif [[ "$branch" == "$FEATURE"* ]]; then
FeatureBranch+=("$branch")
elif [ "$branch" == "$DEVELOP" ]; then
DevelopBranch="$branch"
elif [[ "$branch" == "$RELEASE"* ]]; then
ReleaseBranch="$branch"
elif [[ "$branch" == "$HOTFIX"* ]]; then
HotfixBranch="$branch"
elif [ "$branch" == "$MAIN" ]; then
MainBranch="$branch"
elif [ "$branch" == "$DIST" ]; then
DistBranch="$branch"
fi
done
;;
esac
}
function load_config() {
if [ -n "$ConfigFile" ]; then
source "$ConfigFile" || die || return
elif [ -n "$ConfigBranch" ]; then
if ! array_contains LocalBranches "$ConfigBranch"; then
die "$ConfigBranch: branche de configuration introuvable" || return
else
ac_set_tmpfile ConfigFile
git show "$ConfigBranch:.pman.conf" >"$ConfigFile" 2>/dev/null
[ -s "$ConfigFile" ] || die "$ConfigBranch: aucune configuration trouvée sur cette branche" || return
source "$ConfigFile"
fi
elif [ -f .pman.conf ]; then
ConfigFile="$(pwd)/.pman.conf"
source "$ConfigFile"
elif [ -n "${MYNAME#prel}" ]; then
ConfigFile="$NULIBDIR/bash/src/pman${MYNAME#$1}.conf.sh"
source "$ConfigFile"
else
ConfigFile="$NULIBDIR/bash/src/pman.conf.sh"
fi
}
################################################################################
# Divers
function _push_branches() {
[ ${#push_branches[*]} -gt 0 ] || return
[ -n "$Origin" ] || Origin=origin
git_have_remote "$Origin" || return
local -a cmds; local branch cmd remote rbranch
for branch in "${push_branches[@]}"; do
if [[ "$branch" == *:* ]]; then
cmds+=("$(qvals git push "$Origin" "$branch")")
else
setx remote=git_get_branch_remote "$branch"
if [ "$remote" == "$Origin" ]; then
setx rbranch=git_get_branch_merge "$branch"
if [ -n "$rbranch" ]; then
# pousser vers la branche distante existante
cmds+=("$(qvals git push "$Origin" "$branch:${rbranch#refs/heads/}")")
else
# pas de branche distante: pousser et traquer
cmds+=("$(qvals git push -u "$Origin" "$branch:$branch")")
fi
elif [ -n "$remote" ]; then
# pousser vers un remote différent
cmds+=("$(qvals git push "$Origin" "$branch:$branch")")
else
# pas de remote: pousser et traquer
cmds+=("$(qvals git push -u "$Origin" "$branch:$branch")")
fi
fi
done
[ -n "$Push" ] || enote "L'option --no-push étant utilisée, les opérations à effectuer sont simplement affichées"
for cmd in "${cmds[@]}"; do
einfo "$cmd"
if [ -n "$Push" ]; then
if ! eval "$cmd"; then
ewarn "Une erreur s'est produite, les opérations seront simplement affichées"
Push=
fi
fi
done
}
################################################################################
# Merge
function _mscript_start() {
>"$script"
_scripta <<EOF
#!/bin/bash
$(qvals source "$NULIBDIR/load.sh") || exit 1
merge=
delete=
push=
for arg in "\$@"; do
case "\$arg" in
merge) merge=1;;
delete) delete=1;;
push) push=1;;
esac
done
EOF
chmod +x "$script"
}
function _mscript_merge_branch() {
local msg
# basculer sur la branche
_scripta "switch to branch $DestBranch" <<EOF
$comment$(qvals git checkout "$DestBranch")$or_die
EOF
if [ -n "$SquashMsg" ]; then
msg="$SquashMsg"
[ -n "$TechMerge" ] && msg="<pman>$msg"
_scripta "squash merge $SrcBranch" <<EOF
$comment$(qvals git merge "$SrcBranch" --squash)$or_die
$comment$(qvals git commit -m "$msg")$or_die
EOF
else
msg="Intégration de la branche $SrcBranch"
[ -n "$TechMerge" ] && msg="<pman>$msg"
_scripta "merge $SrcBranch" <<EOF
$comment$(qvals git merge "$SrcBranch" --no-ff -m "$msg")$or_die
EOF
fi
array_addu push_branches "$DestBranch"
}
function _mscript_delete_branch() {
_scripta "delete branch $SrcBranch" <<EOF
$comment$(qvals git branch -D "$SrcBranch")$or_die
EOF
array_addu delete_branches ":$SrcBranch"
}
################################################################################
# Release
function _rscript_start() {
>"$script"
_scripta <<EOF
#!/bin/bash
$(qvals source "$NULIBDIR/load.sh") || exit 1
create=
merge=
push=
for arg in "\$@"; do
case "\$arg" in
create) create=1;;
merge) merge=1;;
push) push=1;;
esac
done
EOF
chmod +x "$script"
}
function _rscript_create_release_branch() {
local date changelog
_init_changelog
echo >>"$changelog" "\
## Release $Tag du $date
"
_list_commits | _filter_changes | _format_md >>"$changelog"
if [ -s CHANGES.md ]; then
echo >>"$changelog"
cat CHANGES.md >>"$changelog"
fi
"${EDITOR:-nano}" +7 "$changelog"
[ -s "$changelog" ] || exit_with ewarn "Création de la release annulée"
# créer la branche de release et basculer dessus
_scripta "create branch $ReleaseBranch" <<EOF
$(qvals git checkout -b "$ReleaseBranch" "$SrcBranch")$or_die
EOF
# créer le changelog
_scripta "update CHANGES.md" <<EOF
$(qvals echo "$(awk <"$changelog" '
BEGIN { p = 0 }
p == 0 && $0 == "" { p = 1; next }
p == 1 { gsub(/\$/, "\\$", $0); print }
')") >CHANGES.md
git add CHANGES.md
EOF
# mettre à jour la version
_scripta "update VERSION.txt" <<EOF
$(qvals echo "$Version") >VERSION.txt
git add VERSION.txt
EOF
# Enregistrer les changements
_scripta "commit" <<EOF
$(qvals git commit -m "<pman>Init changelog & version $Version")
EOF
}
function _rscript_merge_release_branch() {
local dest="$1" tag="$2"
# basculer sur la branche
_scripta "switch to branch $dest" <<EOF
$comment$(qvals git checkout "$dest")$or_die
EOF
# fusionner la branche de release
_scripta "merge branch $ReleaseBranch" <<EOF
$comment$(qvals git merge "$ReleaseBranch" -m "<pman>Intégration de la branche $ReleaseBranch" --no-ff)$or_die
EOF
array_addu push_branches "$dest"
# tagger la release
if [ -n "$tag" ]; then
_scripta "create tag $tag" <<EOF
$comment$(qvals git tag --force "$tag")$or_die
EOF
array_addu push_tags "$tag"
fi
}
function _rscript_delete_release_branch() {
_scripta "delete branch $ReleaseBranch" <<EOF
$comment$(qvals git branch -D "$ReleaseBranch")$or_die
EOF
}