feat: sparo pull

This commit is contained in:
Cheng Liu 2024-04-09 15:51:05 -07:00
parent 7babbfaf5f
commit 9aadefbcbd
No known key found for this signature in database
GPG key ID: EEC8452F7DB85CD6
3 changed files with 16 additions and 24 deletions

View file

@ -11,7 +11,7 @@ import { GitCheckoutCommand } from './git-checkout';
import { GitFetchCommand } from './git-fetch'; import { GitFetchCommand } from './git-fetch';
import { GitPullCommand } from './git-pull'; import { GitPullCommand } from './git-pull';
import { InitProfileCommand } from './init-profile'; import { InitProfileCommand } from './init-profile';
// import { PullCommand } from './pull'; import { PullCommand } from './pull';
// When adding new Sparo subcommands, remember to update this doc page: // When adding new Sparo subcommands, remember to update this doc page:
// https://github.com/tiktok/sparo/blob/main/apps/website/docs/pages/commands/overview.md // https://github.com/tiktok/sparo/blob/main/apps/website/docs/pages/commands/overview.md
@ -23,8 +23,7 @@ export const COMMAND_LIST: Constructable[] = [
CloneCommand, CloneCommand,
CheckoutCommand, CheckoutCommand,
FetchCommand, FetchCommand,
// Should be introduced after sparo merge|rebase PullCommand,
// PullCommand,
// The commands customized by Sparo require a mirror command to Git // The commands customized by Sparo require a mirror command to Git
GitCloneCommand, GitCloneCommand,

View file

@ -8,15 +8,12 @@ import type { ICommand } from './base';
import type { TerminalService } from '../../services/TerminalService'; import type { TerminalService } from '../../services/TerminalService';
export interface IPullCommandOptions { export interface IPullCommandOptions {
branch?: string;
remote?: string;
profile?: string[]; profile?: string[];
addProfile?: string[];
} }
@Command() @Command()
export class PullCommand implements ICommand<IPullCommandOptions> { export class PullCommand implements ICommand<IPullCommandOptions> {
public cmd: string = 'pull [remote] [branch]'; public cmd: string = 'pull';
public description: string = 'Incorporates changes from a remote repository into the current branch.'; public description: string = 'Incorporates changes from a remote repository into the current branch.';
@inject(GitService) private _gitService!: GitService; @inject(GitService) private _gitService!: GitService;
@ -24,18 +21,19 @@ export class PullCommand implements ICommand<IPullCommandOptions> {
public builder(yargs: Argv<{}>): void { public builder(yargs: Argv<{}>): void {
/** /**
* sparo pull [remote] [branch] --profile <profile_name> --add-profile <profile_name> --no-profile * sparo pull [repository] [refsepc...] [--profile <profile_name> | --no-profile]
*
* sparo pull origin
*
* sparo pull origin master
*/ */
yargs yargs
.positional('remote', { type: 'string' })
.positional('branch', { type: 'string' })
.string('remote')
.string('branch')
.boolean('full')
.array('profile') .array('profile')
.default('profile', []) .default('profile', [])
.array('add-profile') .parserConfiguration({ 'unknown-options-as-args': true })
.default('add-profile', []); .usage(
'Usage: sparo pull [options] [repository] [refsepc...] [--profile <profile_name> | --no-profile]'
);
} }
public handler = async ( public handler = async (
@ -46,17 +44,12 @@ export class PullCommand implements ICommand<IPullCommandOptions> {
const { terminal } = terminalService; const { terminal } = terminalService;
terminal.writeDebugLine(`got args in pull command: ${JSON.stringify(args)}`); terminal.writeDebugLine(`got args in pull command: ${JSON.stringify(args)}`);
const pullArgs: string[] = ['pull']; // Collect anything that is not related to profile, pass down them to native git pull
const pullArgs: string[] = args._ as string[];
const { branch, remote } = args;
if (branch && remote) {
pullArgs.push(remote, branch);
}
const { isNoProfile, profiles, addProfiles } = await sparoProfileService.preprocessProfileArgs({ const { isNoProfile, profiles, addProfiles } = await sparoProfileService.preprocessProfileArgs({
profilesFromArg: args.profile ?? [], profilesFromArg: args.profile ?? [],
addProfilesFromArg: args.addProfile ?? [] addProfilesFromArg: []
}); });
// invoke native git pull command // invoke native git pull command

View file

@ -91,6 +91,6 @@ export class LocalState {
if (!repoRoot) { if (!repoRoot) {
throw new Error('Running outside of the git repository folder'); throw new Error('Running outside of the git repository folder');
} }
return path.join(this._gitService.getRepoInfo().root, '.git/info/local-state.json'); return path.join(repoRoot, '.git/info/local-state.json');
} }
} }