From 55a5e323d241cfbd5a59d9a440c506b24b4c255a Mon Sep 17 00:00:00 2001 From: Eric Goodwin Date: Mon, 3 Mar 2008 10:49:28 -0800 Subject: Added in the stashes --- .gitignore | 1 + Rakefile | 2 +- lib/git.rb | 3 +++ lib/git/branch.rb | 7 +++++++ lib/git/lib.rb | 32 +++++++++++++++++++++++++++++++- lib/git/stash.rb | 26 ++++++++++++++++++++++++++ lib/git/stashes.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 lib/git/stash.rb create mode 100644 lib/git/stashes.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549ba1a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.kpf \ No newline at end of file diff --git a/Rakefile b/Rakefile index a174eac..cc744c1 100644 --- a/Rakefile +++ b/Rakefile @@ -5,7 +5,7 @@ require 'rake/gempackagetask' spec = Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = "git" - s.version = "1.0.4" + s.version = "1.0.5" s.author = "Scott Chacon" s.email = "schacon@gmail.com" s.summary = "A package for using Git in Ruby code." diff --git a/lib/git.rb b/lib/git.rb index 49a61ea..c7c2703 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -23,6 +23,9 @@ require 'git/diff' require 'git/status' require 'git/author' +require 'git/stashes' +require 'git/stash' + require 'git/raw/repository' diff --git a/lib/git/branch.rb b/lib/git/branch.rb index 6c1bc44..5dc8ec7 100644 --- a/lib/git/branch.rb +++ b/lib/git/branch.rb @@ -5,12 +5,15 @@ module Git @base = nil @gcommit = nil + @stashes = nil def initialize(base, name) @remote = nil @full = name @base = base + @stashes = Git::Stashes.new(@base) + parts = name.split('/') if parts[1] @remote = Git::Remote.new(@base, parts[0]) @@ -25,6 +28,10 @@ module Git @gcommit end + def stashes + @stashes ||= Git::Stashes.new(@base) + end + def checkout check_if_create @base.checkout(@full) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index ad34709..411b491 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -401,8 +401,38 @@ module Git arr_opts << commit.to_s if commit command('reset', arr_opts) end + + def stashes_all + arr = [] + filename = File.join(@git_dir, 'logs/refs/stash') + if File.exist?(filename) + File.open(filename).each_with_index { |line, i| + m = line.match(/:(.*)$/) + arr << [i, m[1].strip] + } + end + arr + end + + def stash_save(message) + output = command('stash save', [message]) + return false unless output.match(/HEAD is now at/) + return true + end - + def stash_apply(id) + command('stash apply', [id]) + # Already uptodate! ---???? What then + end + + def stash_clear + command('stash clear') + end + + def stash_list + command('stash list') + end + def branch_new(branch) command('branch', branch) end diff --git a/lib/git/stash.rb b/lib/git/stash.rb new file mode 100644 index 0000000..e02c043 --- /dev/null +++ b/lib/git/stash.rb @@ -0,0 +1,26 @@ +module Git + class Stash + def initialize(base, message, existing=false) + @base = base + @message = message + save if existing == false + end + + def save + @saved = @base.lib.stash_save(@message) + end + + def saved? + @saved + end + + def message + @message + end + + def to_s + message + end + + end +end \ No newline at end of file diff --git a/lib/git/stashes.rb b/lib/git/stashes.rb new file mode 100644 index 0000000..78b1d59 --- /dev/null +++ b/lib/git/stashes.rb @@ -0,0 +1,49 @@ +module Git + + # object that holds all the available stashes + class Stashes + include Enumerable + + @base = nil + @stashes = nil + + def initialize(base) + @stashes = [] + + @base = base + + @base.lib.stashes_all.each do |id, message| + @stashes.unshift(Git::Stash.new(@base, message, true)) + end + end + + def save(message) + s = Git::Stash.new(@base, message) + @stashes.unshift(s) if s.saved? + end + + def apply(index=0) + @base.lib.stash_apply(index.to_i) + end + + def clear + @base.lib.stash_clear + @stashes = [] + end + + def size + @stashes.size + end + + def each + @stashes.each do |s| + yield s + end + end + + def [](symbol) + @stashes[symbol.to_s] + end + + end +end \ No newline at end of file -- cgit