From 05ea4ac1952c546a81781ac0662747dbe6073616 Mon Sep 17 00:00:00 2001 From: Juergen Funk Date: Wed, 8 Mar 2023 10:14:55 +0100 Subject: [PATCH] git-hooks: better handling in './g -z' with win-links and linux-links This patch improve patch 9afc6b22e225e8a6ec50c1cbb4d966585d15dbfd git-hooks: overwrite the windows-links not with linux-links The main problem with the previous patch is that when an alias is set for git, this alias will not map in the Shell script passed, better you use a git-symlink. The other problem was the behavior of cygwin-bash and win-git-bash that is not always the same, e.g. - winlnk=$(cmd /C ... 2>&1) it hang infinity, line 123 - the 'ln' made not link, it only copy the files Improving the FOR in ./git-hooks/README, when you have the copied files, need a other del command The patch can not set the windows-links, only output the ./git-hooks/README But in Win 11 should be possible, it is not needed admin-rights for the 'mklink' command Change-Id: Icecdb96e65fe2bba1270dfad2ac1af5af145925a Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148462 Tested-by: Jenkins Reviewed-by: Juergen Funk --- .git-hooks/README | 3 +- g | 82 ++++++++++++++++++++++++++++------------------- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/.git-hooks/README b/.git-hooks/README index 5ef9e8e45a48..62a16c1de69e 100644 --- a/.git-hooks/README +++ b/.git-hooks/README @@ -7,7 +7,8 @@ To install manually, run: When you using GIT for Windows, you need Windows links Open a Dos-Box with admin rights then cd .git/hooks - FOR /F " usebackq " %i IN (`dir /b ..\..\.git-hooks`) DO del /as /f %i & mklink %i ..\..\.git-hooks\%i + FOR /F " usebackq " %i IN (`dir /b ..\..\.git-hooks`) DO del /as /f %i & del %i & mklink %i ..\..\.git-hooks\%i +the error 'Could Not Find ...' can be ignore There are two groups of these hooks: client side and server side. diff --git a/g b/g index a9a879f5d907..f6f9e434df9a 100755 --- a/g +++ b/g @@ -90,41 +90,65 @@ refresh_all_hooks() local hook_name local hook local winlnk + local wingit + local gitbash local lnkfile=".git/hooks/pre-commit" pushd "${COREDIR?}" > /dev/null - if [ $WINGIT -eq 1 ]; then - winlnk=0 - if [ -e "${lnkfile}" ] && [ -L "${lnkfile}" ] ; then - # if linux-links or windows-links? - # dos dir output windows link: - # 04.09.2020 10:54 pre-commit [..\..\.git-hooks\pre-commit] - # dos dir output linux link: - # file not found - winlnk=$(cmd /C "DIR ${lnkfile//'/'/'\'}" 2>&1) - winlnk=$(echo "$winlnk" | grep -icE ".*${lnkfile##*/} \[") - fi + # it is 'GIT for Windows' + wingit=$(git --version | grep -ic windows) + gitbash=$(echo $OSTYPE | grep -ic msys) - if [ $winlnk -eq 0 ]; then - echo "You using GIT for Windows, but the hook-links not right, change with mklink" - cat .git-hooks/README + # In the win-git-bash, do not make links, it make only copies + if [ $gitbash -eq 1 ]; then + if [ -d ".git" ]; then + if [ ! -e "${lnkfile}" ] || [ ! -L "${lnkfile}" ] ; then + # here when wrong link then the file not exist + echo "Your hooks not right, solve this in cygwin with" + echo " ./g -z" + fi fi else - # There's no ".git" e.g. in a secondary worktree - if [ -d ".git" ]; then - for hook_name in "${COREDIR?}/.git-hooks"/* ; do - hook=".git/hooks/${hook_name##*/}" - if [ ! -e "${hook?}" ] || [ ! -L "${hook?}" ] ; then - rm -f "${hook?}" - ln -sf "${hook_name}" "${hook?}" + if [ $wingit -eq 1 ]; then + # There's no ".git" e.g. in a secondary worktree + if [ -d ".git" ]; then + winlnk=0 + if [ -e "${lnkfile}" ] && [ -L "${lnkfile}" ] ; then + # if linux-links or windows-links? + # dos dir output windows link: + # 04.09.2020 10:54 pre-commit [..\..\.git-hooks\pre-commit] + # dos dir output linux link: + # file not found + winlnk=$(cmd /C "DIR ${lnkfile//'/'/'\'}" 2>&1) + winlnk=$(echo "$winlnk" | grep -icE ".*${lnkfile##*/} \[") fi + + if [ $winlnk -eq 0 ]; then + echo "You using GIT for Windows, but the hook-links not right, change with mklink" + cat .git-hooks/README + fi + fi + else + # There's no ".git" e.g. in a secondary worktree + if [ -d ".git" ]; then + for hook_name in "${COREDIR?}/.git-hooks"/* ; do + hook=".git/hooks/${hook_name##*/}" + if [ ! -e "${hook?}" ] || [ ! -L "${hook?}" ] ; then + rm -f "${hook?}" + ln -sf "${hook_name}" "${hook?}" + fi + done + fi + + for repo in ${SUBMODULES_ALL?} ; do + refresh_submodule_hooks "$repo" done fi + fi - for repo in ${SUBMODULES_ALL?} ; do - refresh_submodule_hooks "$repo" - done + if [ $wingit -eq 0 ] && [ "$OSTYPE" == "cygwin" ] ; then + echo "When you using GIT for Windows, and get git-error look to .git-hooks/README" fi popd > /dev/null @@ -316,9 +340,8 @@ if [ "$#" -eq "0" ] ; then usage fi -gitfile="$(type -p git)" -if [ ! "${gitfile}" ]; then +if [ ! "$(type -p git)" ]; then echo "Cannot find the git binary! Is git installed and is in PATH?" exit 1 fi @@ -347,14 +370,7 @@ REPORT_REPOS=1 REPORT_COMMANDS=0 REPORT_COMPACT=0 DO_HOOK_REFRESH=false -WINGIT= -# it is 'GIT for Windows' -WINGIT=$(echo $gitfile | grep -c cygdrive) -if [ $WINGIT -eq 1 ]; then - gitfile=$(git --version) - WINGIT=$(echo $gitfile | grep -ic windows) -fi while [ "${COMMAND:0:1}" = "-" ] ; do case "$COMMAND" in