diff options
| -rwxr-xr-x | scripts/gist | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/scripts/gist b/scripts/gist index b901046..a40a6a1 100755 --- a/scripts/gist +++ b/scripts/gist | |||
| @@ -30,7 +30,6 @@ | |||
| 30 | # Since now a gist is a local cloned repo | 30 | # Since now a gist is a local cloned repo |
| 31 | # It is your business to do git commit and git push | 31 | # It is your business to do git commit and git push |
| 32 | 32 | ||
| 33 | # TODO push github.com (may need new token) | ||
| 34 | # TODO test on bats, mac and remote machine | 33 | # TODO test on bats, mac and remote machine |
| 35 | # TODO completion | 34 | # TODO completion |
| 36 | 35 | ||
| @@ -165,7 +164,7 @@ _show_list() { | |||
| 165 | [[ -z $1 ]] && local filter='/^ *s/ d; /^$/ d' | 164 | [[ -z $1 ]] && local filter='/^ *s/ d; /^$/ d' |
| 166 | [[ $1 == "s" ]] && local filter='/^ *[^ s]/ d; /^$/ d' | 165 | [[ $1 == "s" ]] && local filter='/^ *[^ s]/ d; /^$/ d' |
| 167 | 166 | ||
| 168 | while read index link blob_code file_num extra author description; do | 167 | while read index link files blob_code file_num extra author description; do |
| 169 | [[ $1 == "s" ]] && local name=$author | 168 | [[ $1 == "s" ]] && local name=$author |
| 170 | local repo=$folder/$(echo $link | sed 's#.*/##') | 169 | local repo=$folder/$(echo $link | sed 's#.*/##') |
| 171 | local occupy=0 | 170 | local occupy=0 |
| @@ -201,7 +200,8 @@ _handle_gists() { | |||
| 201 | raw = json.load(sys.stdin) | 200 | raw = json.load(sys.stdin) |
| 202 | for gist in raw: | 201 | for gist in raw: |
| 203 | print(gist["html_url"], end=" ") | 202 | print(gist["html_url"], end=" ") |
| 204 | print([file["raw_url"] for file in gist["files"].values()], end=" ") | 203 | print([file for file in gist["files"].keys()], end=" ") |
| 204 | print([details["raw_url"] for details in gist["files"].values()], end=" ") | ||
| 205 | print(gist["public"], end=" ") | 205 | print(gist["public"], end=" ") |
| 206 | print(len(gist["files"]), end=" ") | 206 | print(len(gist["files"]), end=" ") |
| 207 | print(gist["comments"], end=" ") | 207 | print(gist["comments"], end=" ") |
| @@ -212,14 +212,15 @@ for gist in raw: | |||
| 212 | 212 | ||
| 213 | # TODO check if a user create a very first gist | 213 | # TODO check if a user create a very first gist |
| 214 | # parse response from gists require | 214 | # parse response from gists require |
| 215 | _parse_response() { | 215 | _parse_gists() { |
| 216 | AccessJsonElement "$(_handle_gists)" \ | 216 | AccessJsonElement "$(_handle_gists)" \ |
| 217 | | tac | sed 's/, /,/g' | nl -s' ' \ | 217 | | tac | sed 's/, /,/g' | nl -s' ' \ |
| 218 | | while read index link file_url_array public file_num comment_num author description; do | 218 | | while read index link file_array file_url_array public file_num comment_num author description; do |
| 219 | local files=$(echo $file_array | sed -E "s/.*'(.+)'.*/\1/g" | tr ',' '-') | ||
| 219 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 220 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
| 220 | [[ $public == 'False' ]] && local mark=p | 221 | [[ $public == 'False' ]] && local mark=p |
| 221 | [[ -n $1 ]] && local index=$1 | 222 | [[ -n $1 ]] && local index=$1 |
| 222 | echo $mark$index $link $blob_code $file_num $comment_num $author $description | tr -d '"' | 223 | echo $mark$index $link $files $blob_code $file_num $comment_num $author $description | tr -d '"' |
| 223 | done | 224 | done |
| 224 | } | 225 | } |
| 225 | 226 | ||
| @@ -237,7 +238,7 @@ _update() { | |||
| 237 | filter='/^[s]/ d; /^$/ d' | 238 | filter='/^[s]/ d; /^$/ d' |
| 238 | fi | 239 | fi |
| 239 | 240 | ||
| 240 | result=$(http_method GET $GITHUB_API/$route | _parse_response) | 241 | result=$(http_method GET $GITHUB_API/$route | _parse_gists) |
| 241 | [[ -z $result ]] && echo Failed to update gists && return 1 | 242 | [[ -z $result ]] && echo Failed to update gists && return 1 |
| 242 | 243 | ||
| 243 | sed -i "$filter" $INDEX && echo "$result" >> $INDEX | 244 | sed -i "$filter" $INDEX && echo "$result" >> $INDEX |
| @@ -248,11 +249,11 @@ _update() { | |||
| 248 | 249 | ||
| 249 | _query_user() { | 250 | _query_user() { |
| 250 | local route="users/$1/gists" | 251 | local route="users/$1/gists" |
| 251 | result=$(http_method GET $GITHUB_API/$route | _parse_response) | 252 | result=$(http_method GET $GITHUB_API/$route | _parse_gists) |
| 252 | [[ -z $result ]] && echo Failed to update gists && return 1 | 253 | [[ -z $result ]] && echo Failed to update gists && return 1 |
| 253 | 254 | ||
| 254 | echo "$result" \ | 255 | echo "$result" \ |
| 255 | | while read index link blob_code file_num extra description; do | 256 | | while read index link files blob_code file_num extra description; do |
| 256 | echo $link $file_num $extra $(echo $description | cut -c -70 ) | 257 | echo $link $file_num $extra $(echo $description | cut -c -70 ) |
| 257 | done | 258 | done |
| 258 | } | 259 | } |
| @@ -265,7 +266,7 @@ _sync_repos() { | |||
| 265 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} | 266 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} |
| 266 | 267 | ||
| 267 | # pull if remote repo has different blob objects | 268 | # pull if remote repo has different blob objects |
| 268 | cat $INDEX | cut -d' ' -f2,3 \ | 269 | cat $INDEX | cut -d' ' -f2,4 \ |
| 269 | | while read url blob_code_remote; do | 270 | | while read url blob_code_remote; do |
| 270 | local repo=$folder/$(echo $url | sed 's#.*/##') | 271 | local repo=$folder/$(echo $url | sed 's#.*/##') |
| 271 | local blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-') | 272 | local blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-') |
| @@ -423,7 +424,7 @@ _create_gist() { | |||
| 423 | | AccessJsonElement "$(_gist_body)" > $http_data \ | 424 | | AccessJsonElement "$(_gist_body)" > $http_data \ |
| 424 | && http_method POST $GITHUB_API/gists \ | 425 | && http_method POST $GITHUB_API/gists \ |
| 425 | | sed '1 s/^/[/; $ s/$/]/' \ | 426 | | sed '1 s/^/[/; $ s/$/]/' \ |
| 426 | | _parse_response $(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) \ | 427 | | _parse_gists $(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) \ |
| 427 | | tee -a $INDEX \ | 428 | | tee -a $INDEX \ |
| 428 | | cut -d' ' -f2 | sed -E 's#.*/##' \ | 429 | | cut -d' ' -f2 | sed -E 's#.*/##' \ |
| 429 | | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) | 430 | | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) |