From a5e940f645e4f93cd2e408489f253eca2f0f1e8a Mon Sep 17 00:00:00 2001 From: typebrook Date: Fri, 7 Feb 2020 15:37:53 +0800 Subject: update --- gist | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/gist b/gist index 2ac7fd7..9420243 100755 --- a/gist +++ b/gist @@ -5,33 +5,36 @@ # https://gist.github.com/typebrook/b0d2e7e67aa50298fdf8111ae7466b56 # # gist -# Description: Host your gists as local cloned git repo +# Description: Manage your gists with git and Github API v3 # Usage: gist [command] [] # # [star | s] list your gists with format below, star for your starred gists: # [index_of_gist] [url] [file_num] [comment_num] [short description] # update, u [star | s] update the local list of your gists, star for your starred gists # [--no-action] show the path of local gist repo and do custom actions -# new, n [-d | --desc ] ... create a new gist with files -# new, n [-d | --desc ] [-f | --file ] create a new gist from STDIN +# new, n [-d | --desc ] [-p] ... create a new gist with files +# new, n [-d | --desc ] [-p] [-f | --file ] create a new gist from STDIN # detail, d show the detail of a gist -# edit, e edit a gist description +# edit, e edit a gist's description # delete, D ... delete a gist # clean, C clean removed gists in local # config, c [token | user | folder | auto_sync | EDITOR | action [value] ] do configuration # user, U get gists from a given Github user # grep, g grep gists by a given pattern +# github, G Import this gist as a new Github repo # push, p push changes by git # help, h show this help message # # Example: # gist (Show your gists) +# gist update (update the list of gists from github.com) # gist 3 (show the repo path of your 3rd gist, and do custom actions) +# gist 3 --no-action (show the repo path of your 3rd gist, and do not perform actions) +# gist new foo --desc bar (create a new gist with file and description) # # Since now a gist is a local cloned repo # It is your business to do git commit and git push -# TODO import to github repo (may need a new token) # TODO test on bats, mac and remote machine # TODO completion @@ -49,15 +52,17 @@ _configure() { [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0 local target="" - if [[ $1 == 'user' ]]; then - [[ -z $2 ]] && echo "Must specify username" >&2 && return 1 - elif [[ $1 == 'token' ]]; then - [[ ${#2} -ne 40 ]] && echo 'Invalid token format, it is not 40 chars' >&2 \ - && return 1 - elif [[ $1 == 'auto_sync' ]]; then - [[ ! $2 =~ ^(true|false)$ ]] && return 1 + if [[ $1 =~ ^(user|token|folder|auto_sync|EDITOR|action)$ ]]; then + if [[ $1 == 'user' ]]; then + [[ -z $2 ]] && echo "Must specify username" >&2 && return 1 + elif [[ $1 == 'token' ]]; then + [[ ${#2} -ne 40 ]] && echo 'Invalid token format, it is not 40 chars' >&2 \ + && return 1 + elif [[ $1 == 'auto_sync' ]]; then + [[ ! $2 =~ ^(true|false)$ ]] && return 1 + fi + target=$1=$2 fi - target=$1=$2 umask 0077 && touch $CONFIG sed -i "/^$1=/ d" $CONFIG && [[ "$target" =~ [^=]$ ]] && echo $target >> $CONFIG @@ -120,8 +125,6 @@ _apply_config() { INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX } -_apply_config "$@" || exit 1 - # This function determines which http get tool the system has installed and returns an error if there isnt one getConfiguredClient() { if command -v curl &>/dev/null; then @@ -176,6 +179,8 @@ _show_list() { [[ ! -d $repo ]] && extra="\e[32m[Not cloned yet]\e[0m" && occupy=16 # if there are some changes in git index or working directory, show blue message "working" [[ -n $(cd $repo && git status --short) ]] 2>/dev/null && extra="\e[36m[working]\e[0m" && occupy=9 + # if there files are different, show red message "outdated" + [[ $(_blob_code $repo) != $blob_code ]] 2>/dev/null && extra="\e[31m[outdated]\e[0m" && occupy=10 # if there is a commit not yet push, show red message "ahead" [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 @@ -187,7 +192,15 @@ _show_list() { # TODO support filenames, file contents _grep_content() { - _show_list other | grep -i $1 + _show_list | grep -i $1 +} + +# TODO support filenames, file contents +_import_to_github() { + _gist_id $1 + echo put the folowing URL into webpage: + echo -n git@github.com:$GIST_ID.git + python -mwebbrowser https://github.com/new/import } _push_to_remote() { @@ -230,6 +243,7 @@ _parse_response() { done } +# TODO pagnation for more than 30 gists # TODO add files and date of a gist # get latest list of gists from Github API _update() { @@ -264,6 +278,11 @@ _query_user() { done } +_blob_code() { + cd $1 \ + && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' +} + # update local git repos _sync_repos() { # clone repos which are not in the local @@ -275,7 +294,7 @@ _sync_repos() { cat $INDEX | cut -d' ' -f2,3 \ | while read url blob_code_remote; do local repo=$folder/$(echo $url | sed 's#.*/##') - local blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-') + local blob_code_local=$(_blob_code $repo) cd $repo \ && [[ $blob_code_local != $blob_code_remote ]] \ && [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] \ @@ -463,6 +482,7 @@ usage() { sed -E -n ' /^$/ q; 7,$ s/^# //p' $0 } +_apply_config "$@" || exit 1 getConfiguredClient if [[ $init ]]; then _update; exit 0; fi case "$1" in @@ -496,6 +516,9 @@ case "$1" in grep | g) shift _grep_content "$@" ;; + github | G) + shift + _import_to_github "$1" ;; push | p) shift _push_to_remote "$1" ;; -- cgit v1.2.3-70-g09d2