-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(accureleaser): 从面向对象改为组合式的扩展git工具对象
- Loading branch information
Showing
2 changed files
with
36 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,66 @@ | ||
import * as fsp from 'fs/promises'; | ||
import simpleGit from 'simple-git'; | ||
|
||
class ExtendedGit { | ||
/**@param {import('simple-git').SimpleGit} git Git 操作对象 */ | ||
constructor(git) { | ||
this.git = git; | ||
} | ||
/** | ||
* @typedef {ReturnType<typeof getExtendedGit>} ExtendedGit | ||
*/ | ||
|
||
/**@param {import('simple-git').SimpleGit} git Git 操作对象 */ | ||
function getExtendedGit(git) { | ||
const instance = {}; | ||
/** | ||
* 获取提交的差异索引 | ||
* @param {string} commit 提交 | ||
* @param {string} file 文件 | ||
*/ | ||
diffIndex = async (commit, file = '') => { | ||
return await this.git.raw('diff-index', ...file ? [commit, file] : [commit]); | ||
instance.diffIndex = async function (commit, file = '') { | ||
return await git.raw('diff-index', ...file ? [commit, file] : [commit]); | ||
}; | ||
/** | ||
* 查看一个文件本次提交是否是新建或修改过 | ||
* @param {string} file 文件路径 | ||
*/ | ||
isChanged = async file => { | ||
return await this.diffIndex('HEAD^', file) !== ''; | ||
instance.isChanged = async function (file) { | ||
return await instance.diffIndex('HEAD^', file) !== ''; | ||
}; | ||
/** | ||
* 查看一个文件本次提交是否是新建 | ||
* @param {string} file 文件路径 | ||
*/ | ||
isNewed = async file => { | ||
return (await this.diffIndex('HEAD^', file)).slice(1, 7) === '000000'; | ||
instance.isNewed = async function (file) { | ||
return (await instance.diffIndex('HEAD^', file)).slice(1, 7) === '000000'; | ||
}; | ||
/** | ||
* 读取特定提交的文件 | ||
* @param {string} commit 提交 | ||
* @param {string} file 文件 | ||
*/ | ||
checkoutFile = async (commit, file) => { | ||
await this.git.checkout(commit, ['--', file]); | ||
instance.checkoutFile = async function (commit, file) { | ||
await git.checkout(commit, ['--', file]); | ||
const content = await fsp.readFile(file); | ||
await this.git.checkout('HEAD', ['--', file]); | ||
await git.checkout('HEAD', ['--', file]); | ||
return content; | ||
}; | ||
|
||
return instance; | ||
} | ||
|
||
/** | ||
* 获得拓展过的 Git 操作对象 | ||
* @param {string} root Git 仓库地址 | ||
* @returns {ExtendedGit & import('simple-git').SimpleGit} | ||
*/ | ||
export default function getGit(root) { | ||
const git = simpleGit(root); | ||
return Object.assign(git, new ExtendedGit(git)); | ||
const sgit = simpleGit(root); | ||
const git = getExtendedGit(sgit); | ||
for (const key in sgit) { | ||
// @ts-ignore | ||
if (typeof sgit[key] === 'function') { | ||
// @ts-ignore | ||
git[key] = (...args) => sgit[key](...args); | ||
} | ||
} | ||
// @ts-ignore | ||
return git; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters