summaryrefslogtreecommitdiffhomepage
path: root/scripts/gist
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/gist')
-rwxr-xr-xscripts/gist32
1 files changed, 16 insertions, 16 deletions
diff --git a/scripts/gist b/scripts/gist
index bd48566..010f6c9 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -131,8 +131,8 @@ auth_header="Authorization: token $token"
131 131
132[[ -z "$folder" ]] && folder=~/gist 132[[ -z "$folder" ]] && folder=~/gist
133mkdir -p $folder 133mkdir -p $folder
134index=$folder/index 134INDEX=$folder/index
135starred=$folder/starred 135STARRED=$folder/starred
136 136
137# Show the list of gist, but not updated time 137# Show the list of gist, but not updated time
138# TODO a way to show files 138# TODO a way to show files
@@ -140,7 +140,7 @@ starred=$folder/starred
140_show_list() { 140_show_list() {
141 if [[ ! -e "$1" ]]; then 141 if [[ ! -e "$1" ]]; then
142 echo 'No local file found for last update, please run command:' 142 echo 'No local file found for last update, please run command:'
143 echo " gist update $([[ $1 == $starred ]] && echo 'star')" 143 echo " gist update $([[ $1 == $STARRED ]] && echo 'star')"
144 return 0 144 return 0
145 fi 145 fi
146 cat $1 \ 146 cat $1 \
@@ -163,10 +163,10 @@ _show_list() {
163_update() { 163_update() {
164 echo "fetching $user's gists from $GITHUB_API..." 164 echo "fetching $user's gists from $GITHUB_API..."
165 echo 165 echo
166 local list_file=$index 166 local list_file=$INDEX
167 local route="users/$user/gists" 167 local route="users/$user/gists"
168 local mark="" 168 local mark=""
169 [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s" 169 [[ "$1" =~ ^(star|s)$ ]] && list_file=$STARRED && route="gists/starred" && mark="s"
170 170
171 curl -s $GITHUB_API/$route \ 171 curl -s $GITHUB_API/$route \
172 | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file \ 172 | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file \
@@ -205,8 +205,8 @@ _parse_response() {
205} 205}
206 206
207_sync_repos() { 207_sync_repos() {
208 local list_file=$index 208 local list_file=$INDEX
209 [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" 209 [[ "$1" =~ ^(star|s)$ ]] && list_file=$STARRED && route="gists/starred"
210 210
211 # clone repos which are not in the local 211 # clone repos which are not in the local
212 comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ 212 comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \
@@ -214,7 +214,7 @@ _sync_repos() {
214 | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} 214 | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{}
215 215
216 # pull if remote repo has different blob objects 216 # pull if remote repo has different blob objects
217 cat $index | cut -d' ' -f2,3 \ 217 cat $INDEX | cut -d' ' -f2,3 \
218 | while read url blob_code_remote; do 218 | while read url blob_code_remote; do
219 local repo=$folder/$(echo $url | sed 's#.*/##') 219 local repo=$folder/$(echo $url | sed 's#.*/##')
220 local blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-') 220 local blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-')
@@ -228,12 +228,12 @@ _sync_repos() {
228 228
229# get gist id from index files 229# get gist id from index files
230_gist_id() { 230_gist_id() {
231 GIST_ID=$( (grep -hs '' $index $starred || true) | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') 231 GIST_ID=$( (grep -hs '' $INDEX $STARRED || true) | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##')
232 if [[ -z "$GIST_ID" ]]; then 232 if [[ -z "$GIST_ID" ]]; then
233 echo -e "Not a valid index: \e[31m$1\e[0m" 233 echo -e "Not a valid index: \e[31m$1\e[0m"
234 echo Use the index in the first column instead: 234 echo Use the index in the first column instead:
235 echo 235 echo
236 _show_list "$index" 236 _show_list "$INDEX"
237 return 1 237 return 1
238 fi 238 fi
239} 239}
@@ -262,14 +262,14 @@ _delete_gist() {
262 _gist_id "$i" 262 _gist_id "$i"
263 curl -X DELETE -s -H "$auth_header" $GITHUB_API/gists/$GIST_ID \ 263 curl -X DELETE -s -H "$auth_header" $GITHUB_API/gists/$GIST_ID \
264 && echo "$i" deleted \ 264 && echo "$i" deleted \
265 && sed -i -E "/^$i / d" $index 265 && sed -i -E "/^$i / d" $INDEX
266 done 266 done
267} 267}
268 268
269# remove repos which are not in user gists anymore 269# remove repos which are not in user gists anymore
270_clean_repos() { 270_clean_repos() {
271 comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ 271 comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \
272 <(cat $index $starred 2> /dev/null | cut -d' ' -f2 | sed 's#.*/##' | sort) \ 272 <(cat $INDEX $STARRED 2> /dev/null | cut -d' ' -f2 | sed 's#.*/##' | sort) \
273 | while read dir; do 273 | while read dir; do
274 mv $folder/$dir /tmp && echo move $folder/$dir to /tmp 274 mv $folder/$dir /tmp && echo move $folder/$dir to /tmp
275 done 275 done
@@ -352,11 +352,11 @@ _create_gist() {
352 | curl -s -H "$auth_header" --data @- $GITHUB_API/gists \ 352 | curl -s -H "$auth_header" --data @- $GITHUB_API/gists \
353 | sed '1 s/^/[/; $ s/$/]/' \ 353 | sed '1 s/^/[/; $ s/$/]/' \
354 | _parse_response \ 354 | _parse_response \
355 | sed -E "s/^/$(( $(wc -l $index | cut -d' ' -f1) + 1 )) /" >> $index \ 355 | sed -E "s/^/$(( $(wc -l $INDEX | cut -d' ' -f1) + 1 )) /" >> $INDEX \
356 && echo -e '\nGist created' \ 356 && echo -e '\nGist created' \
357 || echo 'Fail to create gist' 357 || echo 'Fail to create gist'
358 358
359 _show_list $index | tail -1 359 _show_list $INDEX | tail -1
360} 360}
361 361
362# update description of a gist 362# update description of a gist
@@ -376,9 +376,9 @@ _help_message() {
376 376
377case "$1" in 377case "$1" in
378 "") 378 "")
379 _show_list $index ;; 379 _show_list $INDEX ;;
380 star | s) 380 star | s)
381 _show_list $starred ;; 381 _show_list $STARRED ;;
382 update | u) 382 update | u)
383 _update "$2" ;; 383 _update "$2" ;;
384 new | n) 384 new | n)