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
8 populate
= # Set to 1 if we shall seed local branches with this
20 echo "Usage: ${tgname:-tg} [...] remote [--populate] [<remote>]" >&2
30 git config
"remote.$name.url" >/dev
/null || die
"unknown remote '$name'"
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
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
46 while read -r rn
&& [ -n "$rn" ]; do
48 "refs/remotes/$name/{top-bases}"/?
*)
50 "refs/remotes/$name/top-bases"/?
*)
53 [ "$sawnew$sawold" != "11" ] ||
break
55 $(git for-each-ref --format='%(refname)' "refs/remotes/$name/{top-bases}" "refs/remotes/$name/top-bases")
57 if [ "$sawold$sawnew" = "11" ]; then
58 err
"remote \"$name\" has top-bases in both locations:"
59 err
" refs/remotes/$name/{top-bases}/..."
60 err
" refs/remotes/$name/top-bases/..."
61 err
"set \"topgit.top-bases\" to \"heads\" for the first, preferred location"
62 err
"or set \"topgit.top-bases\" to \"refs\" for the second, old location"
63 err
"(the \"-c topgit.top-bases=<val>\" option can be used for this)"
64 err
"then re-run the tg remote command"
65 err
"(the tg migrate-bases command can also help with this problem)"
66 die
"schizophrenic remote \"$name\" requires topgit.top-bases setting"
68 if [ -n "$sawold$sawnew" ]; then
70 [ -z "$sawold" ] || val
="refs"
71 GIT_CONFIG_PARAMETERS
="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }'topgit.top-bases=$val'"
72 export GIT_CONFIG_PARAMETERS
78 ## Configure the remote
80 git config
--replace-all "remote.$name.fetch" "+refs/$topbases/*:refs/remotes/$name/${topbases#heads/}/*" \
81 "\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$name/(top-bases|[{]top-bases[}])/\\*"
83 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
84 info
"Probably you want to remove the push specs introduced by an old version of topgit:"
85 info
' git config --unset-all "remote.'"$name"'.push" "\\+refs/top-bases/\\*:refs/top-bases/\\*"'
86 info
' git config --unset-all "remote.'"$name"'.push" "\\+refs/heads/\\*:refs/heads/\\*"'
87 info
'(or use git config --bool --add topgit.dontwarnonoldpushspecs true to get rid of this warning)'
90 info
"Remote $name can now follow TopGit topic branches."
91 if [ -z "$populate" ]; then
92 info
"Next, do: git fetch $name"
97 ## Populate local branches
99 info
"Populating local topic branches from remote '$name'..."
101 ## The order of refspecs is very important, because both heads and
102 ## $topbases are mapped under the same namespace refs/remotes/$name.
103 ## If we put the 2nd refspec before the 1st one, stale refs reverse
104 ## lookup would fail and "refs/remotes/$name/$topbases/XX" reverse
105 ## lookup as a non-exist "refs/heads/$topbases/XX", and would be
106 ## deleted by accident.
107 [ -n "$fetchdone" ] || git fetch
--prune "$name" \
108 "+refs/$topbases/*:refs/remotes/$name/${topbases#heads/}/*" \
109 "+refs/heads/*:refs/remotes/$name/*"
111 git for-each-ref
--format='%(objectname) %(refname)' "refs/remotes/$name/${topbases#heads/}" |
112 while read rev ref
; do
113 branch
="${ref#refs/remotes/$name/${topbases#heads/}/}"
114 if ! git rev-parse
--verify "refs/remotes/$name/$branch" -- >/dev
/null
2>&1; then
115 info
"Skipping remote $name/${topbases#heads/}/$branch that's missing its branch"
118 if git rev-parse
--verify "refs/heads/$branch" -- >/dev
/null
2>&1; then
119 git rev-parse
--verify "refs/$topbases/$branch" -- >/dev
/null
2>&1 ||
{
120 init_reflog
"refs/$topbases/$branch"
121 git update-ref
"refs/$topbases/$branch" "$rev"
123 info
"Skipping branch $branch: Already exists"
126 info
"Adding branch $branch..."
127 init_reflog
"refs/$topbases/$branch"
128 git update-ref
"refs/$topbases/$branch" "$rev"
129 git update-ref
"refs/heads/$branch" "$(git rev-parse --verify "$name/$branch" --)"
132 git config
"topgit.remote" "$name"
133 info
"The remote '$name' is now the default source of topic branches."