Skip to content

Commit

Permalink
Merge 795f704 into 0cc99f0
Browse files Browse the repository at this point in the history
  • Loading branch information
twollgam committed Apr 12, 2022
2 parents 0cc99f0 + 795f704 commit bee2ff4
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 0 deletions.
40 changes: 40 additions & 0 deletions misc/git-hook/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
param([switch]$all, [switch]$clear)

$gitDir = git rev-parse --git-common-dir

if (!(test-path $gitDir))
{
$gitDir = git rev-parse --git-dir
}

$gitDir = (resolve-path ($gitDir)).Path

if (!$gitDir) {
throw ".git - Directory not found"
}

$gitHooksDir = join-path $gitDir "hooks"

if (!(test-path $gitHooksDir)) {
throw "$gitHooksDir not present"
}

if($clear) {
write-host "cleaning old hooks dir: $githooksdir"
Remove-Item (join-path $githooksdir "*") -force -recurse
}

write-host "providing files:"

if ($all) {
Copy-Item $PSScriptRoot/optional/* $githooksdir -force -recurse -PassThru | format-table Fullname | out-host
}

Copy-Item $PSScriptRoot/necessary/* $githooksdir -force -recurse -PassThru | format-table Fullname | out-host

if($IsLinux) {
chmod 755 (join-path $githooksdir "post-merge")
chmod 755 (join-path $githooksdir "pre-commit")
chmod 755 (join-path $githooksdir "precommit/check/*")
chmod 755 (join-path $githooksdir "precommit/modify/*")
}
62 changes: 62 additions & 0 deletions misc/git-hook/necessary/functions.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
concatenate()
{
list="$1"
element="$2"
delimiter="\n"

if [ -z "$list" ]
then
echo "$element"
else
echo -e "$list$delimiter$element"
fi
}

removeEmptyLines()
{
echo "$@" | sed '/^$/d'
}

isLinux()
{
if [ "$OSTYPE" = "linux" ]
then
return 0
fi

return 1
}

isWindows()
{
if [ "$OSTYPE" = "linux" ]
then
return 1
fi

return 0
}

onLinux()
{
isLinux && $@
}

onWindows()
{
isWindows && $@
}

runPSScript()
{
enter "runPSScript"

if isLinux
then
"pwsh" $@
else
"powershell" "-noprofile" $@
fi
}


34 changes: 34 additions & 0 deletions misc/git-hook/necessary/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

# Usage: add this file to your project's .git/hooks directory.
# Now, when you change some files in repository and try to commit these
# changes, git will run this script right after merge.

base_dir="misc/git-hook"

if [ ! -d "$base_dir" ]
then
echo "Warning: cannot determine hooks base folder"
exit 0
fi

cp -rf "$base_dir/necessary/"* .git/hooks/;

for file in .git/hooks/*; do
if [ -d "${file}" ]
then
for subfile in .git/hooks/"${file##*/}"/*; do
if [ -e "$base_dir/optional/${file##*/}/${subfile##*/}" ]
then
cp -f "$base_dir/optional/${file##*/}/${subfile##*/}" "$subfile";
fi
done
else
if [ -e "$base_dir/optional/${file##*/}" ]
then
cp -f "$base_dir/optional/${file##*/}" "$file";
fi
fi
done

exit 0
75 changes: 75 additions & 0 deletions misc/git-hook/necessary/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

# Usage: add this file to your project's .git/hooks directory.
# Now, when you change some files in repository and try to commit these
# changes, git will run this script right before commit.

