Skip to content

oneRepo API: git

Special handlers for managing complex queries and manipulation of the git repository’s state.

new StagingWorkflow(options): StagingWorkflow

Parameters:

ParameterTypeDescription
options{ graph: Graph; logger: Logger; }-
options.graphGraphThe repository Graph
options.loggerLoggerLogger instance to use for all actions

Returns: StagingWorkflow
Defined in: modules/git/src/workflow.ts

restoreUnstaged(): Promise<void>

Restores the unstaged changes previously backed up by `saveUnstaged()`.

This command will go through a series of attempts to ressurect upon failure, eventually throwing an error if unstaged changes cannot be reapplied.

Returns: Promise<void>
Defined in: modules/git/src/workflow.ts

saveUnstaged(): Promise<void>

Backup any unstaged changes, whether that’s full files or parts of files. This will result in the git status only including what was already in the stage. All other changes will be backed up to:

  1. A patch file in the .git/ directory
  2. A git stash

To restore the unstaged changes, call `restoreUnstaged()`.

Returns: Promise<void>
Defined in: modules/git/src/workflow.ts

type ModifiedBaseOptions<ByStatus>: {
allStatus: boolean;
byStatus: ByStatus;
};
Type ParameterDefault type
ByStatus extends booleanfalse
optional allStatus: boolean;

By default, this function will not return deleted and unmerged files unless either allStatus or byStatus is set to true

optional byStatus: ByStatus;

Return modified files categorized by the type of modification (added, deleted, modified, etc)

Defined in: modules/git/src/index.ts


type ModifiedByStatus: {
added: string[];
copied: string[];
deleted: string[];
fileTypeChanged: string[];
modified: string[];
renamed: string[];
unknown: string[];
unmerged: string[];
};

This type defines the different statuses of files when running a git-diff. More information around the file statuses can be found in the official git documentation for git-diff.

added: string[];

Git status A: addition of a file

copied: string[];

Git status C: copy of a file into a new one

deleted: string[];

Git status D: deletion of a file

fileTypeChanged: string[];

Git status T: change in the type of the file (regular file, symbolic link or submodule)

modified: string[];

Git status M: modification of the contents or mode of a file

renamed: string[];

Git status R: renaming of a file

unknown: string[];

Git status X: addition of a file

unmerged: string[];

Git status U: “unknown” change type (most probably a bug, please report it)

Defined in: modules/git/src/index.ts


type ModifiedFromThrough<ByStatus>: ModifiedBaseOptions<ByStatus> & {
from: string;
staged: false;
through: string;
};
optional from: string;

Git ref for start (exclusive) to get list of modified files

optional staged: false;

Cannot include staged files when using from/through refs.

optional through: string;

Git ref for end (inclusive) to get list of modified files

Type Parameter
ByStatus extends boolean

Defined in: modules/git/src/index.ts


type ModifiedStaged<ByStatus>: ModifiedBaseOptions<ByStatus> & {
from: never;
staged: true;
through: never;
};
optional from: never;

Disallowed when staged: true

staged: true;

Get staged modified files only

optional through: never;

Disallowed when staged: true

Type Parameter
ByStatus extends boolean

Defined in: modules/git/src/index.ts


type Options: {
step: LogStep;
};

Generic options passed to all Git operations.

optional step: LogStep;

Avoid creating a new step in output for each function. Pass a Logger Step to pipe all logs and output to that instead.

Defined in: modules/git/src/index.ts


type UpdateIndexOptions: Options & {
immediately: boolean;
};
optional immediately: boolean;

Set whether to immediately add to the git index or defer until process shutdown

Default: false
Defined in: modules/git/src/index.ts

function flushUpdateIndex(options?): Promise<void>;

Write all pending files added using `updateIndex()` to the git index.

await git.flushUpdateIndex();

Parameters:

ParameterType
options?Options

Returns: Promise<void>
Defined in: modules/git/src/index.ts


function getBranch(options?): Promise<string>;

Get the name of the current branch. Equivalent to git rev-parse --abbrev-ref HEAD.

const currentBranch = await git.getBranch();

Parameters:

ParameterType
options?Options

Returns: Promise<string>
Defined in: modules/git/src/index.ts


function getCurrentSha(options?): Promise<string>;

Get the current sha ref. This is equivalent to git rev-parse HEAD.

const sha = await git.getCurrentSha();

Parameters:

ParameterType
options?Options

Returns: Promise<string>
Defined in: modules/git/src/index.ts


function getMergeBase(options?): Promise<string>;

Determine the git ref for merging the current working branch, sha, or ref, whichever that is. This function does a bunch of internal checks to determine the where the most likely point of forking happened.

const mergeBase = await getMergeBase();

Parameters:

ParameterType
options?Options

Returns: Promise<string>
Defined in: modules/git/src/index.ts


function getModifiedFiles<ByStatus>(modified?, options?): Promise<ByStatus extends true ? ModifiedByStatus : string[]>;

Get a map of the currently modified files based on their status. If from and through are not provided, this will use merge-base determination to get the changes to the working tree using git diff and git diff-tree.

By default, this function will not return deleted and unmerged files. If you wish to include files with those statuses, set the option allStatus: true or get a map of all files by status using byStatus: true.

const changesSinceMergeBase = await git.getModifiedFiles();
const betweenRefs = await git.getModifiedFiles({ from: 'v1.2.3', through: 'v2.0.0' });

Get modified files categorized by modification type:

const allChanges = await git.getModifiedFiles({ byStatus: true });

Will result in allChanges equal to an object containing arrays of strings:

{
added: [/* ... */],
copied: [/* ... */],
modified: [/* ... */],
deleted: [/* ... */],
renamed: [/* ... */],
fileTypeChanged: [/* ... */],
unmerged: [/* ... */],
unknown: [/* ... */],
}
Type ParameterDefault type
ByStatus extends booleanfalse

Parameters:

ParameterType
modified?ModifiedStaged<ByStatus> | ModifiedFromThrough<ByStatus>
options?Options

Returns: Promise<ByStatus extends true ? ModifiedByStatus : string[]>
Defined in: modules/git/src/index.ts


function isClean(options?): Promise<boolean>;

Check if the current git working state is clean.

const isClean = await git.isClean();
if (!isClean) {
// There are local modifications that have not yet been committed.
}

Parameters:

ParameterType
options?Options

Returns: Promise<boolean>
Defined in: modules/git/src/index.ts


function updateIndex(paths, options?): Promise<string | undefined>;

Add filepaths to the git index. Equivalent to git add [...files]. By default, this method will track the files that need to be added to the git index. It will only add files immediately if given the immediately option.

Use `flushUpdateIndex()` to write all tracked files the git index. This method is automatically called during the oneRepo command shutdown process, so you may not ever need to call this.

It is best to avoid immediately adding items to the git index to avoid race conditions which can drop git into a bad state, requiring users to manually delete their .git/index.lock file before continuing.

await git.updateIndex(['tacos.ts']);

Parameters:

ParameterType
pathsstring | string[]
options?UpdateIndexOptions

Returns: Promise<string | undefined>
Defined in: modules/git/src/index.ts