Module: Git::Repository::Stashing

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

Overview

Facade methods for stash operations

Included by Git::Repository.

Instance Method Summary collapse

Instance Method Details

#stash_apply(id = nil) ⇒ String

Apply a stash to the working directory

Applies the changes recorded in a stash entry to the working directory without removing the entry from the stash list. Unlike git stash pop, the stash entry is preserved after applying.

Examples:

Apply the most recent stash

repo.stash_apply #=> "HEAD is now at abc1234 Initial commit"

Apply a specific stash entry by reference

repo.stash_apply('stash@{1}') #=> "HEAD is now at abc1234 Initial commit"

Parameters:

  • id (String, Integer, nil) (defaults to: nil)

    the stash identifier (e.g., 'stash@{0}', 0) or nil to apply the most recent stash entry. When an Integer is given it is passed directly to git as stash@{N}, where 0 is the most recent stash — the opposite order from #stashes_all's sequential indices, where 0 is the oldest stash.

Returns:

  • (String)

    the output from the git stash apply command

Raises:

See Also:



119
120
121
# File 'lib/git/repository/stashing.rb', line 119

def stash_apply(id = nil)
  Git::Commands::Stash::Apply.new(@execution_context).call(id).stdout
end

#stash_clearString

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:



138
139
140
# File 'lib/git/repository/stashing.rb', line 138

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

#stash_listString

Deprecated.

Use #stashes_all instead.

Returns stash entries as a formatted string matching git stash list output

Examples:

List stashes as a formatted string

repo.stash_list #=> "stash@{0}: On main: WIP\nstash@{1}: On feature: Fix bug"

Returns:

  • (String)

    newline-joined "stash@{n}: <full message>" entries, or an empty string when there are no stashes; the format matches git stash list output

Raises:

See Also:



66
67
68
69
70
71
72
73
74
# File 'lib/git/repository/stashing.rb', line 66

def stash_list
  Git::Deprecation.warn(
    'Git::Repository#stash_list is deprecated and will be removed in a future version. ' \
    'Use Git::Repository#stashes_all instead.'
  )
  result = Git::Commands::Stash::List.new(@execution_context).call
  stashes = Git::Parsers::Stash.parse_list(result.stdout)
  stashes.map { |info| "#{info.name}: #{info.message}" }.join("\n")
end

#stash_save(message) ⇒ Boolean

Save the current working directory and index state to a new stash

Examples:

Save current changes

repo.stash_save('WIP: feature work')

Parameters:

  • message (String)

    the stash message

Returns:

  • (Boolean)

    true if changes were stashed, false if there were no local changes to save

Raises:

See Also:



90
91
92
93
# File 'lib/git/repository/stashing.rb', line 90

def stash_save(message) # rubocop:disable Naming/PredicateMethod
  result = Git::Commands::Stash::Push.new(@execution_context).call(message: message)
  !result.stdout.include?('No local changes to save')
end

#stashes_allArray<Array(Integer, String)>

Note:

The sequential index returned here is not the same as git's stash@{N} reference used by #stash_apply. In git, stash@{0} is the most recent stash, while index 0 here is the oldest. To apply a specific stash from this list, convert the entry's position to a git reference: 'stash@{%d}' % (total - 1 - index), or pass the string reference directly to #stash_apply.

Returns all stash entries as an array of index and message pairs

Lists all stash entries in the repository ordered from oldest to newest. The index is a sequential number starting from 0 for the oldest stash. The message is the stash description with the leading branch prefix (e.g. "On main:" or "WIP on main:") stripped.

Examples:

List all stashes (oldest first)

repo.stashes_all #=> [[0, "Fix bug"], [1, "Add feature"]]

Returns:

  • (Array<Array(Integer, String)>)

    array of [index, message] pairs where index is the sequential position (0 is oldest) and message is the stash description with the branch prefix stripped

Raises:

See Also:



40
41
42
43
44
45
46
47
# File 'lib/git/repository/stashing.rb', line 40

def stashes_all
  result = Git::Commands::Stash::List.new(@execution_context).call
  stashes = Git::Parsers::Stash.parse_list(result.stdout)
  stashes.reverse.each_with_index.map do |info, i|
    message = info.message.sub(/^(?:WIP on|On)\s+[^:]+:\s*/, '')
    [i, message]
  end
end