aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/git_untouch.sh
blob: c9d3ede58882df78bbe8cc1104a62a82e411df68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash -e

# From http://stackoverflow.com/questions/1964470/whats-the-equivalent-of-use-commit-times-for-git
# Modified to only run on files passed on command line

OS=${OS:-`uname`}

get_file_rev() {
    git rev-list -n 1 HEAD "$1"
}    

if [ "$OS" = 'FreeBSD' ]
then
    update_file_timestamp() {
        file_time=`date -r "$(git show --pretty=format:%at --abbrev-commit "$(get_file_rev "$1")" | head -n 1)" '+%Y%m%d%H%M.%S'`
        touch -h -t "$file_time" "$1"
    }    
else    
    update_file_timestamp() {
        file_time=`git show --pretty=format:%ai --abbrev-commit "$(get_file_rev "$1")" | head -n 1`
        touch -d "$file_time" "$1"
    }    
fi    

OLD_IFS=$IFS
IFS=$'\n'

for file in $@
do
    if [ ! `git status --ignored --porcelain $file` ]
    then
        update_file_timestamp "$file"
    fi
done

IFS=$OLD_IFS

git update-index --refresh &> /dev/null || true

exit 0