chiark / gitweb /
49fd370ec86fc74393e949aca37ec46d2a7eeb7d
[chiark-utils.git] / scripts / git-branchmove
1 #!/bin/bash
2 #
3 # Moves a branch to or from the current git tree to or from
4 # another git tree
5 #
6 # usage:   git-branchmove get|put REMOTE BRANCH
7
8 set -e
9 set -o posix
10
11 fail () { echo >&2 "git-branchmove: $*"; exit 16; }
12 badusage () { fail "bad usage: $*"; }
13
14 case "$#.$1" in
15 3.get)
16         op=get
17         remote="$2"
18         branch="$3"
19         ;;
20 3.put)
21         op=put
22         branch="$3"
23         ;;
24 *)
25         badusage "wrong number of arguments or wrong operation"
26         ;;
27 esac
28
29 # Plan of attack:
30 #  determine execute-sh runes for src and dst trees
31 #  check that source branch is not checked out
32 #  list affected branches on source
33 #  list affected branches on destination and moan if any nonequal overlap
34 #  transfer src->dst refs/heads/BRANCH:refs/heads/BRANCH
35 #  transfer and merge reflog(s) xxx todo
36 #  delete src refs
37
38 case "$remote" in
39 *:*)    remoteurl="$remote" ;;
40 *)      remoteurl="$(
41                 git config remote."$remote".pushurl ||
42                 git config remote."$remote".url ||
43                 fail "no pushurl or url defined for remote $remote"
44                 )"
45 esac
46
47 remote_spec="$(perl -e '
48     $_ = $ARGV[0];
49     if (m#^ssh://([^:/]+)(?:\:(\w+))?#) {
50         print "$'\''|ssh ";
51         print " -p $3" if $2;
52         print "$1\n";
53     } elsif (m#^([-+_.0-9a-zA-Z\@]+):(?!//)#) {
54         print "$'\''|ssh $1\n";
55     } else {
56         die "git-branchmove: unsupported remote url \`$_'\''\n";
57     }
58 ' "$remoteurl")"
59
60 remote_path="${remote_spec%%|*}"
61 remote_rune="${remote_spec#*|}"
62
63 case $op in
64 get)
65         src_rune="$remote_rune"
66         src_path="$remote_path"
67         dst_rune="sh -c"
68         dst_path=.
69         ;;
70 put)
71         dst_rune="$remote_rune"
72         dst_path="$remote_path"
73         src_rune="sh -c"
74         src_path=.
75         ;;
76 esac
77