Class: Git::Stashes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/git/stashes.rb

Overview

Collection of stash entries for a Git repository

Examples:

Iterate over stash entries

git.stashes.each { |s| puts s.message }

Check and apply a stash

git.stashes.size   #=> 2
git.stashes.apply

Instance Method Summary collapse

Constructor Details

#initialize(base)

Initialize the stashes collection

Loads all existing stash entries from the repository at construction time.

Examples:

Load stashes for a repository

stashes = Git::Stashes.new(repo)
stashes.size  #=> 2

Parameters:

Raises:



34
35
36
37
38
39
40
41
42
# File 'lib/git/stashes.rb', line 34

def initialize(base)
  @stashes = []
  @base = base

  stash_repository.stashes_all.each do |stash|
    message = stash[1]
    @stashes.unshift(Git::Stash.new(@base, message, existing: true))
  end
end

Instance Method Details

#[](index) ⇒ Git::Stash?

Returns the stash entry at the given index

Stashes are stored in newest-first order; index 0 is the most recent stash.

Examples:

Access the most recent stash

git.stashes[0].message  #=> "WIP: feature work"

Parameters:

  • index (Integer, #to_i)

    the stash index (0 = most recent)

Returns:

  • (Git::Stash, nil)

    the stash entry, or nil if the index is out of bounds



156
157
158
# File 'lib/git/stashes.rb', line 156

def [](index)
  @stashes[index.to_i]
end

#allArray<Array(Integer, String)>

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

Entries are listed in oldest-first order matching Repository#stashes_all.

Examples:

List all stash entries

git.stashes.all  #=> [[0, "testing-stash-all"], [1, "another-stash"]]

Returns:

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

    array of [index, message] pairs where index 0 is the oldest stash

Raises:



56
57
58
# File 'lib/git/stashes.rb', line 56

def all
  stash_repository.stashes_all
end

#apply(index = nil) ⇒ String

Applies a stash entry to the working directory

Examples:

Apply the most recent stash

git.stashes.apply

Apply a specific stash by index

git.stashes.apply(1)

Parameters:

  • index (Integer, nil) (defaults to: nil)

    the stash index to apply (default: latest)

Returns:

  • (String)

    the output from the git stash apply command

Raises:



91
92
93
# File 'lib/git/stashes.rb', line 91

def apply(index = nil)
  stash_repository.stash_apply(index)
end

#clear

This method returns an undefined value.

Removes all stash entries

Examples:

Clear all stashes

git.stashes.clear
git.stashes.size  #=> 0

Raises:



105
106
107
108
109
# File 'lib/git/stashes.rb', line 105

def clear
  stash_repository.stash_clear
  @stashes = []
  nil
end

#eachEnumerator<Git::Stash> #each {|stash| ... } ⇒ Array<Git::Stash>

Iterates over each stash entry in newest-first order

Examples:

Iterate over stashes

git.stashes.each { |s| puts s.message }

Overloads:

  • #eachEnumerator<Git::Stash>

    Returns an enumerator over stash entries.

    Returns:

    • (Enumerator<Git::Stash>)

      an enumerator over stash entries

  • #each {|stash| ... } ⇒ Array<Git::Stash>

    Returns the stash entries.

    Yields:

    • (stash)

      each stash entry

    Yield Parameters:

    Yield Returns:

    • (void)

    Returns:



141
142
143
# File 'lib/git/stashes.rb', line 141

def each(&)
  @stashes.each(&)
end

#save(message)

This method returns an undefined value.

Saves the current working-directory state to a new stash entry

Examples:

Save current changes to the stash

git.stashes.save('WIP: feature work')
git.stashes.size  #=> 1

Parameters:

  • message (String)

    the stash message

Raises:



72
73
74
75
# File 'lib/git/stashes.rb', line 72

def save(message)
  s = Git::Stash.new(@base, message)
  @stashes.unshift(s) if s.saved?
end

#sizeInteger

Returns the number of stash entries

Examples:

Check how many stashes exist

git.stashes.size  #=> 2

Returns:

  • (Integer)

    the number of stashes



118
119
120
# File 'lib/git/stashes.rb', line 118

def size
  @stashes.size
end