Skip to content

Commit

Permalink
Merge pull request #55 from greg0ire/submodules
Browse files Browse the repository at this point in the history
Get git directory programmatically
  • Loading branch information
greg0ire committed May 10, 2018
2 parents 8a11881 + 7ddc096 commit 0a609bc
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 45 deletions.
9 changes: 3 additions & 6 deletions cleanup-2-x-x-mess.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/bin/bash -eu
main()
{
local templateDir=$(git config --get --path init.templatedir)
local gitDir
gitDir=$(git rev-parse --git-dir)

if [ ! -d .git ]
then
echo "This script is supposed to be run at the root of a git repository" >&2
fi
rm -rf .git/tests .git/docs .git/mkdocs.yml .git/README.md .git/LICENSE .git/CONTRIBUTING.md .git/update.sh
rm -rf "$gitDir/{tests,docs,mkdocs.yml,README.md,LICENSE,CONTRIBUTING.md,update.sh}"
}
main
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Just run the configuration script. From your repository root, run

.git/configure.sh
"$(git rev-parse --git-dir)/configure.sh"

The script will help you configure some scripts that shouldn't be configured globally.

Expand Down
11 changes: 4 additions & 7 deletions template/configure.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#!/bin/bash
. .git/hooks/hook_switcher.sh

readonly PROGNAME=$(basename "$0")
readonly PROGDIR="$(cd "$(dirname "$0")"; pwd)"
readonly GIT_DIR="$(git rev-parse --git-dir)"
export GIT_DIR

. "$GIT_DIR/hooks/hook_switcher.sh"

main() {
local hookName
if [ ! -d .git ]
then
echo "This script should be run from the root of a repository" >&2
exit 1
fi
for directory in $(find "$PROGDIR/hooks" -type d | sort | \
awk '$0 !~ last "/" {print last} {last=$0} END {print last}')
do
hookName=${directory#$PROGDIR/hooks/}
echo "Configuring $hookName"
switch_hook "$hookName"
find "$directory" -name configure.sh -exec {} \;

done
}

Expand Down
12 changes: 7 additions & 5 deletions template/hooks/change_detector.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
has_changed()
{
local hook_type=$1; shift
local monitored_paths=("$@")
local monitored_paths="$@"
local against
local changed
local workDir
set -f

monitored_paths=( "${monitored_paths[@]/#/$GIT_DIR/../}" )
workDir=$(git rev-parse --show-toplevel)

case $hook_type in
post-commit)
Expand All @@ -22,12 +24,12 @@ has_changed()
fi
changed="$(git diff-tree $against 'HEAD' \
--stat \
-- ${monitored_paths[*]}| wc -l)"
-- ${monitored_paths}| wc -l)"
;;
post-checkout | post-merge )
changed="$(git diff 'HEAD@{1}' \
--stat \
-- ${monitored_paths[*]}| wc -l)"
-- ${monitored_paths}| wc -l)"
;;
pre-commit)
if git rev-parse --verify HEAD >/dev/null 2>&1
Expand All @@ -39,7 +41,7 @@ has_changed()
fi
changed="$(git diff-index \
--name-status $against \
-- "${monitored_paths[*]}" | wc -l)"
-- ${monitored_paths} | wc -l)"
;;
esac

Expand Down
2 changes: 1 addition & 1 deletion template/hooks/php/composer/react.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
. .git/hooks/git_config_wrapper.sh
. $GIT_DIR/hooks/git_config_wrapper.sh

