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