topgit: fully qualify more refs
[topgit/pro.git] / tg-remote.sh
blob9082ef91faea3fe9446c5bea7f4bb083da43338b
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (C) Petr Baudis <pasky@suse.cz> 2008
4 # (C) Kyle J. McKay <mackyle@gmail.com> 2016,2017
5 # All rights reserved.
6 # GPLv2
8 populate= # Set to 1 if we shall seed local branches with this
9 name=
12 ## Parse options
14 while [ -n "$1" ]; do
15 arg="$1"; shift
16 case "$arg" in
17 --populate)
18 populate=1;;
19 -*)
20 echo "Usage: ${tgname:-tg} [...] remote [--populate] [<remote>]" >&2
21 exit 1;;
23 name="$arg";;
24 esac
25 done
27 [ -n "$name" ] ||
28 name="$base_remote"
30 git config "remote.$name.url" >/dev/null || die "unknown remote '$name'"
32 fetchdone=
33 if [ -n "$topbases_implicit_default" ]; then
34 # set $topbases based on remote bases as the local repository does not have
35 # any bases already present and has not explicitly set topgit.top-bases
36 if [ -n "$populate" ]; then
37 # Do the fetch now but fetch both old and new top-bases
38 fetchdone=1
39 git fetch --prune "$name" \
40 "+refs/top-bases/*:refs/remotes/$name/top-bases/*" \
41 "+refs/heads/*:refs/remotes/$name/*"
43 # see if we have any remote bases
44 rc=0 remotebases=
45 remotebases="$(
46 git for-each-ref --format='%(refname)' "refs/remotes/$name" 2>/dev/null |
47 run_awk_ref_prefixes -n -- "refs/remotes/$name/{top-bases}" "refs/remotes/$name/top-bases" "refs/remotes/$name")" ||
48 rc=$?
49 if [ "$rc" = "65" ]; then
50 err "remote \"$name\" has top-bases in both locations:"
51 err " refs/remotes/$name/{top-bases}/..."
52 err " refs/remotes/$name/top-bases/..."
53 err "set \"topgit.top-bases\" to \"heads\" for the first, preferred location"
54 err "or set \"topgit.top-bases\" to \"refs\" for the second, old location"
55 err "(the \"-c topgit.top-bases=<val>\" option can be used for this)"
56 err "then re-run the tg remote command"
57 err "(the tg migrate-bases command can also help with this problem)"
58 die "schizophrenic remote \"$name\" requires topgit.top-bases setting"
60 if [ -n "$remotebases" ]; then
61 val="heads"
62 [ "$remotebases" = "refs/remotes/$name/{top-bases}" ] || val="refs"
63 GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }'topgit.top-bases=$val'"
64 export GIT_CONFIG_PARAMETERS
65 unset tg_topbases_set
66 set_topbases
70 ## Configure the remote
72 git config --replace-all "remote.$name.fetch" "+refs/$topbases/*:refs/remotes/$name/${topbases#heads/}/*" \
73 "\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$name/(top-bases|[{]top-bases[}])/\\*"
75 if git config --get-all "remote.$name.push" "\\+refs/top-bases/\\*:refs/top-bases/\\*" >/dev/null && test "xtrue" != "x$(git config --bool --get topgit.dontwarnonoldpushspecs)"; then
76 info "Probably you want to remove the push specs introduced by an old version of topgit:"
77 info ' git config --unset-all "remote.'"$name"'.push" "\\+refs/top-bases/\\*:refs/top-bases/\\*"'
78 info ' git config --unset-all "remote.'"$name"'.push" "\\+refs/heads/\\*:refs/heads/\\*"'
79 info '(or use git config --bool --add topgit.dontwarnonoldpushspecs true to get rid of this warning)'
82 info "Remote $name can now follow TopGit topic branches."
83 if [ -z "$populate" ]; then
84 info "Next, do: git fetch $name"
85 exit
89 ## Populate local branches
91 info "Populating local topic branches from remote '$name'..."
93 ## The order of refspecs is very important, because both heads and
94 ## $topbases are mapped under the same namespace refs/remotes/$name.
95 ## If we put the 2nd refspec before the 1st one, stale refs reverse
96 ## lookup would fail and "refs/remotes/$name/$topbases/XX" reverse
97 ## lookup as a non-exist "refs/heads/$topbases/XX", and would be
98 ## deleted by accident.
99 [ -n "$fetchdone" ] || git fetch --prune "$name" \
100 "+refs/$topbases/*:refs/remotes/$name/${topbases#heads/}/*" \
101 "+refs/heads/*:refs/remotes/$name/*"
103 git for-each-ref --format='%(objectname) %(refname)' "refs/remotes/$name/${topbases#heads/}" |
104 while read rev ref; do
105 branch="${ref#refs/remotes/$name/${topbases#heads/}/}"
106 if ! git rev-parse --verify "refs/remotes/$name/$branch^0" -- >/dev/null 2>&1; then
107 info "Skipping remote $name/${topbases#heads/}/$branch that's missing its branch"
108 continue
110 if git rev-parse --verify "refs/heads/$branch^0" -- >/dev/null 2>&1; then
111 git rev-parse --verify "refs/$topbases/$branch^0" -- >/dev/null 2>&1 || {
112 init_reflog "refs/$topbases/$branch"
113 git update-ref "refs/$topbases/$branch" "$rev^0"
115 info "Skipping branch $branch: Already exists"
116 continue
118 info "Adding branch $branch..."
119 init_reflog "refs/$topbases/$branch"
120 git update-ref "refs/$topbases/$branch" "$rev^0"
121 git update-ref "refs/heads/$branch" "$(git rev-parse --verify "refs/remotes/$name/$branch^0" --)"
122 done
124 git config "topgit.remote" "$name"
125 info "The remote '$name' is now the default source of topic branches."