t5030-update-newbranch.sh: test remote update new branch handling
[topgit/pro.git] / tg-remote.sh
blob9b18cc4645b938390d9dcfea56758a3c04ac58fd
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 remotebases=
45 check_remote_topbases "$name" remotebases
46 if [ -n "$remotebases" ]; then
47 val="heads"
48 [ "$remotebases" = "refs/remotes/$name/{top-bases}" ] || val="refs"
49 GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }'topgit.top-bases=$val'"
50 export GIT_CONFIG_PARAMETERS
51 unset tg_topbases_set
52 set_topbases
56 ## Configure the remote
58 git config --replace-all "remote.$name.fetch" "+refs/$topbases/*:refs/remotes/$name/${topbases#heads/}/*" \
59 "\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$name/(top-bases|[{]top-bases[}])/\\*"
61 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
62 info "Probably you want to remove the push specs introduced by an old version of topgit:"
63 info ' git config --unset-all "remote.'"$name"'.push" "\\+refs/top-bases/\\*:refs/top-bases/\\*"'
64 info ' git config --unset-all "remote.'"$name"'.push" "\\+refs/heads/\\*:refs/heads/\\*"'
65 info '(or use git config --bool --add topgit.dontwarnonoldpushspecs true to get rid of this warning)'
68 info "Remote $name can now follow TopGit topic branches."
69 if [ -z "$populate" ]; then
70 info "Next, do: git fetch $name"
71 exit
75 ## Populate local branches
77 info "Populating local topic branches from remote '$name'..."
79 ## The order of refspecs is very important, because both heads and
80 ## $topbases are mapped under the same namespace refs/remotes/$name.
81 ## If we put the 2nd refspec before the 1st one, stale refs reverse
82 ## lookup would fail and "refs/remotes/$name/$topbases/XX" reverse
83 ## lookup as a non-exist "refs/heads/$topbases/XX", and would be
84 ## deleted by accident.
85 [ -n "$fetchdone" ] || git fetch --prune "$name" \
86 "+refs/$topbases/*:refs/remotes/$name/${topbases#heads/}/*" \
87 "+refs/heads/*:refs/remotes/$name/*"
89 git for-each-ref --format='%(objectname) %(refname)' "refs/remotes/$name/${topbases#heads/}" |
90 while read rev ref; do
91 branch="${ref#refs/remotes/$name/${topbases#heads/}/}"
92 if ! git rev-parse --verify "refs/remotes/$name/$branch^0" -- >/dev/null 2>&1; then
93 info "Skipping remote $name/${topbases#heads/}/$branch that's missing its branch"
94 continue
96 if git rev-parse --verify "refs/heads/$branch^0" -- >/dev/null 2>&1; then
97 git rev-parse --verify "refs/$topbases/$branch^0" -- >/dev/null 2>&1 || {
98 init_reflog "refs/$topbases/$branch"
99 git update-ref "refs/$topbases/$branch" "$rev^0"
101 info "Skipping branch $branch: Already exists"
102 continue
104 info "Adding branch $branch..."
105 init_reflog "refs/$topbases/$branch"
106 git update-ref "refs/$topbases/$branch" "$rev^0"
107 git update-ref "refs/heads/$branch" "$(git rev-parse --verify "refs/remotes/$name/$branch^0" --)"
108 done
110 git config "topgit.remote" "$name"
111 info "The remote '$name' is now the default source of topic branches."