summaryrefslogtreecommitdiff
path: root/localgit-update-file-timestamps
blob: c0497c94ec41eec924a79e546d4d86245ab62a37 (plain)
  1. #!/bin/sh
  2. set -e
  3. ####
  4. # Helper script to update the Last modified timestamp of files in a Git SCM
  5. # Projects working Copy
  6. #
  7. # When you clone a Git repository, it sets the timestamp of all the files to the
  8. # time when you cloned the repository.
  9. #
  10. # This becomes a problem when you want the cloned repository, which is part of a
  11. # Web application have a proper cacheing mechanism so that it can re-cache files
  12. # (into a webtree) that have been modified since the last cache.
  13. #
  14. # @see http://stackoverflow.com/questions/1964470/whats-the-equivalent-of-use-commit-times-for-git
  15. #
  16. # Author: Jeffery Fernandez <jeffery@fernandez.net.au>
  17. ####
  18. #
  19. # Original source: http://www.jefferyfernandez.id.au/2011/07/30/update-timestamp-of-files-in-checked-out-git-repository/
  20. # Make sure we are not running this on a bare Repository
  21. REPO_TYPE=`git config --list|egrep ^core.bare | awk -F '=' '{ print $2 }'`
  22. if [ "$REPO_TYPE" = "true" ]
  23. then
  24. echo "Cannot run this script on a bare Repository" && exit 1
  25. fi
  26. echo "Updating Git Repository Last Modified Time-stamp"
  27. # Obtain the Operating System
  28. OS=${OS:-`uname`}
  29. # Get the last revision hash of a particular file in the git repository
  30. getFileLastRevision()
  31. {
  32. git rev-list HEAD "$1" | head -n 1
  33. }
  34. # Extract the actual last modified timestamp of the file and Update the time-stamp
  35. updateFileTimeStamp()
  36. {
  37. # Extract the file revision
  38. FILE_REVISION_HASH=`getFileLastRevision "$1"`
  39. # Get the File last modified time
  40. FILE_MODIFIED_TIME=`git show --pretty=format:%ai --abbrev-commit ${FILE_REVISION_HASH} | head -n 1`
  41. # Extract the last modified timestamp, differently for Linux, FreeBSD and Mac OS X
  42. if [ "$OS" = 'Linux' ]
  43. then
  44. # for displaying the date in readable format
  45. #FORMATTED_TIMESTAMP=`date --date="${FILE_MODIFIED_TIME}" +'%d-%m-%Y %H:%M:%S %z'`
  46. #echo "Modified: ${FILE_MODIFIED_TIME} | ${FORMATTED_TIMESTAMP} > ${1}"
  47. # Modify the last modified timestamp
  48. touch -d "${FILE_MODIFIED_TIME}" $2
  49. elif [ "$OS" = 'Darwin' ] || [ "$OS" = 'FreeBSD' ]
  50. then
  51. # Format the date for updating the timestamp
  52. FORMATTED_TIMESTAMP=`date -j -f '%Y-%m-%d %H:%M:%S %z' "${FILE_MODIFIED_TIME}" +'%Y%m%d%H%M.%S'`
  53. #echo "Modified: ${FILE_MODIFIED_TIME} | ${FORMATTED_TIMESTAMP} > ${1}"
  54. # Modify the last modified timestamp
  55. touch -t "${FORMATTED_TIMESTAMP}" $2
  56. else
  57. echo "Unknown Operating System to perform timestamp update" >&2
  58. exit 1
  59. fi
  60. }
  61. # Backup and update the "Internal Field Separator" to a newline. This is so that
  62. # we can deal with spaces in file names in the for loop below
  63. IFS_BAK=$IFS
  64. IFS="
  65. "
  66. # Loop through and fix timestamps on all files in our checked-out repository
  67. for file in $(git ls-files)
  68. do
  69. updateFileTimeStamp "${file}" "${file}"
  70. done
  71. # Revert the default delimiter
  72. IFS=$IFS_BAK
  73. IFS_BAK=