Module: Git::Repository::Stashing Private

Included in:
Git::Repository
Defined in:
lib/git/repository/stashing.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Facade methods for stash operations

Each method maps onto a git stash subcommand. Methods that identify or create a stash entry (#stash_list, #stash_push, #stash_store) return StashInfo values; methods that only change or display the stash return git's stdout. Every method that takes a stash (#stash_apply, #stash_pop, #stash_drop, #stash_show, #stash_branch) accepts a StashInfo, a stash@{N} name, an Integer index (0 is the most recent entry), or nil for the most recent entry. Methods that take options accept them as a trailing positional Hash, so repo.stash_apply(index: true) and repo.stash_apply(nil, opts) with a stored opts Hash both work.

Included by Git::Repository.

Instance Method Summary collapse

Instance Method Details

#stash_apply(stash = nil, opts = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

If git exits non-zero, nothing is unwound: whatever git applied stays in the working tree and index, including untracked files restored from the entry even when git refused to touch the tracked files, and on a conflict the conflicted paths are left for the caller to resolve.

Apply a stash entry to the working tree, keeping it in the stash list

Examples:

Apply the most recent entry

repo.stash_apply #=> "On branch main\nChanges not staged for commit:..."

Apply an entry from #stash_list

repo.stash_apply(repo.stash_list.last)

Apply an entry by name and restore the index too

repo.stash_apply('stash@{1}', index: true)

Parameters:

  • stash (Git::StashInfo, String, Integer, nil) (defaults to: nil) —

    the entry to apply: a StashInfo, a stash@{N} name, an Integer N (0 is the most recent entry), or nil for the most recent entry

  • opts (Hash) (defaults to: {}) —

    options for the apply

Options Hash (opts):

  • :index (Boolean, nil) — default: nil —

    restore the index state as well as the working tree

  • :quiet (Boolean, nil) — default: nil —

    suppress informational messages (alias: :q)

  • :q (Boolean, nil) — default: nil —

    alias for :quiet

Returns:

  • (String) —

    git's stdout from the apply

Raises:

  • (ArgumentError) —

    if unsupported options are provided

  • (Git::FailedError) —

    if git exits with a non-zero exit status

See Also:



191
192
193
194
195
# File 'lib/git/repository/stashing.rb', line 191

def stash_apply(stash = nil, opts = {})
  stash, opts = Private.split_stash_and_opts(stash, opts)
  SharedPrivate.assert_valid_opts!(STASH_APPLY_ALLOWED_OPTS, **opts)
  Git::Commands::Stash::Apply.new(@execution_context).call(stash, **opts).stdout
end

#stash_branch(branch_name, stash = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

If applying the entry fails, branch_name has already been created and checked out, so the repository is left on it with whatever git could apply in the working tree, and the entry stays in the stash list.

Create and check out a branch from the commit a stash entry was based on

Applies the entry on the new branch and, when that succeeds, drops the entry from the stash list.

Examples:

Branch from the most recent entry

repo.stash_branch('feature') #=> "Switched to a new branch 'feature'\n..."

Branch from an entry from #stash_list

repo.stash_branch('feature', repo.stash_list.last)

Parameters:

  • branch_name (String) —

    the name of the branch to create

  • stash (Git::StashInfo, String, Integer, nil) (defaults to: nil) —

    the entry to branch from: a StashInfo, a stash@{N} name, an Integer N (0 is the most recent entry), or nil for the most recent entry

Returns:

  • (String) —

    git's stdout from the branch command

Raises:

See Also:



388
389
390
# File 'lib/git/repository/stashing.rb', line 388

def stash_branch(branch_name, stash = nil)
  Git::Commands::Stash::Branch.new(@execution_context).call(branch_name, stash).stdout
end

#stash_clear ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Remove all stash entries

Removes all entries from the stash list. Use with caution as this operation cannot be undone.

Examples:

Clear all stashes

repo.stash_clear #=> ""

Returns:

  • (String) —

    the output from the git stash clear command (typically empty)

Raises:

See Also:



507
508
509
# File 'lib/git/repository/stashing.rb', line 507

def stash_clear
  Git::Commands::Stash::Clear.new(@execution_context).call.stdout
end

#stash_create(message = nil) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a stash commit without adding it to the stash list

The working tree and index are left unchanged. Pass the returned object id to #stash_store to add it to the stash list later.

Examples:

Create a stash commit

repo.stash_create('WIP') #=> "3f8b2d9c..." (the full object id)

Nothing to stash

repo.stash_create #=> nil

Parameters:

  • message (String, nil) (defaults to: nil) —

    the message for the stash commit; nil for git's default message

Returns:

  • (String, nil) —

    the object id of the stash commit, or nil when there were no local changes

Raises:

See Also:



413
414
415
416
# File 'lib/git/repository/stashing.rb', line 413

def stash_create(message = nil)
  oid = Git::Commands::Stash::Create.new(@execution_context).call(message).stdout.strip
  oid.empty? ? nil : oid
end

#stash_drop(stash = nil, opts = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Remove a single stash entry from the stash list

Examples:

Drop the most recent entry

repo.stash_drop #=> "Dropped refs/stash@{0} (abc1234...)"

Drop an entry from #stash_list

repo.stash_drop(repo.stash_list.last)

Parameters:

  • stash (Git::StashInfo, String, Integer, nil) (defaults to: nil) —

    the entry to drop: a StashInfo, a stash@{N} name, an Integer N (0 is the most recent entry), or nil for the most recent entry

  • opts (Hash) (defaults to: {}) —

    options for the drop

Options Hash (opts):

  • :quiet (Boolean, nil) — default: nil —

    suppress informational messages (alias: :q)

  • :q (Boolean, nil) — default: nil —

    alias for :quiet

Returns:

  • (String) —

    git's stdout from the drop

Raises:

  • (ArgumentError) —

    if unsupported options are provided

  • (Git::FailedError) —

    if git exits with a non-zero exit status

See Also:



272
273
274
275
276
# File 'lib/git/repository/stashing.rb', line 272

def stash_drop(stash = nil, opts = {})
  stash, opts = Private.split_stash_and_opts(stash, opts)
  SharedPrivate.assert_valid_opts!(STASH_DROP_ALLOWED_OPTS, **opts)
  Git::Commands::Stash::Drop.new(@execution_context).call(stash, **opts).stdout
end

#stash_list ⇒ Array<Git::StashInfo> Also known as: stash_infos

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns every stash entry as a StashInfo, newest first

The order and indices match git stash list: the first element is stash@{0}, the most recent entry.

Examples:

List stash entries (newest first)

repo.stash_list.map(&:name) #=> ["stash@{0}", "stash@{1}"]

Apply the oldest entry

repo.stash_apply(repo.stash_list.last)

Returns:

  • (Array<Git::StashInfo>) —

    the stash entries, newest first; empty when there are none

Raises:

See Also:



46
47
48
49
# File 'lib/git/repository/stashing.rb', line 46

def stash_list
  result = Git::Commands::Stash::List.new(@execution_context).call
  Git::Parsers::Stash.parse_list(result.stdout)
end

#stash_pop(stash = nil, opts = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

If git exits non-zero, nothing is unwound, as described at #stash_apply, and the entry is kept in the stash list rather than dropped.

Apply a stash entry to the working tree and remove it from the stash list

Examples:

Pop the most recent entry

repo.stash_pop #=> "On branch main\n...Dropped refs/stash@{0} (abc1234...)"

Pop an entry from #stash_list

repo.stash_pop(repo.stash_list.last)

Parameters:

  • stash (Git::StashInfo, String, Integer, nil) (defaults to: nil) —

    the entry to pop: a StashInfo, a stash@{N} name, an Integer N (0 is the most recent entry), or nil for the most recent entry

  • opts (Hash) (defaults to: {}) —

    options for the pop

Options Hash (opts):

  • :index (Boolean, nil) — default: nil —

    restore the index state as well as the working tree

  • :quiet (Boolean, nil) — default: nil —

    suppress informational messages (alias: :q)

  • :q (Boolean, nil) — default: nil —

    alias for :quiet

Returns:

  • (String) —

    git's stdout from the pop

Raises:

  • (ArgumentError) —

    if unsupported options are provided

  • (Git::FailedError) —

    if git exits with a non-zero exit status

See Also:



235
236
237
238
239
# File 'lib/git/repository/stashing.rb', line 235

def stash_pop(stash = nil, opts = {})
  stash, opts = Private.split_stash_and_opts(stash, opts)
  SharedPrivate.assert_valid_opts!(STASH_POP_ALLOWED_OPTS, **opts)
  Git::Commands::Stash::Pop.new(@execution_context).call(stash, **opts).stdout
end

#stash_push(*pathspec, options = {}) ⇒ Git::StashInfo?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Save the working tree and index state to a new stash entry

The stash list is read before and after the push and the entry counts are compared, so the return value is nil whenever git created no entry. This holds with quiet: true, which suppresses git's "No local changes to save" message.

Returns the new entry, or nil when there were no local changes to save.

Examples:

Stash all changes with a message

info = repo.stash_push(message: 'WIP: feature work')
info.name    #=> "stash@{0}"
info.message #=> "On main: WIP: feature work"

Stash only specific paths

repo.stash_push('src/a.rb', 'src/b.rb', message: 'partial work')

Nothing to stash

repo.stash_push #=> nil

Parameters:

  • pathspec (Array<String>) —

    paths that limit what gets stashed; when empty, all changes are stashed

  • options (Hash) (defaults to: {}) —

    options for the push

Options Hash (options):

  • :patch (Boolean, nil) — default: nil —

    interactively select hunks to stash (alias: :p)

  • :p (Boolean, nil) — default: nil —

    alias for :patch

  • :staged (Boolean, nil) — default: nil —

    stash only the staged changes (alias: :S)

  • :S (Boolean, nil) — default: nil —

    alias for :staged

  • :keep_index (Boolean, nil) — default: nil —

    keep the staged changes in the index (alias: :k)

  • :k (Boolean, nil) — default: nil —

    alias for :keep_index

  • :no_keep_index (Boolean, nil) — default: nil —

    do not keep the staged changes in the index

  • :quiet (Boolean, nil) — default: nil —

    suppress informational messages (alias: :q)

  • :q (Boolean, nil) — default: nil —

    alias for :quiet

  • :include_untracked (Boolean, nil) — default: nil —

    include untracked files in the stash (alias: :u)

  • :u (Boolean, nil) — default: nil —

    alias for :include_untracked

  • :all (Boolean, nil) — default: nil —

    include untracked and ignored files in the stash (alias: :a)

  • :a (Boolean, nil) — default: nil —

    alias for :all

  • :message (String) — default: nil —

    the stash message (alias: :m)

  • :m (String) — default: nil —

    alias for :message

  • :pathspec_from_file (String) — default: nil —

    read pathspecs from the given file; pass - to read from standard input

  • :pathspec_file_nul (Boolean, nil) — default: nil —

    when used with :pathspec_from_file, pathspecs are NUL-separated

Returns:

  • (Git::StashInfo, nil) —

    the new entry, or nil when there were no local changes to save

Raises:

  • (ArgumentError) —

    if unsupported options are provided

  • (Git::FailedError) —

    if git exits with a non-zero exit status

See Also:



140
141
142
143
144
145
146
147
# File 'lib/git/repository/stashing.rb', line 140

def stash_push(*pathspec)
  pathspec, opts = Private.split_pathspec_and_opts(pathspec)
  SharedPrivate.assert_valid_opts!(STASH_PUSH_ALLOWED_OPTS, **opts)
  previous_count = stash_list.size
  Git::Commands::Stash::Push.new(@execution_context).call(*pathspec, **opts)
  entries = stash_list
  entries.first if entries.size > previous_count
end

#stash_show(stash = nil, opts = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Show the changes recorded in a stash entry as a diff

Without options, git prints a diffstat. The output is returned as git prints it.

Examples:

Show the diffstat of the most recent entry

repo.stash_show #=> " file.txt | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)"

Show the full patch of an entry from #stash_list

repo.stash_show(repo.stash_list.last, patch: true)

Parameters:

  • stash (Git::StashInfo, String, Integer, nil) (defaults to: nil) —

    the entry to show: a StashInfo, a stash@{N} name, an Integer N (0 is the most recent entry), or nil for the most recent entry

  • opts (Hash) (defaults to: {}) —

    options for the show

Options Hash (opts):

  • :patch (Boolean, nil) — default: nil —

    show the diff as a patch

  • :numstat (Boolean, nil) — default: nil —

    show per-file insertion and deletion counts

  • :raw (Boolean, nil) — default: nil —

    show per-file mode, object id, and status metadata

  • :shortstat (Boolean, nil) — default: nil —

    show only the summary line

  • :unified (Integer, String) — default: nil —

    number of context lines in the patch (alias: :U)

  • :U (Integer, String) — default: nil —

    alias for :unified

  • :include_untracked (Boolean, nil) — default: nil —

    include the untracked files recorded in the entry (alias: :u)

  • :u (Boolean, nil) — default: nil —

    alias for :include_untracked

  • :no_include_untracked (Boolean, nil) — default: nil —

    exclude the untracked files recorded in the entry

  • :only_untracked (Boolean, nil) — default: nil —

    show only the untracked files recorded in the entry

  • :find_renames (Boolean, Integer, nil) — default: nil —

    detect renames, optionally with a similarity threshold (alias: :M)

  • :M (Boolean, Integer, nil) — default: nil —

    alias for :find_renames

  • :find_copies (Boolean, Integer, nil) — default: nil —

    detect copies as well as renames, optionally with a similarity threshold (alias: :C)

  • :C (Boolean, Integer, nil) — default: nil —

    alias for :find_copies

  • :find_copies_harder (Boolean, nil) — default: nil —

    inspect unmodified files as copy sources

  • :inter_hunk_context (Integer, String) — default: nil —

    number of context lines between hunks before they are merged

  • :dirstat (Boolean, String, nil) — default: nil —

    include directory statistics, optionally with parameters

Returns:

  • (String) —

    git's stdout from the show

Raises:

  • (ArgumentError) —

    if unsupported options are provided

  • (Git::FailedError) —

    if git exits with a non-zero exit status

See Also:



355
356
357
358
359
# File 'lib/git/repository/stashing.rb', line 355

def stash_show(stash = nil, opts = {})
  stash, opts = Private.split_stash_and_opts(stash, opts)
  SharedPrivate.assert_valid_opts!(STASH_SHOW_ALLOWED_OPTS, **opts)
  Git::Commands::Stash::Show.new(@execution_context).call(stash, **opts).stdout
end

#stash_store(commit, opts = {}) ⇒ Git::StashInfo

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Storing the commit that is already at the top of the stash list adds no entry: git stash store writes nothing to the refs/stash reflog, any :message given is discarded, and the existing top entry is returned.

Add a stash commit created by #stash_create to the stash list

commit is resolved to the full object id of the commit with git rev-parse and that id is what is stored, so an annotated tag stores the tagged commit rather than the tag object (which git stash store accepts but git stash list cannot show). Because the id is what git sees, a failure from git stash store names the resolved id rather than the revision as it was given. The stash list is read after the store and the entry whose object id equals that id is returned.

Examples:

Store a stash commit

oid = repo.stash_create
info = repo.stash_store(oid, message: 'saved for later')
info.name    #=> "stash@{0}"
info.message #=> "saved for later"

Store a stash commit named by a ref

repo.stash_store('refs/tmp/wip').oid #=> "9b9b31e704c0b85ffdd8d2af2ded85170a5af87d"

Parameters:

  • commit (String, #to_s) —

    the stash commit to store, as any revision that resolves to it

    Full or abbreviated object ids, branch, tag, and other ref names, stash@{N}, and :/<text> searches all resolve with git rev-parse, and a StashInfo resolves through its #to_s. A :/<text> search finds only commits reachable from a ref, so it cannot name a commit from #stash_create until that commit is stored or a ref points at it.

  • opts (Hash) (defaults to: {}) —

    options for the store

Options Hash (opts):

  • :message (String) — default: nil —

    the message for the stash entry (alias: :m)

  • :m (String) — default: nil —

    alias for :message

  • :quiet (Boolean, nil) — default: nil —

    suppress informational messages (alias: :q)

  • :q (Boolean, nil) — default: nil —

    alias for :quiet

Returns:

Raises:

  • (ArgumentError) —

    if unsupported options are provided

  • (ArgumentError) —

    if commit is nil or starts with a hyphen or a caret

  • (Git::FailedError) —

    if git exits with a non-zero exit status

  • (Git::UnexpectedResultError) —

    if the stash listing cannot be parsed or does not contain the stored commit

    The entry is missing when another process dropped or cleared it between the store and the lookup.

See Also:



484
485
486
487
488
489
490
# File 'lib/git/repository/stashing.rb', line 484

def stash_store(commit, opts = {})
  SharedPrivate.assert_valid_opts!(STASH_STORE_ALLOWED_OPTS, **opts)
  oid = Private.resolve_commit_oid(@execution_context, commit)
  Git::Commands::Stash::Store.new(@execution_context).call(oid, **opts)
  stash_list.find { |entry| entry.oid == oid } ||
    raise(Git::UnexpectedResultError, "stash was stored but not found in the stash list: #{oid}")
end