function react()
{
Expand Down
2 changes: 1 addition & 1 deletion template/hooks/php/cs-fixer/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

if has_changed pre-commit '**/*.php' '*.php'
then
.git/hooks/php/cs-fixer/fix-cs
"$GIT_DIR/hooks/php/cs-fixer/fix-cs"
fi
4 changes: 2 additions & 2 deletions template/hooks/php/ctags/configure.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
. .git/hooks/hook_switcher.sh
#!/bin/bash -u
. "$GIT_DIR/hooks/hook_switcher.sh"

main() {
local projectType
Expand Down
2 changes: 1 addition & 1 deletion template/hooks/php/ctags/post-checkout
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
.git/hooks/php/ctags/update-ctags >/dev/null 2>&1 &
"$GIT_DIR/hooks/php/ctags/update-ctags" >/dev/null 2>&1 &
2 changes: 1 addition & 1 deletion template/hooks/php/ctags/post-commit
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
.git/hooks/php/ctags/update-ctags >/dev/null 2>&1 &
"$GIT_DIR/hooks/php/ctags/update-ctags" >/dev/null 2>&1 &
2 changes: 1 addition & 1 deletion template/hooks/php/ctags/post-merge
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
.git/hooks/php/ctags/update-ctags >/dev/null 2>&1 &
"$GIT_DIR/hooks/php/ctags/update-ctags" >/dev/null 2>&1 &
13 changes: 8 additions & 5 deletions template/hooks/php/ctags/update-ctags
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash -u
source .git/hooks/git_config_wrapper.sh
gitDir=$(git rev-parse --git-dir)
source "$gitDir/hooks/git_config_wrapper.sh"

new_flags_are_supported()
{
Expand All @@ -9,6 +10,8 @@ new_flags_are_supported()
build_ctags_command()
{
local tagKinds
local gitDir
gitDir=$(git rev-parse --git-dir)
get_hook_config php-ctags tag-kinds tagKinds optional
if [[ $? -ne 0 ]]
then
Expand All @@ -20,14 +23,14 @@ build_ctags_command()
ctags
-h ".php"
--recurse
-f.git/tags.$$
-f$gitDir/tags.$$
--languages=php
--totals=yes
--tag-relative=yes
--PHP-kinds=${tagKinds}
--exclude="*.js"
--exclude=".svn"
--exclude=".git"
--exclude="$gitDir"
--exclude=".rsync_cache"
--exclude="vendor/*/vendor"
--exclude="vendor/*/Tests"
Expand Down Expand Up @@ -61,7 +64,7 @@ build_ctags_command()
eval $command
}

trap "rm --force .git/tags.$$" EXIT
trap "rm --force $gitDir/tags.$$" EXIT
build_ctags_command

mv .git/tags.$$ .git/tags
mv "$gitDir/tags.$$" "$gitDir/tags"
2 changes: 1 addition & 1 deletion template/hooks/php/sismo/build-project
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
. .git/hooks/git_config_wrapper.sh
. $GIT_DIR/hooks/git_config_wrapper.sh
get_hook_config php-sismo path path required
path_is_defined=$?
get_hook_config php-sismo slug slug required
Expand Down
2 changes: 1 addition & 1 deletion template/hooks/php/sismo/configure.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
. .git/hooks/hook_switcher.sh
. $GIT_DIR/hooks/hook_switcher.sh

main() {
if hook_is_enabled php/sismo
Expand Down
2 changes: 1 addition & 1 deletion template/hooks/php/sismo/post-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
.git/hooks/php/sismo/build-project
$GIT_DIR/hooks/php/sismo/build-project

2 changes: 1 addition & 1 deletion template/hooks/post-rewrite
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
case "$1" in
rebase) exec .git/hooks/post-merge ;;
rebase) exec "$(git rev-parse --git-dir)/hooks/post-merge" ;;
esac
5 changes: 3 additions & 2 deletions template/hooks/pre-push
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/bin/bash
gitDir=$(git rev-parse --git-dir)
for enabled_plugin in $(git config --get-all hooks.enabled-plugins)
do
if [ -f ".git/hooks/$enabled_plugin/pre-push" ]
if [ -f "$gitDir/hooks/$enabled_plugin/pre-push" ]
then
".git/hooks/$enabled_plugin/pre-push"
"$gitDir/hooks/$enabled_plugin/pre-push"
ret=$?
if [ $ret -ne 0 ]
then
Expand Down
5 changes: 3 additions & 2 deletions tests/all.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash
if [ -f .git/hooks/configure.sh ]
gitDir=$(git rev-parse --git-dir)
if [ -f "$gitDir/hooks/configure.sh" ]
then
echo "Huh ? Copy failed or what ?"
ls .git/hooks
ls $gitDir/hooks
ls "$(git config --path --get init.templatedir)"
exit 1
fi
Expand Down
9 changes: 3 additions & 6 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
main()
{
local templateDir=$(git config --get --path init.templatedir)
local gitDir=$(git rev-parse --git-dir)

if [ ! -d .git ]
then
echo "This script is supposed to be run at the root of a git repository" >&2
fi
rsync --archive --verbose --compress --cvs-exclude "$templateDir/hooks/" .git/hooks --delete
cp -f "$templateDir/configure.sh" .git
rsync --archive --verbose --compress --cvs-exclude "$templateDir/hooks/" "$gitDir/hooks" --delete
cp -f "$templateDir/configure.sh" "$gitDir"
}
main

0 comments on commit 0a609bc

Please sign in to comment.