diff options
author | typebrook <typebrook@gmail.com> | 2020-05-11 15:42:00 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-05-11 21:07:03 +0800 |
commit | bce48aa18a848c6bde53ea9635146e239a1307c3 (patch) | |
tree | b326bb97281bd68dc0352dd9cc943d996fc4f3dc | |
parent | eae4feaee4b88efaa6f273945f741f994023ef35 (diff) |
Refactor variable 'mark'
- make it global and export it to sub-shell
- refator _sync_repos(), now only do 'git pull' in repos that:
1. Already cloned before
2. Fit the mark. That is, either user's gist or starred gists
-rwxr-xr-x | gist | 76 |
1 files changed, 44 insertions, 32 deletions
@@ -63,6 +63,7 @@ CONFIG=~/.config/gist.conf; mkdir -p ~/.config | |||
63 | INDEX_FORMAT=('index' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') | 63 | INDEX_FORMAT=('index' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') |
64 | TAG_CHAR='-_[:alnum:]' | 64 | TAG_CHAR='-_[:alnum:]' |
65 | [[ ! -t 0 ]] && INPUT=$(cat) | 65 | [[ ! -t 0 ]] && INPUT=$(cat) |
66 | export mark=[^s] # By defaut, only process user's gists, not starred gist | ||
66 | 67 | ||
67 | # Default configuration | 68 | # Default configuration |
68 | [[ ! -t 1 && -z $hint ]] && hint=false | 69 | [[ ! -t 1 && -z $hint ]] && hint=false |
@@ -321,14 +322,13 @@ _index_pattern() { | |||
321 | # Display the list of gist, show username for starred gist | 322 | # Display the list of gist, show username for starred gist |
322 | # If hint=false, do not print hint to tty. If mark=<pattern>, filter index with regex | 323 | # If hint=false, do not print hint to tty. If mark=<pattern>, filter index with regex |
323 | # If display=tag, print tags instead or url | 324 | # If display=tag, print tags instead or url |
324 | # TODO color private/starred mark | ||
325 | _show_list() { | 325 | _show_list() { |
326 | if [[ ! -s $INDEX ]]; then | 326 | if [[ ! -s $INDEX ]]; then |
327 | echo 'Index file is empty, please run commands "gist fetch" or "gist create"' | 327 | echo 'Index file is empty, please run commands "gist fetch" or "gist create"' |
328 | return 0 | 328 | return 0 |
329 | fi | 329 | fi |
330 | 330 | ||
331 | sed -Ee "/^${mark:-[^s]}/ !d; /^$(_index_pattern) / !d" $INDEX \ | 331 | sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ |
332 | | while read -r "${INDEX_FORMAT[@]}"; do | 332 | | while read -r "${INDEX_FORMAT[@]}"; do |
333 | local message=$url | 333 | local message=$url |
334 | if [[ $display == 'tag' ]]; then | 334 | if [[ $display == 'tag' ]]; then |
@@ -411,8 +411,8 @@ _parse_response() { | |||
411 | _parse_gists \ | 411 | _parse_gists \ |
412 | | tac | nl -s' ' \ | 412 | | tac | nl -s' ' \ |
413 | | while read -r "${INDEX_FORMAT[@]:0:2}" public file_url_array "${INDEX_FORMAT[@]:4:7}"; do | 413 | | while read -r "${INDEX_FORMAT[@]:0:2}" public file_url_array "${INDEX_FORMAT[@]:4:7}"; do |
414 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p | 414 | local private_prefix=''; [[ $public == 'False' ]] && private_prefix=p |
415 | [[ -n $1 ]] && local index=${1}; index=${prefix}${index} | 415 | [[ -n $1 ]] && local index=${1}; index=${private_prefix}${prefix}${index} |
416 | 416 | ||
417 | local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) | 417 | local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
418 | file_array=${file_array//@None/@Text} | 418 | file_array=${file_array//@None/@Text} |
@@ -432,19 +432,19 @@ _fetch_gists() { | |||
432 | echo "fetching $user's gists from $GITHUB_API..." | 432 | echo "fetching $user's gists from $GITHUB_API..." |
433 | echo | 433 | echo |
434 | local route="users/$user/gists" | 434 | local route="users/$user/gists" |
435 | local mark='' | 435 | local prifix='' |
436 | if [[ $1 =~ ^(star|s)$ ]];then | 436 | if [[ $mark == s ]]; then |
437 | route='gists/starred' | 437 | route='gists/starred' |
438 | local mark=s | 438 | prefix=s |
439 | fi | 439 | fi |
440 | 440 | ||
441 | result=$(http_method GET $GITHUB_API/$route?per_page=100 | mark=$mark _parse_response) | 441 | result=$(http_method GET $GITHUB_API/$route?per_page=100 | prefix=$prefix _parse_response) |
442 | [[ -z $result ]] && echo 'Not a single valid gist' && return 0 | 442 | [[ -z $result ]] && echo 'Not a single valid gist' && return 0 |
443 | 443 | ||
444 | sed -i'' -Ee "/^${mark:-[^s]}/ d" $INDEX && echo "$result" >> $INDEX | 444 | sed -i'' -Ee "/^$mark/ d" $INDEX && echo "$result" >> $INDEX |
445 | mark=$mark hint=$hint _show_list | 445 | hint=$hint _show_list |
446 | 446 | ||
447 | [[ $auto_sync == 'true' ]] && (_sync_repos "$1" &> /dev/null &) | 447 | [[ $auto_sync == 'true' ]] && (_sync_repos &> /dev/null &) |
448 | true | 448 | true |
449 | } | 449 | } |
450 | 450 | ||
@@ -467,24 +467,34 @@ _blob_code() { | |||
467 | cd "$1" && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' | 467 | cd "$1" && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' |
468 | } | 468 | } |
469 | 469 | ||
470 | # Update local git repos | 470 | _pull_if_needed() { |
471 | _sync_repos() { | 471 | sed -ne "/$1 / p" "$INDEX" \ |
472 | # clone repos which are not in the local | 472 | | while read -r "${INDEX_FORMAT[@]}"; do |
473 | comm -13 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ | 473 | local repo; repo=$folder/$1 |
474 | <(cut -d' ' -f2 < "$INDEX" | sed -e 's#.*/##' | sort) \ | ||
475 | | xargs -I{} --max-procs 8 git clone "$(_repo_url {})" $folder/{} | ||
476 | |||
477 | # pull if remote repo has different blob objects | ||
478 | cut -d' ' -f2,3 < "$INDEX" \ | ||
479 | | while read -r url blob_code_remote; do | ||
480 | local repo; repo=$folder/${url##*/} | ||
481 | local blob_code_local; blob_code_local=$(_blob_code "$repo") | 474 | local blob_code_local; blob_code_local=$(_blob_code "$repo") |
482 | cd "$repo" \ | 475 | cd "$repo" \ |
483 | && [[ $blob_code_local != "$blob_code_remote" ]] \ | 476 | && [[ $blob_code_local != "$blob_code" ]] \ |
484 | && [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] \ | 477 | && [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] \ |
485 | && git pull | 478 | && git pull |
486 | done | 479 | done |
487 | echo Everything is fine! | 480 | } |
481 | export -f _pull_if_needed | ||
482 | |||
483 | # Update local git repos | ||
484 | _sync_repos() { | ||
485 | comm -1 <(ls -A "$folder" | sort) \ | ||
486 | <(cut -d' ' -f1-2 < "$INDEX" | sed -ne "/^$mark/ s#.*/##p" | sort) \ | ||
487 | | { | ||
488 | result=$(cat) | ||
489 | |||
490 | # clone repos which are not in the local | ||
491 | sed -n '/^\t/ !p' <<<$result \ | ||
492 | | xargs -I{} --max-procs 8 git clone "$(_repo_url {})" $folder/{} & | ||
493 | |||
494 | # if repo is cloned, do 'git pull' if remote repo has different blob objects | ||
495 | sed -n '/^\t/ p' <<<$result \ | ||
496 | | xargs -I{} --max-procs 8 bash -c '_pull_if_needed {}' | ||
497 | } | ||
488 | } | 498 | } |
489 | 499 | ||
490 | # Get the url where to clone repo, take user and repo name as parameters | 500 | # Get the url where to clone repo, take user and repo name as parameters |
@@ -817,11 +827,12 @@ _gists_with_languages() { | |||
817 | } | 827 | } |
818 | 828 | ||
819 | _gists_with_range() { | 829 | _gists_with_range() { |
820 | [[ ! $* =~ ^s*[0-9]*-s*[0-9]*$ ]] && echo 'Invalid range' && exit 1 | 830 | [[ ! $* =~ ^s?[0-9]*-s?[0-9]*$ ]] && echo 'Invalid range' && exit 1 |
821 | local mark='' && [[ $* =~ s ]] && mark=s | 831 | local prefix=''; [[ $* =~ s ]] && prefix=s |
822 | 832 | ||
823 | local range=$(sed -Ee "s/s//g; s/^-/1-/; s/-$/-$(wc -l <$INDEX)/; s/-/\n/" <<< "$*") | 833 | local maximum=$(sed -Ene "/^${prefix:-[^s]}/ p" $INDEX | wc -l) |
824 | INPUT=$(seq $range | sed -e "s/^/p*$mark/") | 834 | local range=$(sed -Ee "s/s//g; s/^-/1-/; s/-$/-$maximum/; s/-/ /" <<< "$*") |
835 | INPUT=$(seq $range | sed -e "s/^/p?$prefix/") | ||
825 | hint=false _show_list | 836 | hint=false _show_list |
826 | } | 837 | } |
827 | 838 | ||
@@ -837,11 +848,12 @@ case "$1" in | |||
837 | "") | 848 | "") |
838 | _show_list ;; | 849 | _show_list ;; |
839 | star | s) | 850 | star | s) |
840 | mark=s _show_list ;; | 851 | mark=s; _show_list ;; |
841 | all | a) | 852 | all | a) |
842 | mark=. _show_list ;; | 853 | mark=.; _show_list ;; |
843 | fetch | f) | 854 | fetch | f) |
844 | _fetch_gists "$2" ;; | 855 | [[ $2 =~ ^[s|star]$ ]] && mark=s || mark=[^s] |
856 | _fetch_gists ;; | ||
845 | new | n) | 857 | new | n) |
846 | shift | 858 | shift |
847 | _create_gist "$@" ;; | 859 | _create_gist "$@" ;; |
@@ -885,7 +897,7 @@ case "$1" in | |||
885 | shift | 897 | shift |
886 | _gists_with_languages "$@" ;; | 898 | _gists_with_languages "$@" ;; |
887 | *-*) | 899 | *-*) |
888 | _gists_with_range "$@" ;; | 900 | mark=.; _gists_with_range "$@" ;; |
889 | last | L) | 901 | last | L) |
890 | _access_last_index "$@" ;; | 902 | _access_last_index "$@" ;; |
891 | version) | 903 | version) |