diff options
-rwxr-xr-x | gist | 81 |
1 files changed, 55 insertions, 26 deletions
@@ -17,6 +17,7 @@ | |||
17 | # gist | 17 | # gist |
18 | # | 18 | # |
19 | # * clone gist repos which are not in local | 19 | # * clone gist repos which are not in local |
20 | # * pull master branch if a local repo is behind its remote | ||
20 | # gist [sync | s] | 21 | # gist [sync | s] |
21 | # | 22 | # |
22 | # * Go to local gist repo | 23 | # * Go to local gist repo |
@@ -50,24 +51,47 @@ mkdir -p $folder | |||
50 | index=$folder/index | 51 | index=$folder/index |
51 | 52 | ||
52 | # Validate settings. | 53 | # Validate settings. |
53 | [ "$TRACE" ] && set -x && echo foo | 54 | [ "$TRACE" ] && set -x |
55 | |||
56 | # Show the list of gist, but not updated time | ||
57 | _show_list() { | ||
58 | cat $index | cut -d' ' -f1-2,4- | ||
59 | } | ||
54 | 60 | ||
55 | # get the list of gists | 61 | # get the list of gists |
56 | _update() { | 62 | _update() { |
57 | curl -s -H "$auth_header" $github_api/users/$user/gists |\ | 63 | curl -s -H "$auth_header" $github_api/users/$user/gists |\ |
58 | jq '.[] | "\( .html_url ) \(.files | keys | length) \(.comments) \( .description )"' |\ | 64 | tee jojo |\ |
59 | tr -d '"' | tac | nl |\ | 65 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ |
60 | while read line_num link file_num comment_num description; do | 66 | tac | nl |\ |
61 | echo $line_num $link $file_num $comment_num $(echo $description | cut -c -70) | 67 | while read line_num link blobs file_num comment_num description; do |
62 | done | tee $index | 68 | blob_code=$(echo $blobs | jq -r '.[]' | sed -r 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
69 | echo $line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -80) | tr -d '"' | ||
70 | done > $index && \ | ||
71 | _show_list | ||
63 | } | 72 | } |
64 | 73 | ||
65 | # clone repos which are not in the local | ||
66 | # TODO pull if repo is outdated | ||
67 | _sync_repos() { | 74 | _sync_repos() { |
75 | # clone repos which are not in the local | ||
68 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 76 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
69 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ | 77 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ |
70 | xargs -I{} git clone git@github.com:{}.git $folder/{} | 78 | xargs -I{} git clone git@github.com:{}.git $folder/{} |
79 | |||
80 | # pull if remote repo has different blob objects | ||
81 | cat $index | cut -d' ' -f2,3 |\ | ||
82 | while read url blob_code_remote; do | ||
83 | repo=$folder/$(echo $url | sed 's#.*/##') | ||
84 | blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-') | ||
85 | |||
86 | cd $repo && \ | ||
87 | [[ $blob_code_local != $blob_code_remote ]] && \ | ||
88 | [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] && \ | ||
89 | git pull | ||
90 | done | ||
91 | } | ||
92 | |||
93 | _gist_id() { | ||
94 | cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##' | ||
71 | } | 95 | } |
72 | 96 | ||
73 | _goto_gist() { | 97 | _goto_gist() { |
@@ -76,17 +100,17 @@ _goto_gist() { | |||
76 | echo Not a valid gist number: $1 | 100 | echo Not a valid gist number: $1 |
77 | echo Use the number in the first column instead: | 101 | echo Use the number in the first column instead: |
78 | echo | 102 | echo |
79 | cat $index | 103 | show_list |
80 | return 0 | 104 | return 0 |
81 | fi | 105 | fi |
82 | 106 | ||
83 | GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') | 107 | GIST_ID=$(_gist_id $1) |
84 | echo This gist is at $folder/$GIST_ID | 108 | echo This gist is at $folder/$GIST_ID |
85 | cd $folder/$GIST_ID && tig --all 2> /dev/null | 109 | cd $folder/$GIST_ID && tig --all 2> /dev/null |
86 | } | 110 | } |
87 | 111 | ||
88 | _delete_gist() { | 112 | _delete_gist() { |
89 | GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') | 113 | GIST_ID=$(_gist_id $1) |
90 | curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \ | 114 | curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \ |
91 | _update | 115 | _update |
92 | } | 116 | } |
@@ -99,33 +123,37 @@ _clean_repos() { | |||
99 | } | 123 | } |
100 | 124 | ||
101 | _show_detail() { | 125 | _show_detail() { |
102 | GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') | 126 | GIST_ID=$(_gist_id $1) |
103 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ | 127 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ |
104 | jq '{site: .html_url, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' | 128 | jq '{site: .html_url, description: .description, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' |
105 | 129 | ||
106 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\ | 130 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\ |
107 | jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' | 131 | jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' |
108 | } | 132 | } |
109 | 133 | ||
110 | # create a new gist with a file and description | 134 | # create a new gist with files |
111 | # TODO support folder of files | 135 | # TODO support folder of files |
112 | _create_gist() { | 136 | _create_gist() { |
113 | FILE=$(basename $1) | 137 | echo -n 'description: ' |
114 | 138 | read DESC | |
115 | jq --arg FILE "$FILE" --arg DESC "$2" '. as $content | { | 139 | |
116 | public: true, | 140 | echo $@ | tr " " "\n" |\ |
117 | files: { | 141 | while read file; do |
118 | ($FILE): {content: $content} | 142 | FILE=$(basename $file) |
119 | }, | 143 | jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file |
144 | done |\ | ||
145 | jq -s --arg DESC "$DESC" '{ | ||
146 | public: true, | ||
147 | files: add, | ||
120 | description: ($DESC) | 148 | description: ($DESC) |
121 | }' -R -s $1 |\ | 149 | }' |\ |
122 | curl -s -H "$auth_header" --data @- $github_api/gists > /dev/null && \ | 150 | curl -s -H "$auth_header" --data @- $github_api/gists > /dev/null && \ |
123 | _update | 151 | _update && _sync_repos |
124 | } | 152 | } |
125 | 153 | ||
126 | # update description of a gist | 154 | # update description of a gist |
127 | _edit_gist() { | 155 | _edit_gist() { |
128 | GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') | 156 | GIST_ID=$(_gist_id $1) |
129 | 157 | ||
130 | jq -n --arg DESC "$2" '{ description: ($DESC) }' |\ | 158 | jq -n --arg DESC "$2" '{ description: ($DESC) }' |\ |
131 | curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \ | 159 | curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \ |
@@ -134,10 +162,11 @@ _edit_gist() { | |||
134 | 162 | ||
135 | case "$1" in | 163 | case "$1" in |
136 | "") | 164 | "") |
137 | cat $index | 165 | _show_list |
138 | ;; | 166 | ;; |
139 | create | c) | 167 | create | c) |
140 | _create_gist "$2" "$3" | 168 | shift; |
169 | _create_gist $@ | ||
141 | ;; | 170 | ;; |
142 | edit | e) | 171 | edit | e) |
143 | _edit_gist "$2" "$3" | 172 | _edit_gist "$2" "$3" |