-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Git cli calls into output channel (#2225)
* add git executor class * add git reader class * register git commands as cli commands (offer to show error) * move constants * remove git util * remove arrow functions from new class * revert exporting of type
- Loading branch information
1 parent
e37da4a
commit eb156d8
Showing
37 changed files
with
462 additions
and
325 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,12 +1,11 @@ | ||
import { join } from 'path' | ||
import { Args } from './constants' | ||
import { joinTruthyItems } from '../util/array' | ||
|
||
export const getCommandString = ( | ||
pythonBinPath: string | undefined, | ||
executable: string, | ||
...args: Args | ||
): string => { | ||
const prefix = pythonBinPath ? join(pythonBinPath, 'python') : undefined | ||
return `${joinTruthyItems([prefix, executable])} ${args.join(' ')}` | ||
export const getCommandString = ({ | ||
args, | ||
executable | ||
}: { | ||
args: string[] | ||
executable: string | ||
}): string => { | ||
return `${joinTruthyItems([executable, ...args])}` | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { EventEmitter } from 'vscode' | ||
import { Cli, CliResult, CliStarted } from '..' | ||
import { Config } from '../../config' | ||
import { Args } from '../constants' | ||
import { getOptions } from '../options' | ||
|
||
export class DvcCli extends Cli { | ||
public autoRegisteredCommands: string[] = [] | ||
|
||
protected readonly config: Config | ||
|
||
constructor( | ||
config: Config, | ||
emitters?: { | ||
processStarted: EventEmitter<CliStarted> | ||
processCompleted: EventEmitter<CliResult> | ||
} | ||
) { | ||
super(emitters) | ||
|
||
this.config = config | ||
} | ||
|
||
public executeDvcProcess(cwd: string, ...args: Args): Promise<string> { | ||
const options = getOptions( | ||
this.config.pythonBinPath, | ||
this.config.getCliPath(), | ||
cwd, | ||
...args | ||
) | ||
return this.executeProcess(options) | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { join } from 'path' | ||
|
||
export const DOT_GIT = '.git' | ||
export const DOT_GIT_HEAD = join(DOT_GIT, 'HEAD') | ||
export const DOT_GIT_INDEX = join(DOT_GIT, 'index') | ||
export const GIT_REFS = join(DOT_GIT, 'refs') | ||
export const GIT_LOGS_REFS = join(DOT_GIT, 'logs', 'refs') | ||
export const HEADS_GIT_REFS = join(GIT_REFS, 'heads') | ||
|
||
export enum Command { | ||
ADD = 'add', | ||
CLEAN = 'clean', | ||
DIFF = 'diff', | ||
LS_FILES = 'ls-files', | ||
PUSH = 'push', | ||
RESET = 'reset', | ||
REV_PARSE = 'rev-parse' | ||
} | ||
|
||
export enum Flag { | ||
DIRECTORIES = '-d', | ||
DIRECTORY = '--directory', | ||
DOT = '.', | ||
EXCLUDE_STANDARD = '--exclude-standard', | ||
FORCE = '-f', | ||
HARD = '--hard', | ||
NAME_ONLY = '--name-only', | ||
NO_EMPTY_DIRECTORY = '--no-empty-directory', | ||
OTHERS = '--others', | ||
QUIET = '-q', | ||
RAW_WITH_NUL = '-z', | ||
SET_UPSTREAM = '--set-upstream', | ||
SHOW_TOPLEVEL = '--show-toplevel' | ||
} | ||
|
||
export enum Commit { | ||
HEAD = 'HEAD' | ||
} | ||
|
||
export const DEFAULT_REMOTE = 'origin' |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { GitCli } from '.' | ||
import { Command, Commit, DEFAULT_REMOTE, Flag } from './constants' | ||
import { getOptions } from './options' | ||
import { typeCheckCommands } from '..' | ||
|
||
export const autoRegisteredCommands = { | ||
GIT_PUSH_BRANCH: 'pushBranch', | ||
GIT_RESET_WORKSPACE: 'resetWorkspace', | ||
GIT_STAGE_ALL: 'stageAll', | ||
GIT_UNSTAGE_ALL: 'reset' | ||
} as const | ||
|
||
export class GitExecutor extends GitCli { | ||
public readonly autoRegisteredCommands = typeCheckCommands( | ||
autoRegisteredCommands, | ||
this | ||
) | ||
|
||
public reset(cwd: string, ...args: (Flag | Commit)[]) { | ||
const options = getOptions(cwd, Command.RESET, ...args) | ||
|
||
return this.executeProcess(options) | ||
} | ||
|
||
public async resetWorkspace(cwd: string) { | ||
await this.reset(cwd, Flag.HARD, Commit.HEAD) | ||
|
||
const options = getOptions( | ||
cwd, | ||
Command.CLEAN, | ||
Flag.FORCE, | ||
Flag.DIRECTORIES, | ||
Flag.QUIET | ||
) | ||
|
||
return this.executeProcess(options) | ||
} | ||
|
||
public async stageAll(cwd: string) { | ||
const gitRoot = await this.getGitRepositoryRoot(cwd) | ||
const options = getOptions(gitRoot, Command.ADD, Flag.DOT) | ||
|
||
return this.executeProcess(options) | ||
} | ||
|
||
public pushBranch(cwd: string, branchName: string) { | ||
const options = getOptions( | ||
cwd, | ||
Command.PUSH, | ||
Flag.SET_UPSTREAM, | ||
DEFAULT_REMOTE, | ||
branchName as Commit | ||
) | ||
|
||
return this.executeProcess(options) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Command, Flag } from './constants' | ||
import { getOptions } from './options' | ||
import { Cli } from '..' | ||
|
||
export class GitCli extends Cli { | ||
public getGitRepositoryRoot(cwd: string) { | ||
const options = getOptions(cwd, Command.REV_PARSE, Flag.SHOW_TOPLEVEL) | ||
|
||
return this.executeProcess(options) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Command, Commit, DEFAULT_REMOTE, Flag } from './constants' | ||
import { ProcessOptions } from '../../processExecution' | ||
|
||
export const getOptions = ( | ||
cwd: string, | ||
...args: (Command | Flag | Commit | typeof DEFAULT_REMOTE)[] | ||
): ProcessOptions => ({ | ||
args, | ||
cwd, | ||
executable: 'git' | ||
}) |
Oops, something went wrong.