Installer: Add registry-based context menus also to directory backgrounds
[msysgit.git] / bin / upload-to-github.sh
bloba78d48ae6e90bc72d2d38d8ba1326b88b5bc20d3
1 #!/bin/sh
3 # Written with a lot of assistance by Scott Chacon
5 USAGE="$0"' <options>... <filepath>
6 Options:
7 --description <download-description> (required)
8 --user <github-account> (required)
9 --repository <github-repository> (required)
12 description=
13 user=
14 repository=
15 filepath=
16 filesize=
17 basename=
18 dryrun=
19 while test $# -gt 0
21 case "$1" in
22 --description|--user|--repository)
23 test $# -gt 1 || {
24 echo "Option '$1' needs a value" >&2
25 exit 1
27 eval ${1#--}="\"$2\""
28 shift
30 --description=*|--user=*|--repository=*)
31 pair="${1#--}"
32 eval ${pair%%=*}="${pair#*=}"
34 --dry-run)
35 dryrun=YouGotThatRight
37 --help)
38 echo "$USAGE"
39 exit 1
42 break;
43 esac
44 shift
45 done
47 if test $# -ne 1 || ! test -f "$1"
48 then
49 echo "$USAGE"
50 exit 1
53 filepath="$1"
54 filesize="$(stat -c %s "$filepath")"
55 basename="$(basename "$filepath")"
57 # extract 'for version <version>' from basename
58 for_version=${basename%-preview*}
59 for_version=${for_version##*Git-}
60 for_version=${for_version##*install-}
61 for_version="for official Git for Windows $for_version"
63 case "$basename" in
64 Git-*)
65 description="${description:-Full installer $for_version}"
66 repository=${repository:-msysgit/git}
68 PortableGit-*)
69 description="${description:-Portable application $for_version}"
70 repository=${repository:-msysgit/git}
72 msysGit-netinstall-*)
73 description="${description:-Net installer if you want to hack on Git}"
74 repository=${repository:-msysgit/git}
76 msysGit-fullinstall-*)
77 description="${description:-Full installer (self-contained) if you want to hack on Git}"
78 repository=${repository:-msysgit/git}
80 esac
82 test -z "$user" &&
83 user="$(grep -A2 -i '^machine *api.github.com' < "$HOME/.netrc" 2> /dev/null |
84 sed -n 's|login *||pi')"
86 if test -z "$description" || test -z "$user" || test -z "$repository"
87 then
88 echo "$USAGE"
89 exit 1
92 get_password () { # args: user host
93 # try $HOME/.netrc; ignore <user> parameter first
94 password="$(grep -A2 -i "^machine *$2" < $HOME/.netrc 2> /dev/null |
95 sed -n 's|^password *||p')"
96 test -z "$password" &&
97 password="$(git gui--askpass "Password for $1@$2")"
98 echo "$password"
101 json_get () { # args: key json
102 echo "$2" |
103 sed -n -s "s|^ *\"$1\" *: *\"\(.*\)\",\?$|\1|p"
106 json_wrap () { # args: key1, value1, [key2, value2], ...
107 printf '{'
108 sep=
109 while test $# -gt 1
111 printf '%s"%s":"%s"' "$sep" "$1" "$2"
112 sep=,
113 shift
114 shift
115 done
116 printf '}'
119 windowsfilepath="$(cd "$(dirname "$filepath")" && pwd -W)\\$basename"
121 test -n "$dryrun" && {
122 cat << EOF
123 basename: $basename
124 size: $filesize
125 description: $description
126 user: $user
127 repository: $repository
129 exit 0
132 password="$(get_password "$user" "api.github.com")"
133 test -n "$password" || exit
135 # get ticket
136 json="$(curl -XPOST \
137 -d "$(json_wrap \
138 name "$basename" \
139 size "$filesize" \
140 description "$description")" \
141 -u "$user:$password" \
142 https://api.github.com/repos/$repository/downloads)"
144 # upload for real, using S3
145 result="$(curl \
146 -F key="$(json_get path "$json")" \
147 -F acl="$(json_get acl "$json")" \
148 -F success_action_status=201 \
149 -F Filename="$(json_get name "$json")" \
150 -F AWSAccessKeyId="$(json_get accesskeyid "$json")" \
151 -F Policy="$(json_get policy "$json")" \
152 -F Signature="$(json_get signature "$json")" \
153 -F Content-Type="$(json_get mime_type "$json")" \
154 -F file=@"$windowsfilepath" \
155 "$(json_get s3_url "$json")")"
157 echo "$result"
159 # Verify that the upload was successful
160 case "$result" in
161 *"<Location>"*"</Location>"*)
162 echo "Success!"
165 exit 1
167 esac