git-hooks: better handling in './g -z' with win-links

and linux-links

This patch improve patch
9afc6b22e2 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 <juergen.funk_ml@cib.de>
This commit is contained in:
Juergen Funk 2023-03-08 10:14:55 +01:00
parent d68e428a2a
commit 05ea4ac195
2 changed files with 51 additions and 34 deletions

View file

@ -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.

82
g
View file

@ -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 <SYMLINK> 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 "<SYMLINK>.*${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 <SYMLINK> 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 "<SYMLINK>.*${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