sh 设置文件上次修改时间以匹配它的git commit timestamp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 设置文件上次修改时间以匹配它的git commit timestamp相关的知识,希望对你有一定的参考价值。

#!/bin/bash -e
####
# based on http://www.clock.co.uk/blog/a-guide-on-how-to-cache-npm-install-with-docker
#
# Set's the last modified timestamp of a file to it's repositories commit timestamp. 
# 
# Particularly useful with docker when building after a new git checkout has been made,
# can improve docker build times for composer, bower, npm, etc
#
# @see https://github.com/docker/docker/issues/3556
####

usage()
{
    echo "Usage: git-timestamp <file>"
    echo "Set a files last modified time to match it's git commit timestamp."
}

if test $# = 0; then
    usage
    exit
fi

FILE=$1

if [ ! -f $FILE ]; then
    echo "File not found!"
    exit
fi

REV=$(git rev-list -n 1 HEAD "$FILE");
STAMP=$(git show --pretty=format:%ai --abbrev-commit "$REV" | head -n 1);
touch -d "$STAMP" $FILE;

echo "Set $FILE to $STAMP"

以上是关于sh 设置文件上次修改时间以匹配它的git commit timestamp的主要内容,如果未能解决你的问题,请参考以下文章

sh 使用Sublime Text打开上次提交的所有修改过的文件

我可以在更新gitattributes后使用Git批量更改行尾吗?

sh 将子目录结构的上次修改日期更新为当前时间

git常用命令总结

git配置文件—— .gitattributes

git clone 更改文件修改时间