Refactor xgit upload defaults to preserve history; add --message/--force controls#6
Refactor xgit upload defaults to preserve history; add --message/--force controls#6Copilot wants to merge 7 commits into
xgit upload defaults to preserve history; add --message/--force controls#6Conversation
| targetBranch: string, | ||
| targetFolder?: string | ||
| targetFolder?: string, | ||
| { message = 'upload by Git-utility CLI', force = false } = {} |
There was a problem hiding this comment.
已改为普通参数形式,uploadFolder 现在使用 message、force 常规参数,不再用对象解构。已在提交 dfb9cd4 中处理。CLI 变更无 UI,截图不适用。
| if (targetFolder) { | ||
| const tempFolder = path.join(os.tmpdir(), new URL(GitURL).pathname); | ||
|
|
||
| await fs.remove(tempFolder); | ||
| await fs.mkdirp(tempFolder); | ||
| cd(tempFolder); | ||
|
|
||
| await $`git clone -b ${targetBranch} ${GitURL} .`; | ||
|
|
||
| targetFolder = path.join(tempFolder, targetFolder); | ||
|
|
||
| await fs.remove(targetFolder); | ||
| await fs.mkdirp(targetFolder); | ||
| await fs.copy(sourceFolder, targetFolder); | ||
| await fs.remove(path.join(targetFolder, '.git')); | ||
|
|
||
| await $`git add .`; | ||
| await $`git commit -m "upload by Git-utility CLI"`; | ||
| await $`git push origin ${targetBranch}`; | ||
| } else { | ||
| if (force) { | ||
| cd(sourceFolder); | ||
|
|
||
| await $`git init`; | ||
| await $`git remote add origin ${GitURL}`; | ||
| await $`git checkout -b ${targetBranch}`; | ||
| await $`git add .`; | ||
| await $`git commit -m "upload by Git-utility CLI"`; | ||
| await $`git commit -m ${message}`; | ||
| await $`git push --set-upstream origin ${targetBranch} -f`; | ||
| await fs.remove('.git'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const tempFolder = path.join(os.tmpdir(), new URL(GitURL).pathname); | ||
|
|
||
| await fs.remove(tempFolder); | ||
| await fs.mkdirp(tempFolder); | ||
| cd(tempFolder); | ||
|
|
||
| await $`git clone -b ${targetBranch} ${GitURL} .`; | ||
|
|
||
| const uploadTarget = targetFolder | ||
| ? path.join(tempFolder, targetFolder) | ||
| : tempFolder; | ||
|
|
||
| await fs.mkdirp(uploadTarget); | ||
|
|
||
| for (const entry of await fs.readdir(sourceFolder)) | ||
| if (entry !== '.git') | ||
| await fs.copy( | ||
| path.join(sourceFolder, entry), | ||
| path.join(uploadTarget, entry), | ||
| { | ||
| overwrite: true | ||
| } | ||
| ); | ||
|
|
||
| await $`git add .`; | ||
| await $`git commit -m ${message}`; | ||
| await $`git push origin ${targetBranch}`; |
There was a problem hiding this comment.
已按原有顶层分支顺序调整为 if (targetFolder) ... else ...,同时保留默认保留历史的新行为。已在提交 dfb9cd4 中处理。CLI 变更无 UI,截图不适用。
| } else { | ||
| cd(sourceFolder); | ||
|
|
||
| await $`git init`; | ||
| await $`git remote add origin ${GitURL}`; | ||
| await $`git checkout -b ${targetBranch}`; | ||
| await $`git add .`; | ||
| await $`git commit -m "upload by Git-utility CLI"`; | ||
| await $`git push --set-upstream origin ${targetBranch} -f`; | ||
| await fs.remove('.git'); | ||
| if (force) { |
There was a problem hiding this comment.
else 和 if 合并,可以减少一层缩进,才能看得清差异。
There was a problem hiding this comment.
已按建议把 else { if (...) ... } 合并为 else if (...),减少一层缩进,便于看差异。对应提交:b957b11。CLI 逻辑调整无 UI 变更,截图不适用。
| } else { | ||
| const tempFolder = path.join(os.tmpdir(), new URL(GitURL).pathname); | ||
|
|
||
| await fs.remove(tempFolder); | ||
| await fs.mkdirp(tempFolder); | ||
| cd(tempFolder); | ||
|
|
||
| await $`git clone -b ${targetBranch} ${GitURL} .`; | ||
|
|
||
| for (const entry of await fs.readdir(sourceFolder)) | ||
| if (entry !== '.git') | ||
| await fs.copy( | ||
| path.join(sourceFolder, entry), | ||
| path.join(tempFolder, entry), | ||
| { | ||
| overwrite: true | ||
| } | ||
| ); | ||
|
|
||
| await $`git add .`; | ||
| await $`git commit -m ${message}`; | ||
| await $`git push origin ${targetBranch}`; | ||
| } |
There was a problem hiding this comment.
给 targetFolder = '.' 的默认值,这个分支是不是就和最上面的分支能合并成一种?
There was a problem hiding this comment.
已处理,统一了非 force 的上传分支:通过 targetFolder ||= '.' 让根目录上传与目标子目录上传走同一套 clone/overlay/push 逻辑;仅在“未传 targetFolder 且 --force”时走强推分支。对应提交:2a7229b。CLI 逻辑调整无 UI 变更,截图不适用。
| if (uploadToRoot && force) { | ||
| cd(sourceFolder); | ||
|
|
||
| await $`git init`; | ||
| await $`git remote add origin ${GitURL}`; | ||
| await $`git checkout -b ${targetBranch}`; | ||
| await $`git add .`; | ||
| await $`git commit -m ${message}`; | ||
| await $`git push --set-upstream origin ${targetBranch} -f`; | ||
| await fs.remove('.git'); | ||
| } else { |
There was a problem hiding this comment.
把这个分支放到下面,还是那个原则 —— 把 Git 差异降到最低!
There was a problem hiding this comment.
已调整为把该分支放到下面:先走通用上传路径,force 特例放到 else,以尽量贴近原始分支顺序并降低差异。对应提交:988c347。CLI 逻辑调整无 UI 变更,截图不适用。
uploadFolderimplementation