Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git post-merge & post-checkout hook for sync changed files timestamps with last commit time (Linux, FreeBSD, Mac OS)
Git hook post-merge and post-checkout for sync modification time to last commit time.
You can download scripts manually and install them yourself, or you can install it with command
> curl -s https://gist.githubusercontent.com/phpdude/9464925/raw/install | sh
This command will download post-merge script, chmod it, and create post-checkout symlink to post-merge hook.
You can also start this script manually with environment variable $GIT_MERGE_LIMIT=100 to updated timestamp of last 100 commits for example.
export GIT_MERGE_LIMIT=100
/path/to/my/project/.git/hooks/post-merge
#!/bin/sh
GIT="/usr/bin/env git"
CURL="/usr/bin/env curl"
SCRIPT="https://gist.githubusercontent.com/phpdude/9464925/raw/post-merge"
GITHOME=`$GIT rev-parse --show-toplevel`
if [ -z "$GITHOME" ]; then
exit
fi
echo "Changind current dir to Git hooks folder"
cd "$GITHOME"/.git/hooks/ || exit
echo "Downloading post-merge hook"
($CURL $SCRIPT -s > post-merge) || exit
echo "Installing post-merge hook and post-checkout"
chmod +x post-merge
ln -fs post-merge post-checkout
#!/bin/sh
OS=${OS:-`uname`}
if [ "$OS" = 'Linux' ]
then
update_file_timestamp() {
file_time=`git log --no-merges --format=%ai "$1" | head -n 1`
touch -d "$file_time" "$1"
}
elif [ "$OS" = 'FreeBSD' ] || [ "$OS" = 'Darwin' ];
then
update_file_timestamp() {
file_time=`date -r "$(git log --no-merges --format=%at "$1" | head -n 1)" '+%Y%m%d%H%M.%S'`
touch -h -t "$file_time" "$1"
}
else
echo "timestamp changing not implemented" >&2
exit 1
fi
LIMIT="-${GIT_MERGE_LIMIT:-1}"
for file in `git log --no-merges --no-commit-id --pretty="format:" --name-only $LIMIT`
do
if [ -f "$file" ]; then
update_file_timestamp "$file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment