git.ts

Git over a SpawnProgram: each function takes the git handle a composition root built with to_git_program, and a cwd handle in its options. The root decides the environment and git is pinned, so nothing here reads PATH or the host's environment.

A read of the local repo captures with out, where a child with no inherited stdio leads its own group and loses the terminal. Anything that may prompt runs with run, which stays in the terminal's foreground group so /dev/tty still reaches ssh, a credential helper, or a gpg pinentry: every fetch, pull, push, ls-remote, checkout, commit, and reset. branch -D can't prompt, and runs with run so its output reaches the user. No function here parses a remote call's output.

A branch or tag name from a caller passes assert_git_ref_name before it reaches git, so it is never a refspec like :deploy or a:b, a pattern like ma*, or a shorthand like @{-1}. It's still a name git resolves, so git_checkout detaches HEAD for a tag or a short hash. An origin goes into argv after --end-of-options, as a ref name does too, so one beginning with - can't be read as an option like --upload-pack. git pull drops that marker before it calls git fetch, so git_pull refuses such an origin itself.

view source

Declarations
#

27 declarations

git_check_clean_workspace
#

git.ts view source

(git: SpawnProgram, options?: SpawnCallOptions | undefined): Promise<string | null> import {git_check_clean_workspace} from '@fuzdev/gro/git.js';

git

options?

optional

returns

Promise<string | null>

an error message if the git workspace has any unstaged or uncommitted changes, or null if it's clean

git_check_fully_staged_workspace
#

git.ts view source

(git: SpawnProgram, options?: SpawnCallOptions | undefined): Promise<string | null> import {git_check_fully_staged_workspace} from '@fuzdev/gro/git.js';

git

options?

optional

returns

Promise<string | null>

an error message if the git workspace has any unstaged changes or untracked files, or null if fully staged

git_check_setting_pull_rebase
#

git.ts view source

(git: SpawnProgram, options?: SpawnCallOptions | undefined): Promise<boolean> import {git_check_setting_pull_rebase} from '@fuzdev/gro/git.js';

Returns the global git config setting for pull.rebase.

git

options?

optional

returns

Promise<boolean>

git_check_workspace
#

git.ts view source

(git: SpawnProgram, options?: SpawnCallOptions | undefined): Promise<GitWorkspaceStatus> import {git_check_workspace} from '@fuzdev/gro/git.js';

Checks the git workspace status using a single git status --porcelain -z call. A directory that isn't a repo reports no changes.

git

options?

optional

returns

Promise<GitWorkspaceStatus>

throws

  • TaskError - if it failed on a repo it couldn't read, which must not read as clean
  • Error - if `git` was stopped rather than exiting, or its output was truncated

git_checkout
#

git.ts view source

(git: SpawnProgram, branch: Flavored<string, "GitBranch">, options?: SpawnCallOptions | undefined): Promise<Flavored<string, "GitBranch"> | null> import {git_checkout} from '@fuzdev/gro/git.js';

Calls git checkout.

git

branch

type Flavored

options?

optional

returns

Promise<Flavored | null>

the previous branch name, if it changed

throws

  • TaskError - if `branch` isn't one plain ref name, or the current branch is unborn
  • Error - if the underlying git command fails

git_current_branch_first_commit_hash
#

git.ts view source

(git: SpawnProgram, options?: SpawnCallOptions | undefined): Promise<string> import {git_current_branch_first_commit_hash} from '@fuzdev/gro/git.js';

Returns the hash of the current branch's first commit.

git

options?

optional

returns

Promise<string>

throws

  • Error - if the underlying git command fails

git_current_branch_name
#

git.ts view source

(git: SpawnProgram, options?: SpawnCallOptions | undefined): Promise<Flavored<string, "GitBranch">> import {git_current_branch_name} from '@fuzdev/gro/git.js';

Returns the current git branch name, or the literal string HEAD on a detached HEAD, which callers comparing it to a branch name read as not that branch.

git

options?

optional

returns

Promise<Flavored>

throws

  • TaskError - on an unborn branch, one with no commits yet
  • Error - if `git` prints no name, as outside a repo, or was stopped or truncated

git_current_commit_hash
#

git.ts view source

(git: SpawnProgram, options?: SpawnCallOptions | undefined): Promise<string | null> import {git_current_commit_hash} from '@fuzdev/gro/git.js';

Returns the hash of the commit HEAD points at, or null when there is none: an unborn branch, or a directory that isn't a repo. A detached HEAD reads as its commit.

git

options?

optional

returns

Promise<string | null>

throws

  • Error - if `git` was stopped rather than exiting, or its output was truncated

git_delete_local_branch
#

git.ts view source

(git: SpawnProgram, branch: Flavored<string, "GitBranch">, options?: SpawnCallOptions | undefined): Promise<void> import {git_delete_local_branch} from '@fuzdev/gro/git.js';

Deletes a branch locally.

git

branch

type Flavored

options?

optional

returns

Promise<void>

throws

  • TaskError - if `branch` isn't one plain ref name
  • Error - if the underlying git command fails

git_fetch
#

git.ts view source

(git: SpawnProgram, origin?: Flavored<string, "GitOrigin">, branch?: Flavored<string, "GitBranch"> | undefined, options?: SpawnCallOptions | undefined): Promise<...> import {git_fetch} from '@fuzdev/gro/git.js';

Calls git fetch. Contacts the remote, so it may prompt.

git

origin?

type Flavored
optional default 'origin'

branch?

type Flavored
optional

options?

optional

returns

Promise<void>

throws

  • TaskError - if `branch` isn't one plain ref name
  • Error - if the underlying git command fails

git_fetch_force
#

git.ts view source

(git: SpawnProgram, origin: Flavored<string, "GitOrigin">, branch: Flavored<string, "GitBranch">, options?: SpawnCallOptions | undefined): Promise<...> import {git_fetch_force} from '@fuzdev/gro/git.js';

Sets the local branch to the remote's, even when that drops commits only the local one has, as the refspec +branch:branch. Contacts the remote, so it may prompt.

git

origin

type Flavored

branch

type Flavored

options?

optional

returns

Promise<void>

throws

  • TaskError - if `branch` isn't one plain ref name
  • Error - if the underlying git command fails

git_head_subject
#

git.ts view source

(git: SpawnProgram, options?: SpawnCallOptions | undefined): Promise<string | null> import {git_head_subject} from '@fuzdev/gro/git.js';

git

options?

optional

returns

Promise<string | null>

the subject line of the commit HEAD points at, or null when there is none

throws

  • Error - if `git` was stopped rather than exiting, or its output was truncated

git_local_branch_exists
#

git.ts view source

(git: SpawnProgram, branch: Flavored<string, "GitBranch">, options?: SpawnCallOptions | undefined): Promise<boolean> import {git_local_branch_exists} from '@fuzdev/gro/git.js';

Checks if a local git branch exists.

git

branch

type Flavored

options?

optional

returns

Promise<boolean>

throws

  • TaskError - if `branch` isn't one plain ref name

GIT_LOCAL_PASS_ENV
#

git.ts view source

readonly string[] import {GIT_LOCAL_PASS_ENV} from '@fuzdev/gro/git.js';

The host variables a git that only reads the local repo needs beyond the base set. GIT_* carries what a repo reached through a hook or a worktree is (GIT_DIR, GIT_INDEX_FILE) and XDG_CONFIG_HOME where the user's config is. SUDO_UID is the owner git's safe.directory check accepts when the effective user is root, so under sudo the invoking user's repo isn't refused as dubiously owned. A git that commits or reaches a remote gets more, decided at its root.

git_parse_workspace_status
#

git.ts view source

(stdout: string | null): GitWorkspaceStatus import {git_parse_workspace_status} from '@fuzdev/gro/git.js';

Parses the output of git status --porcelain -z (v1 format) into a status object.

Format: XY path\0 where X is the staged status (index), Y the unstaged status (work tree), and path is unescaped, with renames and copies as XY new\0old\0. Submodule-specific status codes are interpreted as changes.

stdout

the raw output from git status --porcelain -z

type string | null

returns

GitWorkspaceStatus

git_pull
#

git.ts view source

(git: SpawnProgram, origin?: Flavored<string, "GitOrigin">, branch?: Flavored<string, "GitBranch"> | undefined, options?: SpawnCallOptions | undefined): Promise<...> import {git_pull} from '@fuzdev/gro/git.js';

Calls git pull. Contacts the remote, so it may prompt.

git

origin?

type Flavored
optional default 'origin'

branch?

type Flavored
optional

options?

optional

returns

Promise<void>

throws

  • TaskError - if `branch` isn't one plain ref name
  • Error - if the underlying git command fails, or `origin` begins with `-`

