soybeanjs/elegant-router

[功能] 在`git`的功能执行时推迟路由文件生成,避免导致工作目录冲突

Opened this issue · 0 comments

症状

在进行【部分提交】时选中部分文件因为预提交检测失败直接从工作目录中消失了。需要手动恢复。

原因

在进行【部分提交】时会产生文件短暂的变动,elegant-router检测到文件变化立即去生成新的路由文件。
而提交内容在预提交检测可能因为提交存在错误被拒绝,ide就需要恢复工作目录到提交前状态。
由于elegant-router在提交中因为文件变化触发重新生成路由文件使得工作目录中的生成内容发生变化,直接后续的恢复冲突,恢复失败。

建议

路由生成前请检测当前git的状态,提交中,合并中,变基中等时推迟生成,避免对当前工作目录产生不可控的干扰。

以下是gpt给出的一些判断状态,我认为是需要考虑改进的,不是什么时刻都能立即进行生成的。

#仅参考,实际实现需要再次查询`git`官方资料
#!/bin/bash

GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
if [ -z "$GIT_DIR" ]; then
  echo "Not a git repository"
  exit 1
fi

if [ -f "$GIT_DIR/index.lock" ]; then
  echo "Commit in progress"
elif [ -f "$GIT_DIR/MERGE_HEAD" ]; then
  echo "Merge in progress"
elif [ -d "$GIT_DIR/rebase-apply" ]; then
  echo "Rebase in progress (rebase-apply)"
elif [ -d "$GIT_DIR/rebase-merge" ]; then
  echo "Rebase in progress (rebase-merge)"
elif [ -f "$GIT_DIR/rebase-merge/interactive" ]; then
  echo "Interactive rebase in progress"
elif [ -f "$GIT_DIR/CHERRY_PICK_HEAD" ]; then
  echo "Cherry-pick in progress"
else
  echo "No operations in progress"
fi

补充git日志

[STARTED] Preparing lint-staged...
[COMPLETED] Preparing lint-staged...
[STARTED] Hiding unstaged changes to partially staged files...
[COMPLETED] Hiding unstaged changes to partially staged files...
[STARTED] Running tasks for staged files...
[STARTED] package.json — 19 files
[STARTED] * — 19 files
[STARTED] eslint --fix
[FAILED] eslint --fix [FAILED]
[FAILED] eslint --fix [FAILED]
[COMPLETED] Running tasks for staged files...
[STARTED] Applying modifications from tasks...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Restoring unstaged changes to partially staged files...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[FAILED] error: Your local changes to the following files would be overwritten by merge:
	src/router/elegant/imports.ts
	src/router/elegant/transform.ts
	src/typings/elegant-router.d.ts
Please commit your changes or stash them before you merge.
Aborting
Index was not unstashed.