tg.sh: fix tg update --abort and clarify --stop
[topgit/pro.git] / tg-remote.sh
blob9736cfde88e5198d069cd70925e4115577e829c0
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 sawnew=
45 sawold=
46 while read -r rn && [ -n "$rn" ]; do
47 case "$rn" in
48 "refs/remotes/$name/{top-bases}"/?*)
49 sawnew=1;;
50 "refs/remotes/$name/top-bases"/?*)
51 sawold=1;;
52 esac
53 [ "$sawnew$sawold" != "11" ] || break
54 done <<-EOT
55 $(git for-each-ref --format='%(refname)' "refs/remotes/$name/{top-bases}" "refs/remotes/$name/top-bases")
56 EOT
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
69 val="heads"
70 [ -z "$sawold" ] || val="refs"
71 GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }'topgit.top-bases=$val'"
72 export GIT_CONFIG_PARAMETERS
73 unset tg_topbases_set
74 set_topbases
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"
93 exit
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"
116 continue
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"
124 continue
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" --)"
130 done
132 git config "topgit.remote" "$name"
133 info "The remote '$name' is now the default source of topic branches."