From 1661ee5f480659c306e1222e6e30c5976af27589 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 26 Mar 2015 23:54:43 +0400 Subject: [PATCH 1/4] ajout des fonctions git_is_ancestor(), git_should_ff(), git_should_push(), git_is_merged() --- lib/ulib/vcs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/ulib/vcs b/lib/ulib/vcs index 1248feb..a1b044c 100644 --- a/lib/ulib/vcs +++ b/lib/ulib/vcs @@ -523,6 +523,8 @@ __VCS_GIT_ADVANCED_MAP=( gb:git_get_branch ib:git_is_branch hr:git_have_remote tb:git_track_branch cc:git_check_cleancheckout ec:git_ensure_cleancheckout + ia:git_is_ancestor sff:git_should_ff spu:git_should_push + im:git_is_merged ) __VCS_GIT_ADVANCED=( git_check_gitvcs git_ensure_gitvcs @@ -531,6 +533,8 @@ __VCS_GIT_ADVANCED=( git_get_branch git_is_branch git_have_remote git_track_branch git_check_cleancheckout git_ensure_cleancheckout + git_is_ancestor git_should_ff git_should_push + git_is_merged ) function git_check_gitvcs() { [ "$(_vcs_get_type "$(_vcs_get_dir)")" == git ] @@ -596,6 +600,46 @@ function git_ensure_cleancheckout() { git_check_cleancheckout || die "Vous avez des modifications locales. Enregistrez ces modifications avant de continuer" } +function __git_check_can_ff() { + [ "$1" == "$(git merge-base "$1" "$2")" ] +} +function git_is_ancestor() { + # vérifier que la branche $1 est un ancêtre direct de la branche $2, qui + # vaut par défaut remotes/origin/$1 + # note: cette fonction retourne vrai si $1 et $2 identifient le même commit + local b="$1" o="${2:-remotes/origin/$1}" + b="$(git rev-parse --verify "$b")" || return 1 + o="$(git rev-parse --verify "$o")" || return 1 + __git_check_can_ff "$b" "$o" +} +function git_should_ff() { + # vérifier si la branche $1 devrait être fast-forwardée à partir de la + # branche d'origine $2, qui vaut par défaut remotes/origin/$1 + # note: cette fonction est similaire à git_is_ancestor(), mais retourne + # false si $1 et $2 identifient le même commit + local b="$1" o="${2:-remotes/origin/$1}" + b="$(git rev-parse --verify "$b")" || return 1 + o="$(git rev-parse --verify "$o")" || return 1 + [ "$b" != "$o" ] || return 1 + __git_check_can_ff "$b" "$o" +} +function git_should_push() { + # vérifier si la branche $1 devrait être poussée vers la branche de même nom + # dans l'origine $2(=origin), parce que l'origin peut-être fast-forwardée à + # partir de cette branche. + git_should_ff "remotes/${2:-origin}/$1" "$1" +} + +function git_is_merged() { + # vérifier que les branches $1 et $2 ont un ancêtre commun, et que la + # branche $1 a été complètement fusionnée dans la branche destination $2 + local b="$1" d="$2" + b="$(git rev-parse --verify "$b")" || return 1 + d="$(git rev-parse --verify "$d")" || return 1 + [ -n "$(git merge-base "$b" "$d")" ] || return 1 + [ -z "$(git rev-list "$d..$b")" ] +} + # fonctions pour git annex function git_annex_initial() { # sur le dépôt $1 fraichement cloné, vérifier s'il faut faire git annex From e3e3bbb9eda2f99dec34a93ef98b3216f9715cbb Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Fri, 27 Mar 2015 00:15:40 +0400 Subject: [PATCH 2/4] ajout de git_fast_forward --- lib/ulib/vcs | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/ulib/vcs b/lib/ulib/vcs index a1b044c..54a72e1 100644 --- a/lib/ulib/vcs +++ b/lib/ulib/vcs @@ -600,28 +600,31 @@ function git_ensure_cleancheckout() { git_check_cleancheckout || die "Vous avez des modifications locales. Enregistrez ces modifications avant de continuer" } +function __git_init_ff() { + o="${3:-origin}" + b="$1" s="${2:-remotes/$o/$1}" + b="$(git rev-parse --verify "$b")" || return 1 + s="$(git rev-parse --verify "$s")" || return 1 + return 0 +} function __git_check_can_ff() { [ "$1" == "$(git merge-base "$1" "$2")" ] } function git_is_ancestor() { # vérifier que la branche $1 est un ancêtre direct de la branche $2, qui - # vaut par défaut remotes/origin/$1 + # vaut par défaut remotes/${3:-origin}/$1 # note: cette fonction retourne vrai si $1 et $2 identifient le même commit - local b="$1" o="${2:-remotes/origin/$1}" - b="$(git rev-parse --verify "$b")" || return 1 - o="$(git rev-parse --verify "$o")" || return 1 - __git_check_can_ff "$b" "$o" + local o b s; __git_init_ff "$@" || return + __git_can_ff "$b" "$s" } function git_should_ff() { # vérifier si la branche $1 devrait être fast-forwardée à partir de la - # branche d'origine $2, qui vaut par défaut remotes/origin/$1 + # branche d'origine $2, qui vaut par défaut remotes/${3:-origin}/$1 # note: cette fonction est similaire à git_is_ancestor(), mais retourne # false si $1 et $2 identifient le même commit - local b="$1" o="${2:-remotes/origin/$1}" - b="$(git rev-parse --verify "$b")" || return 1 - o="$(git rev-parse --verify "$o")" || return 1 - [ "$b" != "$o" ] || return 1 - __git_check_can_ff "$b" "$o" + local o b s; __git_init_ff "$@" || return + [ "$b" != "$s" ] || return 1 + __git_can_ff "$b" "$o" } function git_should_push() { # vérifier si la branche $1 devrait être poussée vers la branche de même nom @@ -629,6 +632,17 @@ function git_should_push() { # partir de cette branche. git_should_ff "remotes/${2:-origin}/$1" "$1" } +function git_fast_forward() { + # vérifier que la branche courante est bien $1, puis tester s'il faut la + # fast-forwarder à partir de l'origine $2, puis le faire si c'est + # nécessaire. la branche d'origine $2 vaut par défaut remotes/origin/$1 + local o b s; __git_init_ff "$@" || return + [ "$b" != "$s" ] || return 1 + local head="$(git rev-parse HEAD)" + [ "$head" == "$b" ] || return 1 + __git_can_ff "$b" "$s" || return 1 + git merge --ff-only "$s" +} function git_is_merged() { # vérifier que les branches $1 et $2 ont un ancêtre commun, et que la From 743e17ba0fd79124d7f53f5b5793dcd23886c99f Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Fri, 27 Mar 2015 00:16:03 +0400 Subject: [PATCH 3/4] fast-forwarder automatiquement la branche vers laquelle on bascule --- pdev | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pdev b/pdev index 0cfa33d..9f9dafd 100755 --- a/pdev +++ b/pdev @@ -123,13 +123,15 @@ if [ "$action" == branch ]; then if [ -z "$UTOOLS_VCS_OFFLINE" ]; then git_track_branch "$feature" "$origin" fi + git_fast_forward "$feature" "" "$origin" exit 0 fi # Créer/basculer vers une feature branch git_ensure_cleancheckout + r=0 if git_have_branch "$feature"; then - git checkout "$feature" + git checkout "$feature" || r=$? else estepn "\ Vous allez créer la nouvelle feature branch ${COULEUR_VERTE}$feature${COULEUR_NORMALE} @@ -138,7 +140,11 @@ Vous allez créer la nouvelle feature branch ${COULEUR_VERTE}$feature${COULEUR_N git_ensure_branch "$feature" "$source" "$origin" [ $? -eq 2 ] && die "Impossible de créer la branche $feature. Veuillez vérifier que la branche $source existe" - git checkout "$feature" + git checkout "$feature" || r=$? + fi + if [ "$r" -eq 0 ]; then + # éventuellement fast-forwarder automatiquement + git_fast_forward "$feature" "" "$origin" fi exit $? From c3683a50cd79edae94e11a4c00f545e375ab763a Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Fri, 27 Mar 2015 00:17:29 +0400 Subject: [PATCH 4/4] bug --- lib/ulib/vcs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ulib/vcs b/lib/ulib/vcs index 54a72e1..8bfc336 100644 --- a/lib/ulib/vcs +++ b/lib/ulib/vcs @@ -607,7 +607,7 @@ function __git_init_ff() { s="$(git rev-parse --verify "$s")" || return 1 return 0 } -function __git_check_can_ff() { +function __git_can_ff() { [ "$1" == "$(git merge-base "$1" "$2")" ] } function git_is_ancestor() {