GITDIR=`git rev-parse --git-common-dir`
if [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then
GITDIR=`cygpath --unix $GITDIR`
fi

export GITDIR

if [ -f "./functions.bash" ]
then
. ./functions.bash
else
. $GITDIR/hooks/functions.bash
fi

echo ">>>>>>>> modifying begin <<<<<<<<"

files=`find $GITDIR/hooks/precommit/modify/* -type f -maxdepth 0 -mindepth 0`

for file in $files
do
echo "######## begin $file ########"

if [ -z `echo "$file" | sed 's/.*\.ps1$//g'` ]
then
runPSScript -File $file
else
$file
fi
result="$?"

if [ "$result" -ne 0 ]
then
echo "######## end $file: abort ########"
exit 1
fi

echo "######## end $file: continue ########"
done

echo ">>>>>>>> modifying complete <<<<<<<<"

echo ">>>>>>>> checks begin <<<<<<<<"

files=`find $GITDIR/hooks/precommit/check/* -type f -maxdepth 0 -mindepth 0`

for file in $files
do
echo "######## begin $file ########"

if [ -z `echo "$file" | sed 's/.*\.ps1$//g'` ]
then
runPSScript -File $file
else
$file
fi
result="$?"

if [ "$result" -ne 0 ]
then
echo "######## end $file: abort ########"
exit 1
fi

echo "######## end $file: continue ########"
done

echo ">>>>>>>> checks complete <<<<<<<<"

exit 0
38 changes: 38 additions & 0 deletions misc/git-hook/necessary/precommit/check/indent
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

. $GITDIR/hooks/functions.bash

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# We should check only added or modified C/C++ source files for tabs.
changed_files=$(git diff-index --cached $against | grep -E '[MA] .*\.(c|cpp|cc|cxx|h|hh)$' | cut -d' ' -f 2)

issue_found=0

IFS=$'\n' # bash specific
for file in $changed_files
do
result=`cat "$file" | grep -P -n '^[\t]*( | {1,3}\t)'`

if [ -n "$result" ]
then
echo "file with indent issue: $file"
echo "$result"
issue_found=1
else
echo "file without issue: $file"
fi
done

if [ $issue_found = 1 ]
then
exit 1
fi

exit 0
37 changes: 37 additions & 0 deletions misc/git-hook/necessary/precommit/check/lineendings
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

. $GITDIR/hooks/functions.bash

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# We should check only added or modified files of these given types for line endings.
changed_files=$(git diff-index --cached $against | grep -E '([MA] .*\.(c|cpp|cc|cxx|h|hh)$)' | cut -f 2)
issue_found=0

IFS=$'\n' # bash specific
for file in $changed_files
do
result=`cat "$file" | grep -E -n '[[:space:]]$'`

if [ -n "$result" ]
then
echo "file with line ending issue: $file"
echo "$result"
issue_found=1
else
echo "file without issue: $file"
fi
done

if [ $issue_found = 1 ]
then
exit 1
fi

exit 0
34 changes: 34 additions & 0 deletions misc/git-hook/optional/precommit/modify/remove_trailing_spaces
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

. $GITDIR/hooks/functions.bash

process()
{
file="$1"

mv "$file" "$file.orig"
cat "$file.orig" | sed 's/[ \t]\+$//g' > "$file"
rm "$file.orig"
git add "$file"
}

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

changed_files=$(git diff-index --cached $against | grep -i -E '[MA] .*\.(c|cc|cpp|h|hh)$' | cut -d' ' -f 2)

if [ -n "$changed_files" ]
then
echo "$changed_files" | while read file
do
echo "process \"$file\""
process "$file"
done
fi

exit 0
34 changes: 34 additions & 0 deletions misc/git-hook/optional/precommit/modify/spaces2tabs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

. $GITDIR/hooks/functions.bash

process()
{
file="$1"

mv "$file" "$file.orig"
cat "$file.orig" | sed 's/^\([\t]*\) /\1\t/g' | sed 's/^\([\t]*\) \{1,3\}\t/\1\t/g' > "$file"
rm "$file.orig"
git add "$file"
}

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

changed_files=$(git diff-index --cached $against | grep -i -E '[MA] .*\.(c|cc|cpp|h|hh)$' | cut -d' ' -f 2)

if [ -n "$changed_files" ]
then
echo "$changed_files" | while read file
do
echo "process \"$file\""
process "$file"
done
fi

exit 0

0 comments on commit bee2ff4

Please sign in to comment.