diff options
| -rwxr-xr-x | gist | 19 |
1 files changed, 13 insertions, 6 deletions
| @@ -267,7 +267,7 @@ _check_repo_status() { | |||
| 267 | else | 267 | else |
| 268 | cd "$1" || exit 1 | 268 | cd "$1" || exit 1 |
| 269 | # git status is not clean or working on non-master branch | 269 | # git status is not clean or working on non-master branch |
| 270 | if [[ -n $(git status --short) || $(git branch) =~ '\* (main|master)' ]] &>/dev/null; then | 270 | if [[ -n $(git status --short) || $(git branch) =~ "\* $(_remote_head)" ]] &>/dev/null; then |
| 271 | echo "\e[36m[working]\e[0m" | 271 | echo "\e[36m[working]\e[0m" |
| 272 | else | 272 | else |
| 273 | # files contents are not the same with the last time called GIST API, so warn user to call 'gist fetch' | 273 | # files contents are not the same with the last time called GIST API, so warn user to call 'gist fetch' |
| @@ -470,20 +470,27 @@ _query_user() { | |||
| 470 | done || { echo "Failed to query $1's gists"; exit 1; } | 470 | done || { echo "Failed to query $1's gists"; exit 1; } |
| 471 | } | 471 | } |
| 472 | 472 | ||
| 473 | _remote_head() { | ||
| 474 | [ -d .git/ ] || return 1 | ||
| 475 | local origin_head_content="$(cat .git/refs/remotes/origin/HEAD)" | ||
| 476 | echo ${origin_head_content##*/} | ||
| 477 | } | ||
| 478 | |||
| 473 | # Return the unique code for current commit, to compare repo status and the result of 'gist fetch' | 479 | # Return the unique code for current commit, to compare repo status and the result of 'gist fetch' |
| 474 | # Because there is no way to get commit SHA with 'gist fetch' | 480 | # Because there is no way to get commit SHA with 'gist fetch' |
| 475 | _blob_code() { | 481 | _blob_code() { |
| 476 | cd "$1" && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' | 482 | cd "$1" || return 1 |
| 483 | cd "$1" && git ls-tree $(_remote_head) | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' | ||
| 477 | } | 484 | } |
| 478 | 485 | ||
| 479 | _pull_if_needed() { | 486 | _pull_if_needed() { |
| 480 | sed -ne "/$1 / p" "$INDEX" \ | 487 | sed -ne "/$1 / p" "$INDEX" \ |
| 481 | | while read -r "${INDEX_FORMAT[@]}"; do | 488 | | while read -r "${INDEX_FORMAT[@]}"; do |
| 482 | local repo; repo=$folder/$1 | 489 | local repo=$folder/$1 |
| 483 | local blob_code_local; blob_code_local=$(_blob_code "$repo") | 490 | local blob_code_local=$(_blob_code "$repo") |
| 484 | cd "$repo" \ | 491 | cd "$repo" \ |
| 485 | && [[ $blob_code_local != "$blob_code" ]] \ | 492 | && [[ $blob_code_local != "$blob_code" ]] \ |
| 486 | && [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] \ | 493 | && [[ $(git rev-parse origin/HEAD) == $(git rev-parse $(remote_head)) ]] \ |
| 487 | && git pull & | 494 | && git pull & |
| 488 | done | 495 | done |
| 489 | } | 496 | } |
| @@ -653,7 +660,7 @@ _push_to_remote() { | |||
| 653 | git add . && git commit -m 'update' | 660 | git add . && git commit -m 'update' |
| 654 | fi | 661 | fi |
| 655 | if [[ -n $(git cherry) ]]; then | 662 | if [[ -n $(git cherry) ]]; then |
| 656 | git push origin master && \ | 663 | git push && \ |
| 657 | http_method GET "$GITHUB_API/gists/${GIST_ID}" | _update_gist $index | 664 | http_method GET "$GITHUB_API/gists/${GIST_ID}" | _update_gist $index |
| 658 | fi | 665 | fi |
| 659 | } | 666 | } |