Skip to content

Commit

Permalink
fix: cr fix
Browse files Browse the repository at this point in the history
  • Loading branch information
EscapeB committed Feb 22, 2024
1 parent da300e8 commit f341ce6
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 26 deletions.
12 changes: 2 additions & 10 deletions apps/sparo-lib/src/cli/commands/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {
);
if (!branchExistsInLocal) {
// fetch from remote
const remote: string = this._getBranchRemote();
const remote: string = this._gitService.getBranchRemote(branch);

const fetchResult: child_process.SpawnSyncReturns<string> = this._gitService.executeGitCommand({
args: ['fetch', remote, `refs/heads/${branch}:refs/remotes/${remote}/${branch}`]
});
Expand Down Expand Up @@ -210,15 +211,6 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {
return currentBranch;
}

private _getBranchRemote(): string {
/**
* TODO: Git supports multiple remotes. We need to support using a different
* remote from "origin". A possible way is reading from git config by running
* "git config --get branch.<branch>.remote"
*/
return 'origin';
}

private _processProfilesFromArg(profilesFromArg: string[]): { isNoProfile: boolean; profiles: string[] } {
/**
* --profile is defined as array type parameter, specifying --no-profile is resolved to false by yargs.
Expand Down
17 changes: 13 additions & 4 deletions apps/sparo-lib/src/cli/commands/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@ import type { TerminalService } from '../../services/TerminalService';
export interface IFetchCommandOptions {
all?: boolean;
branch?: string;
remote?: string;
}

@Command()
export class FetchCommand implements ICommand<IFetchCommandOptions> {
public cmd: string = 'fetch';
public cmd: string = 'fetch [remote] [branch]';
public description: string = 'fetch remote branch to local';

@inject(GitService) private _gitService!: GitService;
public builder(yargs: Argv<{}>): void {
yargs.boolean('full');
/**
* sparo fetch <remote> <branch> [--all]
*/
yargs
.positional('remote', { type: 'string' })
.positional('branch', { type: 'string' })
.string('remote')
.string('branch')
.boolean('full');
}

public handler = async (
Expand All @@ -32,13 +41,13 @@ export class FetchCommand implements ICommand<IFetchCommandOptions> {
const { branch: defaultBranch } = repoInfo;

terminal.writeDebugLine(`got args in fetch command: ${JSON.stringify(args)}`);
const { all, branch = defaultBranch } = args;
const { all, branch = defaultBranch, remote = this._gitService.getBranchRemote(branch) } = args;
const fetchArgs: string[] = ['fetch'];

if (all) {
fetchArgs.push('--all');
} else {
fetchArgs.push('origin', branch);
fetchArgs.push(remote, branch);
}

gitService.executeGitCommand({ args: fetchArgs });
Expand Down
6 changes: 3 additions & 3 deletions apps/sparo-lib/src/cli/commands/git-checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export class GitCheckoutCommand implements ICommand<{}> {
const { _gitService: gitService } = this;
const { terminal } = terminalService;
const rawArgs: string[] = process.argv.slice(2);
const idx: number = rawArgs.indexOf(this.cmd);
if (idx >= 0) {
rawArgs[idx] = 'checkout';
const index: number = rawArgs.indexOf(this.cmd);
if (index >= 0) {
rawArgs[index] = 'checkout';
}
terminal.writeDebugLine(`proxy args in git-checkout command: ${JSON.stringify(rawArgs)}`);
gitService.executeGitCommand({ args: rawArgs });
Expand Down
6 changes: 3 additions & 3 deletions apps/sparo-lib/src/cli/commands/git-clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export class GitCloneCommand implements ICommand<{}> {
const { _gitService: gitService } = this;
const { terminal } = terminalService;
const rawArgs: string[] = process.argv.slice(2);
const idx: number = rawArgs.indexOf(this.cmd);
if (idx >= 0) {
rawArgs[idx] = 'clone';
const index: number = rawArgs.indexOf(this.cmd);
if (index >= 0) {
rawArgs[index] = 'clone';
}
terminal.writeDebugLine(`proxy args in git-clone command: ${JSON.stringify(rawArgs)}`);
gitService.executeGitCommand({ args: rawArgs });
Expand Down
6 changes: 3 additions & 3 deletions apps/sparo-lib/src/cli/commands/git-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class GitFetchCommand implements ICommand<{}> {
const { _gitService: gitService } = this;
const { terminal } = terminalService;
const rawArgs: string[] = process.argv.slice(2);
const idx: number = rawArgs.indexOf(this.cmd);
if (idx >= 0) {
rawArgs[idx] = 'fetch';
const index: number = rawArgs.indexOf(this.cmd);
if (index >= 0) {
rawArgs[index] = 'fetch';
}
terminal.writeDebugLine(`proxy args in git-fetch command: ${JSON.stringify(rawArgs)}`);
gitService.executeGitCommand({ args: rawArgs });
Expand Down
6 changes: 3 additions & 3 deletions apps/sparo-lib/src/cli/commands/git-pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export class GitPullCommand implements ICommand<{}> {
const { _gitService: gitService } = this;
const { terminal } = terminalService;
const rawArgs: string[] = process.argv.slice(2);
const idx: number = rawArgs.indexOf(this.cmd);
if (idx >= 0) {
rawArgs[idx] = 'pull';
const index: number = rawArgs.indexOf(this.cmd);
if (index >= 0) {
rawArgs[index] = 'pull';
}
terminal.writeDebugLine(`proxy args in git-pull command: ${JSON.stringify(rawArgs)}`);
gitService.executeGitCommand({ args: rawArgs });
Expand Down
16 changes: 16 additions & 0 deletions apps/sparo-lib/src/services/GitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,22 @@ Please specify a directory on the command line
return getRepoInfo();
}

public getBranchRemote(branch: string): string {
const gitPath: string = this.getGitPathOrThrow();
const { stdout, status } = Executable.spawnSync(gitPath, ['config', `branch.${branch}.remote`]);

if (status === 1 && stdout.trim().length === 0) {
// git config branch.<branch_name>.remote can't get correct remote in these two scenarios
// 1. If target branch is not checked out locally.
// 2. If target branch is a newly created local branch and not pushed to remote.
// For these two scenarios, just return origin as default.
return 'origin';
} else if (status !== 0) {
throw new Error(`Can't get remote for branch ${branch}`);
}
return stdout.trim();
}

public getGitVersion(): [number, number, number] | undefined {
let result: [number, number, number] | undefined;

Expand Down
Empty file modified apps/sparo/bin/sparo
100644 → 100755
Empty file.
Empty file modified apps/sparo/bin/sparo-ci
100644 → 100755
Empty file.
10 changes: 10 additions & 0 deletions common/changes/sparo/fix-cr_fix_2024-02-22-07-24.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "sparo",
"comment": "add remote and branch arguments for sparo fetch & cr fix",
"type": "none"
}
],
"packageName": "sparo"
}
2 changes: 2 additions & 0 deletions common/reviews/api/sparo-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class GitService {
executeGitCommandAndCaptureOutput({ args, workingDirectory }: IExecuteGitCommandParams): string;
getBasenameFromUrl(url: string): string;
// (undocumented)
getBranchRemote(branch: string): string;
// (undocumented)
getGitConfig(k: string, option?: {
dryRun?: boolean;
global?: boolean;
Expand Down

0 comments on commit f341ce6

Please sign in to comment.