feat: sparo fetch --all will fetch all remote branches

This commit is contained in:
Cheng Liu 2024-04-12 14:22:24 -07:00
parent 330402ed5a
commit 6877978893
No known key found for this signature in database
GPG key ID: EEC8452F7DB85CD6
4 changed files with 74 additions and 5 deletions

View file

@ -28,7 +28,7 @@ export class FetchCommand implements ICommand<IFetchCommandOptions> {
.positional('branch', { type: 'string' })
.string('remote')
.string('branch')
.boolean('full');
.boolean('all');
}
public handler = async (
@ -44,16 +44,48 @@ export class FetchCommand implements ICommand<IFetchCommandOptions> {
const { all, branch = defaultBranch, remote = this._gitService.getBranchRemote(branch) } = args;
const fetchArgs: string[] = ['fetch'];
let remoteFetchGitConfig: string[] | undefined;
if (all) {
// Temporary revert single branch fetch
const currentRemoteFetchGitConfig: string[] | undefined = this._getRemoteFetchGitConfig(remote);
if (currentRemoteFetchGitConfig) {
this._setAllBranchFetch(remote);
remoteFetchGitConfig = currentRemoteFetchGitConfig;
}
fetchArgs.push('--all');
} else {
fetchArgs.push(remote, branch);
}
gitService.executeGitCommand({ args: fetchArgs });
if (remoteFetchGitConfig) {
this._restoreSingleBranchFetch(remote, remoteFetchGitConfig);
}
};
public getHelp(): string {
return `fetch help`;
}
private _getRemoteFetchGitConfig(remote: string): string[] | undefined {
const result: string | undefined = this._gitService.getGitConfig(`remote.${remote}.fetch`, {
array: true
});
return result?.split('\n').filter(Boolean);
}
private _setAllBranchFetch(remote: string): void {
this._gitService.setGitConfig(`remote.${remote}.fetch`, `+refs/heads/*:refs/remotes/${remote}/*`, {
replaceAll: true
});
}
private _restoreSingleBranchFetch(remote: string, remoteFetchGitConfig: string[]): void {
this._gitService.unsetGitConfig(`remote.${remote}.fetch`);
for (const value of remoteFetchGitConfig) {
this._gitService.setGitConfig(`remote.${remote}.fetch`, value, { add: true });
}
}
}

View file

@ -38,17 +38,23 @@ export class GitService {
public setGitConfig(
k: string,
v: string | number | boolean,
option?: { dryRun?: boolean; global?: boolean }
option?: { dryRun?: boolean; global?: boolean; replaceAll?: boolean; add?: boolean }
): void {
const gitPath: string = this.getGitPathOrThrow();
const currentWorkingDirectory: string = this.getRepoInfo().root;
const { dryRun = false, global = false } = option ?? {};
const { dryRun = false, global = false, replaceAll = false, add = false } = option ?? {};
const args: string[] = [];
args.push('config');
if (global) {
args.push('--global');
}
if (add) {
args.push('--add');
}
if (replaceAll) {
args.push('--replace-all');
}
args.push(k, String(v));
@ -64,15 +70,21 @@ export class GitService {
}
}
public getGitConfig(k: string, option?: { dryRun?: boolean; global?: boolean }): string | undefined {
public getGitConfig(
k: string,
option?: { dryRun?: boolean; global?: boolean; array?: boolean }
): string | undefined {
const gitPath: string = this.getGitPathOrThrow();
const currentWorkingDirectory: string = this.getRepoInfo().root;
const { dryRun = false, global = false } = option ?? {};
const { dryRun = false, global = false, array = false } = option ?? {};
const args: string[] = [];
args.push('config');
if (global) {
args.push('--global');
}
if (array) {
args.push('--get-all');
}
args.push(k);
this._terminalService.terminal.writeDebugLine(`get git config with args ${JSON.stringify(args)}`);
if (!dryRun) {
@ -93,6 +105,25 @@ export class GitService {
return undefined;
}
public unsetGitConfig(k: string): void {
const gitPath: string = this.getGitPathOrThrow();
const currentWorkingDirectory: string = this.getRepoInfo().root;
const args: string[] = [];
args.push('config');
args.push('--unset-all');
args.push(k);
this._terminalService.terminal.writeDebugLine(`unset git config with args ${JSON.stringify(args)}`);
const { status, stderr } = Executable.spawnSync(gitPath, args, {
currentWorkingDirectory,
stdio: 'inherit'
});
if (status !== 0) {
throw new Error(`Error while unsetting git config: ${stderr}`);
}
}
public setRecommendConfig(option?: { overwrite?: boolean; dryRun?: boolean }): void {
const { overwrite = false, dryRun = false } = option ?? {};
const recommendedConfigs: [string, string, number][] = [

View file

@ -28,6 +28,7 @@ export class GitService {
getGitConfig(k: string, option?: {
dryRun?: boolean;
global?: boolean;
array?: boolean;
}): string | undefined;
// (undocumented)
getGitEmail(): string | undefined;
@ -50,12 +51,16 @@ export class GitService {
setGitConfig(k: string, v: string | number | boolean, option?: {
dryRun?: boolean;
global?: boolean;
replaceAll?: boolean;
add?: boolean;
}): void;
// (undocumented)
setRecommendConfig(option?: {
overwrite?: boolean;
dryRun?: boolean;
}): void;
// (undocumented)
unsetGitConfig(k: string): void;
}
// @alpha

View file

@ -9,6 +9,7 @@ words:
- GVFS
- humanish
- inversify
- Positionals
- rushstack
- Sparo
- tiktok