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