Class: Git::Stashes Deprecated

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

Overview

Deprecated.

Use Repository#stash_infos and StashInfo instead

Collection of stash entries for a Git repository

This class is deprecated and will be removed in v6.0.0. Use the Repository stash methods and StashInfo instead: Repository#stash_infos replaces the collection, and Repository#stash_push, Repository#stash_apply, and Repository#stash_clear replace #save, #apply, and #clear.

Examples:

Iterate over stash entries (deprecated)

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

The replacement

repo.stash_infos.each { |info| puts info.message }
repo.stash_infos.size   #=> 2
repo.stash_apply

See Also:

  • Repository#stash_infos

Instance Method Summary collapse

Constructor Details

#initialize(base)

Deprecated.

Use Repository#stash_infos and Git::StashInfo instead

Initialize the stashes collection

Loads all existing stash entries from the repository at construction time. Emits one deprecation warning per object.

Examples:

Load stashes for a repository

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

Parameters:

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/git/stashes.rb', line 46

def initialize(base)
  Git::Deprecation.warn(
    'Git::Stashes is deprecated and will be removed in v6.0.0. ' \
    'Use the Git::Repository stash methods (stash_infos, stash_push, stash_apply, stash_clear) ' \
    'and Git::StashInfo instead.'
  )
  @stashes = []
  @base = base
  # stashes_all and Git::Stash are deprecated too; silence them so one
  # Git::Stashes.new emits one warning
  Git::Deprecation.silence { load_stashes }
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



173
174
175
# File 'lib/git/stashes.rb', line 173

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:



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

def all
  # stashes_all is deprecated too; silence it so this call emits no second warning
  Git::Deprecation.silence { 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:



108
109
110
# File 'lib/git/stashes.rb', line 108

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:



122
123
124
125
126
# File 'lib/git/stashes.rb', line 122

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:



158
159
160
# File 'lib/git/stashes.rb', line 158

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:



88
89
90
91
92
# File 'lib/git/stashes.rb', line 88

def save(message)
  # Git::Stash is deprecated too; silence it so this call emits no second warning
  s = Git::Deprecation.silence { 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



135
136
137
# File 'lib/git/stashes.rb', line 135

def size
  @stashes.size
end