Skip to content

Commit bc09a70

Browse files
committed
Merge commit 'eric/master' into test
2 parents f859e80 + 55a5e32 commit bc09a70

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.kpf

lib/git.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
require 'git/status'
2424
require 'git/author'
2525

26+
require 'git/stashes'
27+
require 'git/stash'
28+
2629
require 'git/raw/repository'
2730

2831

lib/git/branch.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ class Branch < Path
55

66
@base = nil
77
@gcommit = nil
8+
@stashes = nil
89

910
def initialize(base, name)
1011
@remote = nil
1112
@full = name
1213
@base = base
1314

15+
@stashes = Git::Stashes.new(@base)
16+
1417
parts = name.split('/')
1518
if parts[1]
1619
@remote = Git::Remote.new(@base, parts[0])
@@ -25,6 +28,10 @@ def gcommit
2528
@gcommit
2629
end
2730

31+
def stashes
32+
@stashes ||= Git::Stashes.new(@base)
33+
end
34+
2835
def checkout
2936
check_if_create
3037
@base.checkout(@full)

lib/git/lib.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,38 @@ def reset(commit, opts = {})
401401
arr_opts << commit.to_s if commit
402402
command('reset', arr_opts)
403403
end
404+
405+
def stashes_all
406+
arr = []
407+
filename = File.join(@git_dir, 'logs/refs/stash')
408+
if File.exist?(filename)
409+
File.open(filename).each_with_index { |line, i|
410+
m = line.match(/:(.*)$/)
411+
arr << [i, m[1].strip]
412+
}
413+
end
414+
arr
415+
end
416+
417+
def stash_save(message)
418+
output = command('stash save', [message])
419+
return false unless output.match(/HEAD is now at/)
420+
return true
421+
end
404422

405-
423+
def stash_apply(id)
424+
command('stash apply', [id])
425+
# Already uptodate! ---???? What then
426+
end
427+
428+
def stash_clear
429+
command('stash clear')
430+
end
431+
432+
def stash_list
433+
command('stash list')
434+
end
435+
406436
def branch_new(branch)
407437
command('branch', branch)
408438
end

lib/git/stash.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Git
2+
class Stash
3+
def initialize(base, message, existing=false)
4+
@base = base
5+
@message = message
6+
save if existing == false
7+
end
8+
9+
def save
10+
@saved = @base.lib.stash_save(@message)
11+
end
12+
13+
def saved?
14+
@saved
15+
end
16+
17+
def message
18+
@message
19+
end
20+
21+
def to_s
22+
message
23+
end
24+
25+
end
26+
end

lib/git/stashes.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Git
2+
3+
# object that holds all the available stashes
4+
class Stashes
5+
include Enumerable
6+
7+
@base = nil
8+
@stashes = nil
9+
10+
def initialize(base)
11+
@stashes = []
12+
13+
@base = base
14+
15+
@base.lib.stashes_all.each do |id, message|
16+
@stashes.unshift(Git::Stash.new(@base, message, true))
17+
end
18+
end
19+
20+
def save(message)
21+
s = Git::Stash.new(@base, message)
22+
@stashes.unshift(s) if s.saved?
23+
end
24+
25+
def apply(index=0)
26+
@base.lib.stash_apply(index.to_i)
27+
end
28+
29+
def clear
30+
@base.lib.stash_clear
31+
@stashes = []
32+
end
33+
34+
def size
35+
@stashes.size
36+
end
37+
38+
def each
39+
@stashes.each do |s|
40+
yield s
41+
end
42+
end
43+
44+
def [](symbol)
45+
@stashes[symbol.to_s]
46+
end
47+
48+
end
49+
end

0 commit comments

Comments
 (0)