Wednesday 31 July 2019

Checkout file original timestamp for files from Git

For people like me used to use cvs for version control, when switching to Git, one of the thing bothers me a lot is file's origional timestamp. In case you want to checkout the files origional create/commit timestamp, here is my trick.
After checkout, run the script below, it will get the original create/commit timestamp from Git db.

#!/bin/bash -e

PATH=/usr/bin:/bin
unalias -a

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

update_file_timestamp() {
  file_time=$(git show --pretty=format:%ai --abbrev-commit "$(get_file_rev "$1")" | head -n 1)
  touch -d "$file_time" "$1"
}

OLD_IFS=$IFS
IFS=$'\n'

for file in `git ls-files`
do
  if [ -f "$file" ] ; then
    update_file_timestamp "$file"
  fi
done

IFS=$OLD_IFS

git update-index --refresh

0 comments:

Post a Comment