summaryrefslogtreecommitdiff
path: root/dot/local/bin/git-push-clone
blob: 703c9fde326c70ddf880018b028aec6e32961c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh
set -e

usage()
{
    cat <<EOF
Usage:
    $0                         [git-push options...] <destination> [refspec]
    $0 <--all|--mirror|--tags> [git-push options...] <destination>
EOF
}

die() { printf "Error: %s\n" "$*" >&2; exit 1; }

loudly() { (set -x; "$@"); }

quietly() { "$@" >/dev/null 2>&1; }

bad_usage()
{
    usage >&2
    exit 1
}

check_existence()
{
    (set +e;
     quietly rsync "$rsync_dest"
     case $? in
         23) return 0;;
         0) [ "$NO_ACT" ] || die "Destination already exists: '$rsync_dest'";;
         *) die "rsync returned $? when checking for destination existence.";;
     esac) || exit
}

git_init_push_checkout()
{
    TEMP_DIR=$(mktemp -d)
    trap 'rm -rf "$TEMP_DIR"' EXIT
    git init --bare "$TEMP_DIR"/.git
    loudly git push "$TEMP_DIR" "$@"

    if [ "$CHECKOUT_BRANCH" -a ! "$BARE" ]; then
        (cd "$TEMP_DIR";
        git config core.bare false
        git checkout "$CHECKOUT_BRANCH")
    fi
}

go()
{
    check_existence
    git_init_push_checkout "$@"
}

# What we want to do is push a branch, and then checkout the branch we just
# pushed. Determining the branch to checkout requires parsing the branch out of
# the git push args in $@

check_source_ref()
{
    local src="$1"
    git rev-parse --verify --quiet "$src^{commit}" >/dev/null
}

pop=$(cat <<'EOF'
i=$#;
while [ $i -gt 0 ]; do
    lastarg=$1;
    shift;
    i=$((i - 1))
    if [ $i -eq 0 ]; then
        break;
    else
        set -- "$@" "$lastarg";
    fi
done
EOF
)

parse_target()
{
    case "$1" in
        ssh://*) rsync_dest=${1#ssh://} ;;
        *:*) rsync_dest=$1 ;;
        *) die "Unrecognized git URL: '$1'" ;;
    esac
    target=$1

    case "$target" in
        *[^/].git) BARE=y ;;
        *) BARE= ;;
    esac
}

parse_refspec()
{
    local refspec="$1"
    case "$refspec" in
        *:*:*) die "expected <local-ref>:<remote-ref> but got too many colons" ;;
        ?*:?*)
            check_source_ref "${1%:*}" || die "invalid source ref: $src"
            CHECKOUT_BRANCH=${1#*:}
            ;;
        *) if check_source_ref "$1"; then CHECKOUT_BRANCH=$1; fi;;
    esac
}

current_branch() { git status -bs|sed -ne 's/^## //p'; }

REFSPEC_UNNEEDED=
arg_count=$#
for arg in "$@"; do
    case "$arg" in
        -h|--help|--usage) usage; exit;;
        --all|--mirror|--tags) REFSPEC_UNNEEDED=y;;
        --no-act) NO_ACT=y;;
        --) arg_count=$((arg_count - 1)); break;;
        -*) ;;
        *) break;;
    esac
    arg_count=$((arg_count - 1))
done

case $arg_count in
    1)
        eval "$pop"
        parse_target "$lastarg"
        b=$(current_branch)
        parse_refspec "$b:$b"
        if ! [ "$REFSPEC_UNNEEDED" ]; then
            set -- "$@" "$b"
        fi
        ;;
    2)
        eval "$pop"
        refspec=$lastarg
        parse_refspec "$refspec"
        eval "$pop"
        parse_target "$lastarg"
        set -- "$@" "$refspec"
       ;;
    *) bad_usage;;
esac

go "$@"

loudly rsync ${NO_ACT:+-n} -zrlp --info=stats1 "$TEMP_DIR"/"${BARE:+.git/}" "$rsync_dest"/