Class: Git::Stash

Inherits:
Object
  • Object
show all
Defined in:
lib/git/stash.rb

Overview

Represents a single stash entry in a Git repository

Examples:

Create a stash and inspect the result

stash = Git::Stash.new(repo, 'WIP: feature work')
stash.message  #=> "WIP: feature work"
stash.saved?   #=> true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, message, existing: false)

Initialize a Stash object

When existing is false (the default), immediately calls #save to push the current working-directory state onto the stash stack.

Examples:

Create a new stash entry

stash = Git::Stash.new(repo, 'WIP: feature work')
stash.saved?  #=> true

Reference an existing stash without pushing

stash = Git::Stash.new(repo, 'WIP: feature work', existing: true)
stash.saved?  #=> nil

Parameters:

  • base (Git::Repository, Git::Base)

    the git repository

  • message (String)

    the stash message

  • existing (Boolean) (defaults to: false)

    (false) when true, wraps an existing stash entry without pushing any changes



38
39
40
41
42
# File 'lib/git/stash.rb', line 38

def initialize(base, message, existing: false)
  @base = base
  @message = message
  save unless existing
end

Instance Attribute Details

#messageString (readonly)

Returns the stash description

Examples:

Read the stash message

stash = Git::Stash.new(repo, 'WIP: feature work', existing: true)
stash.message  #=> "WIP: feature work"

Returns:

  • (String)

    the stash message



80
81
82
# File 'lib/git/stash.rb', line 80

def message
  @message
end

Instance Method Details

#saveBoolean

Saves the current working-directory state to the stash stack

Examples:

Save changes to the stash stack

stash = Git::Stash.new(repo, 'WIP', existing: true)
stash.save  #=> true

Returns:

  • (Boolean)

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

Raises:



55
56
57
# File 'lib/git/stash.rb', line 55

def save
  @saved = stash_repository.stash_save(@message)
end

#saved?Boolean?

Returns whether the stash was saved successfully

Examples:

Check if changes were stashed

stash = Git::Stash.new(repo, 'WIP: feature work')
stash.saved?  #=> true

Returns:

  • (Boolean, nil)

    true if changes were stashed, false if there were no local changes, nil if #save has not been called (e.g. existing: true)



68
69
70
# File 'lib/git/stash.rb', line 68

def saved?
  @saved
end

#to_sString

Returns the stash description as a string

Examples:

Convert stash to string

stash = Git::Stash.new(repo, 'WIP: feature work', existing: true)
stash.to_s  #=> "WIP: feature work"

Returns:

  • (String)

    the stash message



90
91
92
# File 'lib/git/stash.rb', line 90

def to_s
  message
end