From 8dc3ab646f1afa93fdcf7f8e61d0385ac0c8c8d3 Mon Sep 17 00:00:00 2001 From: typebrook Date: Sun, 12 Jan 2020 22:56:11 +0800 Subject: update --- gist | 81 ++++++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/gist b/gist index 834d6cf..80996b1 100755 --- a/gist +++ b/gist @@ -17,6 +17,7 @@ # gist # # * clone gist repos which are not in local +# * pull master branch if a local repo is behind its remote # gist [sync | s] # # * Go to local gist repo @@ -50,24 +51,47 @@ mkdir -p $folder index=$folder/index # Validate settings. -[ "$TRACE" ] && set -x && echo foo +[ "$TRACE" ] && set -x + +# Show the list of gist, but not updated time +_show_list() { + cat $index | cut -d' ' -f1-2,4- +} # get the list of gists _update() { curl -s -H "$auth_header" $github_api/users/$user/gists |\ - jq '.[] | "\( .html_url ) \(.files | keys | length) \(.comments) \( .description )"' |\ - tr -d '"' | tac | nl |\ - while read line_num link file_num comment_num description; do - echo $line_num $link $file_num $comment_num $(echo $description | cut -c -70) - done | tee $index + tee jojo |\ + jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ + tac | nl |\ + while read line_num link blobs file_num comment_num description; do + blob_code=$(echo $blobs | jq -r '.[]' | sed -r 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') + echo $line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -80) | tr -d '"' + done > $index && \ + _show_list } -# clone repos which are not in the local -# TODO pull if repo is outdated _sync_repos() { + # clone repos which are not in the local comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ xargs -I{} git clone git@github.com:{}.git $folder/{} + + # pull if remote repo has different blob objects + cat $index | cut -d' ' -f2,3 |\ + while read url blob_code_remote; do + repo=$folder/$(echo $url | sed 's#.*/##') + blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-') + + cd $repo && \ + [[ $blob_code_local != $blob_code_remote ]] && \ + [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] && \ + git pull + done +} + +_gist_id() { + cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##' } _goto_gist() { @@ -76,17 +100,17 @@ _goto_gist() { echo Not a valid gist number: $1 echo Use the number in the first column instead: echo - cat $index + show_list return 0 fi - GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') + GIST_ID=$(_gist_id $1) echo This gist is at $folder/$GIST_ID cd $folder/$GIST_ID && tig --all 2> /dev/null } _delete_gist() { - GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') + GIST_ID=$(_gist_id $1) curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \ _update } @@ -99,33 +123,37 @@ _clean_repos() { } _show_detail() { - GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') + GIST_ID=$(_gist_id $1) curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ - jq '{site: .html_url, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' + jq '{site: .html_url, description: .description, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\ jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' } -# create a new gist with a file and description +# create a new gist with files # TODO support folder of files _create_gist() { - FILE=$(basename $1) - - jq --arg FILE "$FILE" --arg DESC "$2" '. as $content | { - public: true, - files: { - ($FILE): {content: $content} - }, + echo -n 'description: ' + read DESC + + echo $@ | tr " " "\n" |\ + while read file; do + FILE=$(basename $file) + jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file + done |\ + jq -s --arg DESC "$DESC" '{ + public: true, + files: add, description: ($DESC) - }' -R -s $1 |\ + }' |\ curl -s -H "$auth_header" --data @- $github_api/gists > /dev/null && \ - _update + _update && _sync_repos } # update description of a gist _edit_gist() { - GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') + GIST_ID=$(_gist_id $1) jq -n --arg DESC "$2" '{ description: ($DESC) }' |\ curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \ @@ -134,10 +162,11 @@ _edit_gist() { case "$1" in "") - cat $index + _show_list ;; create | c) - _create_gist "$2" "$3" + shift; + _create_gist $@ ;; edit | e) _edit_gist "$2" "$3" -- cgit v1.2.3-70-g09d2