tg-revert.sh: convert any top-bases in TOPGIT REFS
[topgit/pro.git] / tg-remote.sh
blob3275ef2281494bf5b6c3d3847963fc0e908ca458
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 populate= # Set to 1 if we shall seed local branches with this
7 name=
10 ## Parse options
12 while [ -n "$1" ]; do
13 arg="$1"; shift
14 case "$arg" in
15 --populate)
16 populate=1;;
17 -*)
18 echo "Usage: ${tgname:-tg} [...] remote [--populate] [<remote>]" >&2
19 exit 1;;
21 name="$arg";;
22 esac
23 done
25 [ -n "$name" ] ||
26 name="$base_remote"
28 git config "remote.$name.url" >/dev/null || die "unknown remote '$name'"
31 ## Configure the remote
33 git config --replace-all "remote.$name.fetch" "+refs/$topbases/*:refs/remotes/$name/$topbases/*" "\\+refs/$topbases/\\*:refs/remotes/$name/$topbases/\\*"
35 if git config --get-all "remote.$name.push" "\\+refs/$topbases/\\*:refs/$topbases/\\*" >/dev/null && test "xtrue" != "x$(git config --bool --get topgit.dontwarnonoldpushspecs)"; then
36 info "Probably you want to remove the push specs introduced by an old version of topgit:"
37 info ' git config --unset-all "remote.'"$name"'.push" "\\+refs/'"$topbases"'/\\*:refs/'"$topbases"'/\\*"'
38 info ' git config --unset-all "remote.'"$name"'.push" "\\+refs/heads/\\*:refs/heads/\\*"'
39 info '(or use git config --bool --add topgit.dontwarnonoldpushspecs true to get rid of this warning)'
42 info "Remote $name can now follow TopGit topic branches."
43 if [ -z "$populate" ]; then
44 info "Next, do: git fetch $name"
45 exit
49 ## Populate local branches
51 info "Populating local topic branches from remote '$name'..."
53 ## The order of refspecs is very important, because both heads and
54 ## $topbases are mapped under the same namespace refs/remotes/$name.
55 ## If we put the 2nd refspec before the 1st one, stale refs reverse
56 ## lookup would fail and "refs/remotes/$name/$topbases/XX" reverse
57 ## lookup as a non-exist "refs/heads/$topbases/XX", and would be
58 ## deleted by accident.
59 git fetch --prune "$name" \
60 "+refs/$topbases/*:refs/remotes/$name/$topbases/*" \
61 "+refs/heads/*:refs/remotes/$name/*"
63 git for-each-ref --format='%(objectname) %(refname)' "refs/remotes/$name/$topbases" |
64 while read rev ref; do
65 branch="${ref#refs/remotes/$name/$topbases/}"
66 if ! git rev-parse --verify "refs/remotes/$name/$branch" -- >/dev/null 2>&1; then
67 info "Skipping remote $name/$topbases/$branch that's missing its branch"
68 continue
70 if git rev-parse --verify "refs/heads/$branch" -- >/dev/null 2>&1; then
71 git rev-parse --verify "refs/$topbases/$branch" -- >/dev/null 2>&1 || {
72 if [ -n "$logrefupdates" ]; then
73 mkdir -p "$git_dir/logs/refs/$topbases/$(dirname "$branch")" 2>/dev/null || :
74 { >>"$git_dir/logs/refs/$topbases/$branch" || :; } 2>/dev/null
76 git update-ref "refs/$topbases/$branch" "$rev"
78 info "Skipping branch $branch: Already exists"
79 continue
81 info "Adding branch $branch..."
82 if [ -n "$logrefupdates" ]; then
83 mkdir -p "$git_dir/logs/refs/$topbases/$(dirname "$branch")" 2>/dev/null || :
84 { >>"$git_dir/logs/refs/$topbases/$branch" || :; } 2>/dev/null
86 git update-ref "refs/$topbases/$branch" "$rev"
87 git update-ref "refs/heads/$branch" "$(git rev-parse --verify "$name/$branch" --)"
88 done
90 git config "topgit.remote" "$name"
91 info "The remote '$name' is now the default source of topic branches."
93 # vim:noet