forked from msysgit/msysgit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-to-github.sh
executable file
·167 lines (148 loc) · 3.46 KB
/
upload-to-github.sh
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh
# Written with a lot of assistance by Scott Chacon
USAGE="$0"' <options>... <filepath>
Options:
--description <download-description> (required)
--user <github-account> (required)
--repository <github-repository> (required)
'
description=
user=
repository=
filepath=
filesize=
basename=
dryrun=
while test $# -gt 0
do
case "$1" in
--description|--user|--repository)
test $# -gt 1 || {
echo "Option '$1' needs a value" >&2
exit 1
}
eval ${1#--}="\"$2\""
shift
;;
--description=*|--user=*|--repository=*)
pair="${1#--}"
eval ${pair%%=*}="${pair#*=}"
;;
--dry-run)
dryrun=YouGotThatRight
;;
--help)
echo "$USAGE"
exit 1
;;
*)
break;
esac
shift
done
if test $# -ne 1 || ! test -f "$1"
then
echo "$USAGE"
exit 1
fi
filepath="$1"
filesize="$(stat -c %s "$filepath")"
basename="$(basename "$filepath")"
# extract 'for version <version>' from basename
for_version=${basename%-preview*}
for_version=${for_version##*Git-}
for_version=${for_version##*install-}
for_version="for official Git for Windows $for_version"
case "$basename" in
Git-*)
description="${description:-Full installer $for_version}"
repository=${repository:-msysgit/git}
;;
PortableGit-*)
description="${description:-Portable application $for_version}"
repository=${repository:-msysgit/git}
;;
msysGit-netinstall-*)
description="${description:-Net installer if you want to hack on Git}"
repository=${repository:-msysgit/git}
;;
msysGit-fullinstall-*)
description="${description:-Full installer (self-contained) if you want to hack on Git}"
repository=${repository:-msysgit/git}
;;
esac
test -z "$user" &&
user="$(grep -A2 -i '^machine *api.github.com' < "$HOME/.netrc" 2> /dev/null |
sed -n 's|login *||pi')"
if test -z "$description" || test -z "$user" || test -z "$repository"
then
echo "$USAGE"
exit 1
fi
get_password () { # args: user host
# try $HOME/.netrc; ignore <user> parameter first
password="$(grep -A2 -i "^machine *$2" < $HOME/.netrc 2> /dev/null |
sed -n 's|^password *||p')"
test -z "$password" &&
password="$(git gui--askpass "Password for $1@$2")"
echo "$password"
}
json_get () { # args: key json
echo "$2" |
sed -n -s "s|^ *\"$1\" *: *\"\(.*\)\",\?$|\1|p"
}
json_wrap () { # args: key1, value1, [key2, value2], ...
printf '{'
sep=
while test $# -gt 1
do
printf '%s"%s":"%s"' "$sep" "$1" "$2"
sep=,
shift
shift
done
printf '}'
}
windowsfilepath="$(cd "$(dirname "$filepath")" && pwd -W)\\$basename"
test -n "$dryrun" && {
cat << EOF
basename: $basename
size: $filesize
description: $description
user: $user
repository: $repository
EOF
exit 0
}
password="$(get_password "$user" "api.github.com")"
test -n "$password" || exit
# get ticket
json="$(curl -XPOST \
-d "$(json_wrap \
name "$basename" \
size "$filesize" \
description "$description")" \
-u "$user:$password" \
https://api.github.com/repos/$repository/downloads)"
# upload for real, using S3
result="$(curl \
-F key="$(json_get path "$json")" \
-F acl="$(json_get acl "$json")" \
-F success_action_status=201 \
-F Filename="$(json_get name "$json")" \
-F AWSAccessKeyId="$(json_get accesskeyid "$json")" \
-F Policy="$(json_get policy "$json")" \
-F Signature="$(json_get signature "$json")" \
-F Content-Type="$(json_get mime_type "$json")" \
-F file=@"$windowsfilepath" \
"$(json_get s3_url "$json")")"
echo "$result"
# Verify that the upload was successful
case "$result" in
*"<Location>"*"</Location>"*)
echo "Success!"
;;
*)
exit 1
;;
esac