Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #! /usr/bin/env bash
  2. # Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
  3. # Licensed under GPL v3 or later
  4. #
  5. # 2011-04-01
  6.  
  7. GIT='git --no-pager'
  8.  
  9. # Require Git repository in reach
  10. ${GIT} show --oneline >/dev/null || exit 1
  11.  
  12. # Command line parameters
  13. [[ $# == 0 ]] && tags=$(git tag -l) || tags=$@
  14.  
  15. abbrev() {
  16. sha1=$1
  17. ${GIT} show -s --format=%h "${sha1}"
  18. }
  19.  
  20. is_tag_object() {
  21. tag=$1
  22. [ $(${GIT} cat-file -t "${tag}") == tag ] && echo true || echo false
  23. }
  24.  
  25. main() {
  26. for tag in ${tags}; do
  27. tag_ref=refs/tags/"${tag}"
  28.  
  29. # Valid tag?
  30. ${GIT} rev-parse "${tag_ref}" &>/dev/null || {
  31. echo "Tag '${tag}' not found, skipping" 1>&2
  32. continue
  33. }
  34.  
  35. # Plain tag or tag object?
  36. tag_object=$(is_tag_object "${tag_ref}")
  37. if ${tag_object}; then
  38. cat_file_output=$(${GIT} cat-file tag "${tag}")
  39. commit_sha1=$(head -n 1 <<<"${cat_file_output}")
  40. commit_sha1=${commit_sha1##object }
  41. original_commit=${commit_sha1}
  42. else
  43. original_commit=$(${GIT} rev-parse ${tag_ref})
  44. fi
  45.  
  46. # Get contained tree SHA1
  47. tree_sha1=$(${GIT} show -s '--format=%T' "${original_commit}")
  48.  
  49. # Get earliest commit with same tree SHA1
  50. earliest_commit=$(${GIT} rev-list --all --topo-order \
  51. '--format=%H %T' \
  52. | fgrep "${tree_sha1}" | tail -n 1 | cut -f 1)
  53.  
  54. if [[ -z "${earliest_commit}" ]]; then
  55. echo "Sanity check failed, internal error" 1>&2
  56. exit 1
  57. fi
  58.  
  59. # Work to do?
  60. if [[ "${earliest_commit}" == "${original_commit}" ]]; then
  61. printf "Tag '%s' (%s) already at earliest commit (%s), skipping\n" \
  62. "${tag}" \
  63. $(${tag_object} && echo object || echo plain) \
  64. $(abbrev "${original_commit}")
  65. continue
  66. fi
  67.  
  68. if ${tag_object}; then
  69. tagger_line=$(tail -n +4 <<<"${cat_file_output}" | head -n 1)
  70. tagger_line=${tagger_line##tagger }
  71.  
  72. tag_message=$(tail -n +6 <<<"${cat_file_output}")
  73. committer_name=$(sed 's/^\(.\+\) <\([^>]\+\)\+> \([0-9]\+ [+-][0-9]\{4\}\)$/\1/' <<<"${tagger_line}")
  74. committer_email=$(sed 's/^\(.\+\) <\([^>]\+\)\+> \([0-9]\+ [+-][0-9]\{4\}\)$/\2/' <<<"${tagger_line}")
  75. committer_date=$(sed 's/^\(.\+\) <\([^>]\+\)\+> \([0-9]\+ [+-][0-9]\{4\}\)$/\3/' <<<"${tagger_line}")
  76.  
  77. GIT_COMMITTER_NAME="${committer_name}" \
  78. GIT_COMMITTER_EMAIL="${committer_email}" \
  79. GIT_COMMITTER_DATE="${committer_date}" \
  80. ${GIT} tag -f -a -m "${tag_message}" "${tag}" "${earliest_commit}"
  81. else
  82. ${GIT} tag -f "${tag}" "${earliest_commit}"
  83. fi
  84. done
  85. }
  86.  
  87. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement