nulib/wip/pman

237 lines
6.8 KiB
Plaintext
Raw Normal View History

2025-02-26 11:43:33 +04:00
#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
source "$(dirname -- "$0")/../load.sh" || exit 1
require: git pman pman.conf
################################################################################
# Informations
################################################################################
SHOW_VARS=(
--Configuration
"${CONFIG_VARS[@]}"
--Paramètres
CurrentBranch
SrcType SrcBranch
DestBranch
)
function show_action() {
echo_setv ConfigBranch="$ConfigBranch"
echo_setv ConfigFile="$(ppath "$ConfigFile")"
for var in "${SHOW_VARS[@]}"; do
if [ "${var#--}" != "$var" ]; then
estep "${var#--}"
else
echo_setv "$var=${!var}"
fi
done
}
################################################################################
# Initialisation
################################################################################
function _push_branches() {
[ -n "$Origin" ] || Origin=origin
git_have_remote "$Origin" || retun
if [ ${#push_branches[*]} -gt 0 ]; then
local -a cmds remote rbranch
for branch in "${push_branches[@]}"; do
setx remote=git_get_branch_remote "$branch"
if [ "$remote" == "$Origin" ]; then
setx rbranch=git_get_branch_merge "$branch"
if [ -n "$rbranch" ]; then
cmds+=("$(qvals git push "$Origin" "$branch:$rbranch")")
else
cmds+=("$(qvals git push -u "$Origin" "$branch:$branch")")
fi
else
cmds+=("$(qvals git push -u "$Origin" "$branch:$branch")")
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
fi
}
function init_repo_action() {
[ ${#LocalBranches[*]} -eq 0 ] || die "Ce dépôt a déjà été initialisé"
local -a push_branches
if [ ! -f .pman.conf ]; then
cp "$ConfigFile" .pman.conf
"${EDITOR:-nano}" .pman.conf
if testdiff .pman.conf "$ConfigFile"; then
ConfigFile="$(pwd)/.pman.conf"
load_config
load_branches current "$SrcBranch"
fi
git add .pman.conf
fi
if [ ! -f ".gitignore" ]; then
echo >.gitignore "\
.~lock*#
.*.swp"
git add .gitignore
fi
einfo "Création de la branche $MAIN"
git symbolic-ref HEAD "refs/heads/$MAIN"
git commit -m "commit initial"
push_branches+=("$MAIN")
einfo "Création de la branche $DEVELOP"
git checkout -b "$DEVELOP"
push_branches+=("$DEVELOP")
_push_branches
}
function init_develop_action() {
if [ -z "$DevelopBranch" ]; then
local -a push_branches
einfo "Création de la branche $DEVELOP"
git checkout -b "$DEVELOP" "$MAIN"
push_branches+=("$DEVELOP")
_push_branches
fi
git checkout -q "$DEVELOP"
}
function init_upstream_action() {
if [ -z "$UpstreamBranch" ]; then
[ -n "$UPSTREAM" ] || die "La branche UPSTREAM n'a pas été définie"
[ -n "$DevelopBranch" ] || die "$DEVELOP: cette branche n'existe pas"
local -a push_branches; local config
# faire une copie de la configuration actuelle
ac_set_tmpfile config
cp "$ConfigFile" "$config"
einfo "Création de la branche $UPSTREAM"
git checkout --orphan "$UPSTREAM" || die
git rm -rf .
cp "$config" .pman.conf
git add .pman.conf
git commit -m "commit initial"
push_branches+=("$UPSTREAM")
einfo "Fusion dans $DEVELOP"
git checkout "$DEVELOP"
git merge \
--no-ff -m "Intégration initiale de la branche $UPSTREAM" \
-srecursive -Xours --allow-unrelated-histories \
"$UPSTREAM"
push_branches+=("$DEVELOP")
_push_branches
fi
git checkout -q "$UPSTREAM"
}
function init_dist_action() {
if [ -z "$DistBranch" ]; then
[ -n "$DIST" ] || die "La branche DIST n'a pas été définie"
[ -n "$MainBranch" ] || die "$MAIN: cette branche n'existe pas"
local -a push_branches
einfo "Création de la branche $DIST"
git checkout -b "$DIST" "$MAIN" || die
push_branches+=("$DIST")
_push_branches
fi
git checkout -q "$DIST"
}
function init_feature_action() {
local branch="$FEATURE${1#$FEATURE}"
[ -n "$branch" ] || die "Vous devez définir la nom de la branche à créer"
if ! array_contains AllBranches "$branch"; then
[ -n "$DEVELOP" ] || die "La branche DEVELOP n'a pas été définie"
local -a push_branches
einfo "Création de la branche $branch"
git checkout -b "$branch" "$DEVELOP" || die
push_branches+=("$branch")
_push_branches
fi
git checkout -q "$branch"
}
function init_action() {
local what="${1:-develop}"; shift
case "$what" in
init|repo|r) init_repo_action "$@";;
main|m) git checkout -q "$MAIN";;
develop|dev|d) init_develop_action "$@";;
upstream|up|u) init_upstream_action "$@";;
dist|x) init_dist_action "$@";;
*) init_feature_action "$what" "$@";;
esac
}
################################################################################
# Programme principal
################################################################################
chdir=
ConfigBranch=
ConfigFile=
action=init
Origin=
Push=1
args=(
"gérer un projet git"
"repo|develop|upstream|dist"
-d:,--chdir:BASEDIR chdir= "répertoire dans lequel se placer avant de lancer les opérations"
-b:,--config-branch ConfigBranch= "++\
branche à partir de laquelle charger la configuration"
-c:,--config-file:CONFIG ConfigFile= "++\
fichier de configuration des branches. cette option est prioritaire sur --config-branch
par défaut, utiliser le fichier .pman.conf dans le répertoire du dépôt s'il existe"
-s,--show-config action=show "++\
afficher la configuration chargée"
-o:,--origin Origin= "++\
origine vers laquelle pousser les branches"
-n,--no-push Push= "\
ne pas pousser les branches vers leur origine après leur création"
-p,--push Push=1 "++\
pousser les branches vers leur origine après leur création.
c'est l'option par défaut"
)
parse_args "$@"; set -- "${args[@]}"
# charger la configuration
ensure_gitdir "$chdir"
load_branches all
load_config "$MYNAME"
load_branches current
# puis faire l'action que l'on nous demande
case "$action" in
show)
show_action "$@"
;;
init)
git_ensure_cleancheckout
init_action "$@"
;;
esac