|
36 | 36 | uses: actions/checkout@v3 |
37 | 37 | with: |
38 | 38 | persist-credentials: true |
39 | | - fetch-depth: 1 |
| 39 | + fetch-depth: 2 |
40 | 40 |
|
41 | 41 | - name: Whether Skip Deploy(.nodeploy|filePath) |
42 | 42 | id: deploy-check |
|
78 | 78 | - name: Fetch and checkout each branch (shallow) |
79 | 79 | if: env.SKIP_DEPLOY != 'true' |
80 | 80 | run: | |
| 81 | + # TODO: Let's change this(only needed branch) |
81 | 82 | # git clone git@github.com:LetMeFly666/LeetCode.git --depth=1 |
82 | 83 | # cd LeetCode |
83 | 84 | # git rev-list --count HEAD |
@@ -169,51 +170,56 @@ jobs: |
169 | 170 | run: | |
170 | 171 | set -e |
171 | 172 |
|
172 | | - json_file="../website_modifyTime/LastModifiedTime.json" |
| 173 | + # git rev-list --count HEAD |
| 174 | + json_file_name="LastModifiedTime.json" |
| 175 | + json_file="../website_modifyTime/$json_file_name" |
173 | 176 |
|
174 | | - # 获取本次 push 里 master 分支变动的文件列表 |
175 | | - # TODO: Let's处理删除removed |
176 | | - # TODO: Let's solve this |
177 | | - echo '${{ toJSON(github.event) }}' | jq . |
178 | | - changed_files=$(jq -r ' |
179 | | - [ |
180 | | - .commits[].added[]?, |
181 | | - .commits[].modified[]? |
182 | | - ] |
183 | | - | unique |
184 | | - | .[] |
185 | | - ' "$GITHUB_EVENT_PATH") |
186 | | - echo "Changed files:" |
187 | | - echo "$changed_files" |
| 177 | + if [ -f "$json_file" ]; then |
| 178 | + json_content=$(cat "$json_file") |
| 179 | + else |
| 180 | + json_content='{}' |
| 181 | + echo "original Modified Json File Not exists" |
| 182 | + fi |
188 | 183 |
|
189 | | - # 保存原 json 内容顺序 |
190 | | - tmp_json=$(mktemp) |
191 | | - cp "$json_file" "$tmp_json" |
| 184 | + commit_time=$(git show -s --format=%cI HEAD) |
| 185 | + echo "Commit time: $commit_time" |
192 | 186 |
|
193 | | - # 遍历每个文件,看是否是 Solutions 下的 md 文件 |
194 | | - updated=false |
195 | | - commit_time=$(git show -s --format=%cI ${{ github.sha }}) # ISO 8601 commit 时间 |
| 187 | + has_modified=false |
| 188 | + while read -r status file; do |
| 189 | + [[ "$file" == Solutions/*.md ]] || continue |
196 | 190 |
|
197 | | - while IFS= read -r file; do |
198 | | - # 只处理 Solutions/*.md 文件 |
199 | | - if [[ "$file" == Solutions/*.md ]]; then |
200 | | - filename=$(basename "$file") |
201 | | - # 使用 jq 更新 JSON 值 |
202 | | - # 保持原有顺序,用临时文件写回 |
203 | | - jq --arg key "$filename" --arg value "$commit_time" '(.[$key] = $value) | .' "$tmp_json" > "$tmp_json.tmp" && mv "$tmp_json.tmp" "$tmp_json" |
204 | | - updated=true |
205 | | - fi |
206 | | - done <<< "$changed_files" |
| 191 | + filename=$(basename "$file") |
| 192 | + has_modified=true |
| 193 | +
|
| 194 | + case "$status" in |
| 195 | + A|M) |
| 196 | + echo "Updated $filename" |
| 197 | + json_content=$(echo "$json_content" | jq \ |
| 198 | + --arg k "$filename" \ |
| 199 | + --arg v "$commit_time" \ |
| 200 | + '. + {($k): $v}') |
| 201 | + ;; |
| 202 | + D) |
| 203 | + echo "Deleted $filename" |
| 204 | + json_content=$(echo "$json_content" | jq \ |
| 205 | + --arg k "$filename" \ |
| 206 | + 'del(.[$k])') |
| 207 | + ;; |
| 208 | + esac |
| 209 | + done < <(git diff --name-status HEAD~1 HEAD) |
207 | 210 |
|
208 | | - if [ "$updated" != true ]; then |
209 | | - echo "No Solutions/*.md file changed, skip JSON update." |
| 211 | + if [ "$has_modified" = false ]; then |
| 212 | + echo "no modified Solutions/*.md files" |
210 | 213 | exit 0 |
211 | 214 | fi |
212 | 215 |
|
| 216 | + # 按 key 字典序排序并写回 |
| 217 | + jq -S --indent 4 '.' <<<"$json_content" > "$json_file" |
| 218 | + echo "Updated $json_file" |
| 219 | +
|
213 | 220 | # 提交修改 |
214 | | - cp "$tmp_json" "$json_file" |
215 | 221 | cd ../website_modifyTime |
216 | | - git add "$json_file" |
| 222 | + git add "$json_file_name" |
217 | 223 | TOCOMMIT=$(printf "%s\n\n%s" "$MESSAGE" "$HASH") |
218 | 224 | git commit --allow-empty -m "$TOCOMMIT" |
219 | 225 | git push |
|
0 commit comments