Skip to content

Commit

Permalink
Add script for pre-commit git hook
Browse files Browse the repository at this point in the history
The script needs to be copied to .git/hooks/ in order to work.

Resolves #50
  • Loading branch information
goodmami committed Sep 10, 2017
1 parent 51d0f13 commit f9cbfe8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

Recent changes not yet part of a release are listed here:

* ...
* Add `utils/git-hooks/pre-commit` for auto-updating Version.lsp; Note
that it still needs to be copied locally to `.git/hooks/pre-commit`
in order to work

## [2017-09-08][]

Expand Down
2 changes: 1 addition & 1 deletion Version.lsp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(in-package :lkb)

(defparameter *grammar-version* "Jacy (2017-09-08-develop)")
(defparameter *grammar-version* "Jacy-develop (2017-09-10 07:11:19+00:00)")

19 changes: 19 additions & 0 deletions utils/git-hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Git Hooks

This directory contains hooks for Git that developers are encouraged
to use when working on the grammar.

To use the hooks, please copy them into your `.git/hooks` directory.
For example:

```
$ cp utils/git-hooks/pre-commit .git/hooks/pre-commit
```

## pre-commit

The pre-commit hook checks for things before a commit happens. It can
be used, e.g., to check if a grammar compiles or passes regression
tests. The script here only checks if Version.lsp has been updated;
if not, it prompts the user to update it and commit, commit anyway, or
abort the commit entirely.
49 changes: 49 additions & 0 deletions utils/git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# If Version.lsp is not changed, prompt the user to use the current
# date and time as the version.

# if branch is not master, add it to the version string
branch="-$( git rev-parse --abbrev-ref HEAD )"
if "$branch" = "-master"; then
branch=''
fi

newver="Jacy$branch ($( date --utc --rfc-3339=seconds))"

exitcode=0 # 0 is success; otherwise commit is cancelled

if ! $( git diff --cached --name-only --diff-filter=AM | grep -q '^Version.lsp$' ); then

echo "Version.lsp is unchanged. Do you wish to update the version string"
echo "to \"$newver\" ?"
echo " [y]es : update and commit"
echo " [n]o : don't update, commit anyway"
echo " [a]bort : don't commit"

exec < /dev/tty # get keyboard input
while true; do
read -n 1 -p '(y|n|a) ' choice
echo ""
case "$choice" in
[yY])
sed -i "s/^(defparameter \*grammar-version\*.*/(defparameter *grammar-version* \"$newver\")/" Version.lsp
git add Version.lsp
break
;;
[nN])
break
;;
[aA])
echo "Aborting commit"
exitcode=1
break
;;
*)
echo "Invalid option"
;;
esac
done
fi

exit "$exitcode"

0 comments on commit f9cbfe8

Please sign in to comment.