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