schacon-git 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/git/branch.rb ADDED
@@ -0,0 +1,106 @@
1
+ module Git
2
+ class Branch < Path
3
+
4
+ attr_accessor :full, :remote, :name
5
+
6
+ @base = nil
7
+ @gcommit = nil
8
+ @stashes = nil
9
+
10
+ def initialize(base, name)
11
+ @remote = nil
12
+ @full = name
13
+ @base = base
14
+
15
+ parts = name.split('/')
16
+ if parts[1]
17
+ @remote = Git::Remote.new(@base, parts[0])
18
+ @name = parts[1]
19
+ else
20
+ @name = parts[0]
21
+ end
22
+ end
23
+
24
+ def gcommit
25
+ @gcommit = @base.gcommit(@full) if !@gcommit
26
+ @gcommit
27
+ end
28
+
29
+ def stashes
30
+ @stashes ||= Git::Stashes.new(@base)
31
+ end
32
+
33
+ def checkout
34
+ check_if_create
35
+ @base.checkout(@full)
36
+ end
37
+
38
+ def archive(file, opts = {})
39
+ @base.lib.archive(@full, file, opts)
40
+ end
41
+
42
+ # g.branch('new_branch').in_branch do
43
+ # # create new file
44
+ # # do other stuff
45
+ # return true # auto commits and switches back
46
+ # end
47
+ def in_branch (message = 'in branch work')
48
+ old_current = @base.lib.branch_current
49
+ checkout
50
+ if yield
51
+ @base.commit_all(message)
52
+ else
53
+ @base.reset_hard
54
+ end
55
+ @base.checkout(old_current)
56
+ end
57
+
58
+ def create
59
+ check_if_create
60
+ end
61
+
62
+ def delete
63
+ @base.lib.branch_delete(@name)
64
+ end
65
+
66
+ def current
67
+ determine_current
68
+ end
69
+
70
+ def merge(branch = nil, message = nil)
71
+ if branch
72
+ in_branch do
73
+ @base.merge(branch, message)
74
+ false
75
+ end
76
+ # merge a branch into this one
77
+ else
78
+ # merge this branch into the current one
79
+ @base.merge(@name)
80
+ end
81
+ end
82
+
83
+ def update_ref(commit)
84
+ @base.lib.update_ref(@full, commit)
85
+ end
86
+
87
+ def to_a
88
+ [@full]
89
+ end
90
+
91
+ def to_s
92
+ @full
93
+ end
94
+
95
+ private
96
+
97
+ def check_if_create
98
+ @base.lib.branch_new(@name) rescue nil
99
+ end
100
+
101
+ def determine_current
102
+ @base.lib.branch_current == @name
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,57 @@
1
+ module Git
2
+
3
+ # object that holds all the available branches
4
+ class Branches
5
+ include Enumerable
6
+
7
+ @base = nil
8
+ @branches = nil
9
+
10
+ def initialize(base)
11
+ @branches = {}
12
+
13
+ @base = base
14
+
15
+ @base.lib.branches_all.each do |b|
16
+ @branches[b[0]] = Git::Branch.new(@base, b[0])
17
+ end
18
+ end
19
+
20
+ def local
21
+ self.select { |b| !b.remote }
22
+ end
23
+
24
+ def remote
25
+ self.select { |b| b.remote }
26
+ end
27
+
28
+ # array like methods
29
+
30
+ def size
31
+ @branches.size
32
+ end
33
+
34
+ def each
35
+ @branches.each do |k, b|
36
+ yield b
37
+ end
38
+ end
39
+
40
+ def [](symbol)
41
+ @branches[symbol.to_s]
42
+ end
43
+
44
+ def to_s
45
+ out = ''
46
+ @branches.each do |k, b|
47
+ if b.current
48
+ out += "* " + b.to_s + "\n"
49
+ else
50
+ out += " " + b.to_s + "\n"
51
+ end
52
+ end
53
+ out
54
+ end
55
+
56
+ end
57
+ end
data/lib/git/diff.rb ADDED
@@ -0,0 +1,143 @@
1
+ module Git
2
+
3
+ # object that holds the last X commits on given branch
4
+ class Diff
5
+ include Enumerable
6
+
7
+ @base = nil
8
+ @from = nil
9
+ @to = nil
10
+ @path = nil
11
+
12
+ @full_diff = nil
13
+ @full_diff_files = nil
14
+ @stats = nil
15
+
16
+ def initialize(base, from = nil, to = nil)
17
+ @base = base
18
+ @from = from.to_s
19
+ @to = to.to_s
20
+ end
21
+
22
+ def path(path)
23
+ @path = path
24
+ return self
25
+ end
26
+
27
+ def size
28
+ cache_stats
29
+ @stats[:total][:files]
30
+ end
31
+
32
+ def lines
33
+ cache_stats
34
+ @stats[:total][:lines]
35
+ end
36
+
37
+ def deletions
38
+ cache_stats
39
+ @stats[:total][:deletions]
40
+ end
41
+
42
+ def insertions
43
+ cache_stats
44
+ @stats[:total][:insertions]
45
+ end
46
+
47
+ def stats
48
+ cache_stats
49
+ @stats
50
+ end
51
+
52
+ # if file is provided and is writable, it will write the patch into the file
53
+ def patch(file = nil)
54
+ cache_full
55
+ @full_diff
56
+ end
57
+ alias_method :to_s, :patch
58
+
59
+ # enumerable methods
60
+
61
+ def [](key)
62
+ process_full
63
+ @full_diff_files.assoc(key)[1]
64
+ end
65
+
66
+ def each
67
+ process_full
68
+ @full_diff_files.each do |file|
69
+ yield file[1]
70
+ end
71
+ end
72
+
73
+ class DiffFile
74
+ attr_accessor :patch, :path, :mode, :src, :dst, :type
75
+ @base = nil
76
+
77
+ def initialize(base, hash)
78
+ @base = base
79
+ @patch = hash[:patch]
80
+ @path = hash[:path]
81
+ @mode = hash[:mode]
82
+ @src = hash[:src]
83
+ @dst = hash[:dst]
84
+ @type = hash[:type]
85
+ end
86
+
87
+ def blob(type = :dst)
88
+ if type == :src
89
+ @base.object(@src) if @src != '0000000'
90
+ else
91
+ @base.object(@dst) if @dst != '0000000'
92
+ end
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def cache_full
99
+ if !@full_diff
100
+ @full_diff = @base.lib.diff_full(@from, @to, {:path_limiter => @path})
101
+ end
102
+ end
103
+
104
+ def process_full
105
+ if !@full_diff_files
106
+ cache_full
107
+ @full_diff_files = process_full_diff
108
+ end
109
+ end
110
+
111
+ def cache_stats
112
+ if !@stats
113
+ @stats = @base.lib.diff_stats(@from, @to, {:path_limiter => @path})
114
+ end
115
+ end
116
+
117
+ # break up @diff_full
118
+ def process_full_diff
119
+ final = {}
120
+ current_file = nil
121
+ @full_diff.split("\n").each do |line|
122
+ if m = /diff --git a\/(.*?) b\/(.*?)/.match(line)
123
+ current_file = m[1]
124
+ final[current_file] = {:patch => line, :path => current_file,
125
+ :mode => '', :src => '', :dst => '', :type => 'modified'}
126
+ else
127
+ if m = /index (.......)\.\.(.......)( ......)*/.match(line)
128
+ final[current_file][:src] = m[1]
129
+ final[current_file][:dst] = m[2]
130
+ final[current_file][:mode] = m[3].strip if m[3]
131
+ end
132
+ if m = /(.*?) file mode (......)/.match(line)
133
+ final[current_file][:type] = m[1]
134
+ final[current_file][:mode] = m[2]
135
+ end
136
+ final[current_file][:patch] << "\n" + line
137
+ end
138
+ end
139
+ final.map { |e| [e[0], DiffFile.new(@base, e[1])] }
140
+ end
141
+
142
+ end
143
+ end
data/lib/git/index.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Git
2
+ class Index < Git::Path
3
+
4
+ end
5
+ end