git_push
#

git.ts view source

(git: SpawnProgram, origin: Flavored<string, "GitOrigin">, branch?: Flavored<string, "GitBranch"> | undefined, options?: SpawnCallOptions | undefined, set_upstream?: boolean): Promise<...> import {git_push} from '@fuzdev/gro/git.js';

Calls git push. Contacts the remote, so it may prompt.

git

origin

type Flavored

branch?

type Flavored
optional

options?

optional

set_upstream?

type boolean
optional default false

returns

Promise<void>

throws

  • TaskError - if the branch isn't one plain ref name, as on a detached `HEAD`
  • Error - if the underlying git command fails

git_push_to_create
#

git.ts view source

(git: SpawnProgram, origin?: Flavored<string, "GitOrigin">, branch?: Flavored<string, "GitBranch"> | undefined, options?: SpawnCallOptions | undefined): Promise<...> import {git_push_to_create} from '@fuzdev/gro/git.js';

Calls git push, setting upstream if the remote branch does not yet exist.

git

origin?

type Flavored
optional default 'origin'

branch?

type Flavored
optional

options?

optional

returns

Promise<void>

throws

  • TaskError - if the branch isn't one plain ref name, as on a detached `HEAD`
  • Error - if the underlying git command fails

git_remote_branch_exists
#

git.ts view source

(git: SpawnProgram, origin?: Flavored<string, "GitOrigin">, branch?: Flavored<string, "GitBranch"> | undefined, options?: SpawnCallOptions | undefined): Promise<...> import {git_remote_branch_exists} from '@fuzdev/gro/git.js';

Checks if a remote git branch exists. Contacts the remote, so it may prompt.

git

origin?

type Flavored
optional default 'origin'

branch?

type Flavored
optional

options?

optional

returns

Promise<boolean>

throws

  • TaskError - if the branch isn't one plain ref name, as on a detached `HEAD`
  • Error - if `git ls-remote` fails for a reason other than the branch not existing

git_tag_exists
#

git.ts view source

(git: SpawnProgram, tag: string, options?: SpawnCallOptions | undefined): Promise<boolean> import {git_tag_exists} from '@fuzdev/gro/git.js';

git

tag

type string

options?

optional

returns

Promise<boolean>

true if the local tag exists

throws

  • TaskError - if `tag` isn't one plain ref name
  • Error - if `git` was stopped rather than exiting

git_workspace_is_clean
#

git.ts view source

(status: GitWorkspaceStatus): boolean import {git_workspace_is_clean} from '@fuzdev/gro/git.js';

status

returns

boolean

true if the workspace has no changes at all

git_workspace_is_fully_staged
#

git.ts view source

(status: GitWorkspaceStatus): boolean import {git_workspace_is_fully_staged} from '@fuzdev/gro/git.js';

status

returns

boolean

true if the workspace has no unstaged changes and no untracked files (staged changes are OK)

git_workspace_status_message
#

git.ts view source

(status: GitWorkspaceStatus): string import {git_workspace_status_message} from '@fuzdev/gro/git.js';

Converts a workspace status to a human-readable message.

status

returns

string

GitBranch
#

git.ts view source

value + type

Flavored<string, "GitBranch">

type Flavored

import {GitBranch} from '@fuzdev/gro/git.js';

A git branch's name.

external types

s.Infer<typeof GitBranch>

GitOrigin
#

git.ts view source

value + type

Flavored<string, "GitOrigin">

type Flavored

import {GitOrigin} from '@fuzdev/gro/git.js';

A git remote's name, such as origin.

external types

s.Infer<typeof GitOrigin>

GitWorkspaceStatus
#

git.ts view source

GitWorkspaceStatus import type {GitWorkspaceStatus} from '@fuzdev/gro/git.js';

Git workspace status flags indicating which types of changes are present.

unstaged_changes

type boolean

staged_changes

type boolean

untracked_files

type boolean

to_git_program
#

git.ts view source

(spawn: Spawn, options?: SpawnProgramOptions | undefined): SpawnProgram import {to_git_program} from '@fuzdev/gro/git.js';

Finds git for a composition root, on PATH only, so a node_modules/.bin/git can't shadow it.

spawn

the root's spawn host module

type Spawn

options?

the program's environment, fixed when its handle is made

optional

returns

SpawnProgram

throws

  • TaskError - if `git` isn't on `PATH`

Depends on
#

Imported by
#