gcc-git-customization.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/bin/sh
  2. # Script to add some local git customizations suitable for working
  3. # with the GCC git repository
  4. ask () {
  5. question=$1
  6. default=$2
  7. var=$3
  8. printf "%s [%s]? " "$question" "$default"
  9. read answer
  10. if [ "x$answer" = "x" ]
  11. then
  12. eval $var=\$default
  13. else
  14. eval $var=\$answer
  15. fi
  16. }
  17. # Add a git command to find the git commit equivalent to legacy SVN revision NNN
  18. git config alias.svn-rev '!f() { rev=$1; shift; git log --all --grep="^From-SVN: r\\?$rev\\b" "${@}"; } ; f'
  19. # Add git commands to convert git commit to monotonically increasing revision number
  20. # and vice versa
  21. git config alias.gcc-descr '!f() { "`git rev-parse --show-toplevel`/contrib/git-descr.sh" $@; } ; f'
  22. git config alias.gcc-undescr '!f() { "`git rev-parse --show-toplevel`/contrib/git-undescr.sh" $@; } ; f'
  23. git config alias.gcc-verify '!f() { "`git rev-parse --show-toplevel`/contrib/gcc-changelog/git_check_commit.py" $@; } ; f'
  24. git config alias.gcc-backport '!f() { "`git rev-parse --show-toplevel`/contrib/git-backport.py" $@; } ; f'
  25. git config alias.gcc-fix-changelog '!f() { "`git rev-parse --show-toplevel`/contrib/git-fix-changelog.py" $@; } ; f'
  26. git config alias.gcc-mklog '!f() { "`git rev-parse --show-toplevel`/contrib/mklog.py" $@; } ; f'
  27. git config alias.gcc-commit-mklog '!f() { "`git rev-parse --show-toplevel`/contrib/git-commit-mklog.py" "$@"; }; f'
  28. # Make diff on MD files use "(define" as a function marker.
  29. # Use this in conjunction with a .gitattributes file containing
  30. # *.md diff=md
  31. git config diff.md.xfuncname '^\(define.*$'
  32. # Tell git send-email where patches go.
  33. # ??? Maybe also set sendemail.tocmd to guess from MAINTAINERS?
  34. git config sendemail.to 'gcc-patches@gcc.gnu.org'
  35. set_user=$(git config --get "user.name")
  36. set_email=$(git config --get "user.email")
  37. if [ "x$set_user" = "x" ]
  38. then
  39. # Try to guess the user's name by looking it up in the password file
  40. new_user=$(getent passwd $(whoami) | awk -F: '{ print $5 }')
  41. if [ "x$new_user" = "x" ]
  42. then
  43. new_user="(no default)"
  44. fi
  45. else
  46. new_user=$set_user
  47. fi
  48. ask "Your name" "${new_user}" new_user
  49. if [ "x$new_user" = "x(no default)" ]
  50. then
  51. echo "Cannot continue, git needs to record your name against commits"
  52. exit 1
  53. fi
  54. if [ "x$set_email" = "x" ]
  55. then
  56. new_email="(no_default)"
  57. else
  58. new_email=$set_email
  59. fi
  60. ask "Your email address (for git commits)" "${new_email}" new_email
  61. if [ "x$new_email" = "x(no default)" ]
  62. then
  63. echo "Cannot continue, git needs to record your email address against commits"
  64. exit 1
  65. fi
  66. if [ "x$set_user" != "x$new_user" ]
  67. then
  68. git config "user.name" "$new_user"
  69. fi
  70. if [ "x$set_email" != "x$new_email" ]
  71. then
  72. git config "user.email" "$new_email"
  73. fi
  74. upstream=$(git config --get "gcc-config.upstream")
  75. if [ "x$upstream" = "x" ]
  76. then
  77. upstream="origin"
  78. fi
  79. ask "Local name for upstream repository" "origin" upstream
  80. v=$(git config --get-all "remote.${upstream}.fetch")
  81. if [ "x$v" = "x" ]
  82. then
  83. echo "Remote $upstream does not seem to exist as a remote"
  84. exit 1
  85. fi
  86. git config "gcc-config.upstream" "$upstream"
  87. remote_id=$(git config --get "gcc-config.user")
  88. if [ "x$remote_id" = "x" ]
  89. then
  90. # See if the url specifies the remote user name.
  91. url=$(git config --get "remote.$upstream.url")
  92. if [ "x$url" = "x" ]
  93. then
  94. # This is a pure guess, but for many people it might be OK.
  95. remote_id=$(whoami)
  96. else
  97. remote_id=$(echo $url | sed 's|^.*ssh://\(..*\)@gcc.gnu.org.*$|\1|')
  98. if [ x$remote_id = x$url ]
  99. then
  100. remote_id=$(whoami)
  101. fi
  102. fi
  103. fi
  104. ask "Account name on gcc.gnu.org (for your personal branches area)" "$remote_id" remote_id
  105. git config "gcc-config.user" "$remote_id"
  106. old_pfx=$(git config --get "gcc-config.userpfx")
  107. if [ "x$old_pfx" = "x" ]
  108. then
  109. old_pfx="me"
  110. fi
  111. echo
  112. echo "Local branch prefix for personal branches you want to share"
  113. echo "(local branches starting <prefix>/ can be pushed directly to your"
  114. ask "personal area on the gcc server)" $old_pfx new_pfx
  115. git config "gcc-config.userpfx" "$new_pfx"
  116. echo
  117. ask "Install prepare-commit-msg git hook for 'git commit-mklog' alias" yes dohook
  118. if [ "x$dohook" = xyes ]; then
  119. hookdir=`git rev-parse --git-path hooks 2>/dev/null`
  120. if [ $? -eq 0 ]; then
  121. if [ -f "$hookdir/prepare-commit-msg" ]; then
  122. echo " Moving existing prepare-commit-msg hook to prepare-commit-msg.bak"
  123. mv "$hookdir/prepare-commit-msg" "$hookdir/prepare-commit-msg.bak"
  124. fi
  125. install -c "`git rev-parse --show-toplevel`/contrib/prepare-commit-msg" "$hookdir"
  126. else
  127. echo " `git --version` is too old, cannot find hooks dir"
  128. fi
  129. fi
  130. # Scan the existing settings to see if there are any we need to rewrite.
  131. vendors=$(git config --get-all "remote.${upstream}.fetch" "refs/vendors/" | sed 's:.*refs/vendors/\([^/][^/]*\)/.*:\1:' | sort | uniq)
  132. url=$(git config --get "remote.${upstream}.url")
  133. pushurl=$(git config --get "remote.${upstream}.pushurl")
  134. for v in $vendors
  135. do
  136. echo "Migrating vendor \"$v\" to new remote \"vendors/$v\""
  137. git config --unset-all "remote.${upstream}.fetch" "refs/vendors/$v/"
  138. git config --unset-all "remote.${upstream}.push" "refs/vendors/$v/"
  139. git config "remote.vendors/${v}.url" "${url}"
  140. if [ "x$pushurl" != "x" ]
  141. then
  142. git config "remote.vendors/${v}.pushurl" "${pushurl}"
  143. fi
  144. git config --add "remote.vendors/${v}.fetch" "+refs/vendors/$v/heads/*:refs/remotes/vendors/${v}/*"
  145. git config --add "remote.vendors/${v}.fetch" "+refs/vendors/$v/tags/*:refs/tags/vendors/${v}/*"
  146. done
  147. # Convert the remote 'pfx' to users/pfx to avoid problems with ambiguous refs
  148. # on user branches
  149. old_remote=$(git config --get "remote.${old_pfx}.url")
  150. if [ -n "${old_remote}" ]
  151. then
  152. echo "Migrating remote \"${old_pfx}\" to new remote \"users/${new_pfx}\""
  153. # Create a dummy fetch rule that will cause the subsequent prune to remove the old remote refs.
  154. git config --replace-all "remote.${old_pfx}.fetch" "+refs/empty/*:refs/remotes/${old_pfx}/*"
  155. # Remove any remotes
  156. git remote prune ${old_pfx}
  157. git config --remove-section "remote.${old_pfx}"
  158. for br in $(git branch --list "${old_pfx}/*")
  159. do
  160. old_remote=$(git config --get "branch.${br}.remote")
  161. if [ "${old_remote}" = "${old_pfx}" ]
  162. then
  163. git config "branch.${br}.remote" "users/${new_pfx}"
  164. fi
  165. done
  166. fi
  167. echo "Setting up tracking for personal namespace $remote_id in remotes/users/${new_pfx}"
  168. git config "remote.users/${new_pfx}.url" "${url}"
  169. if [ "x$pushurl" != "x" ]
  170. then
  171. git config "remote.users/${new_pfx}.pushurl" "${pushurl}"
  172. fi
  173. git config --replace-all "remote.users/${new_pfx}.fetch" "+refs/users/${remote_id}/heads/*:refs/remotes/users/${new_pfx}/*" "refs/users/${remote_id}/heads/"
  174. git config --replace-all "remote.users/${new_pfx}.fetch" "+refs/users/${remote_id}/tags/*:refs/tags/users/${new_pfx}/*" "refs/users/${remote_id}/tags/"
  175. git config --replace-all "remote.users/${new_pfx}.push" "refs/heads/${new_pfx}/*:refs/users/${remote_id}/heads/*" "refs/users/${remote_id}"
  176. if [ "$old_pfx" != "$new_pfx" -a "$old_pfx" != "${upstream}" ]
  177. then
  178. git config --remove-section "remote.${old_pfx}"
  179. fi
  180. git config --unset-all "remote.${upstream}.fetch" "refs/users/${remote_id}/"
  181. git config --unset-all "remote.${upstream}.push" "refs/users/${remote_id}/"
  182. git fetch "users/${new_pfx}"