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