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

Returns:

  • (String)

    the output from the git stash apply command

Raises:

See Also:



83
84
85
# File 'lib/git/repository/stashing.rb', line 83

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:



102
103
104
# File 'lib/git/repository/stashing.rb', line 102

def stash_clear
  Git::Commands::Stash::Clear.new(@execution_context).call.stdout
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:



57
58
59
60
# File 'lib/git/repository/stashing.rb', line 57

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)>

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:



33
34
35
36
37
38
39
40
41
# File 'lib/git/repository/stashing.rb', line 33

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|
    match_data = info.message.match(/^[^:]+:(.*)$/)
    message = match_data ? match_data[1].strip : info.message
    [i, message]
  end
end