iownbey-git 1.0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/git/log.rb ADDED
@@ -0,0 +1,115 @@
1
+ module Git
2
+
3
+ # object that holds the last X commits on given branch
4
+ class Log
5
+ include Enumerable
6
+
7
+ @base = nil
8
+ @commits = nil
9
+
10
+ @object = nil
11
+ @path = nil
12
+ @count = nil
13
+ @since = nil
14
+ @until = nil
15
+ @between = nil
16
+
17
+ @dirty_flag = nil
18
+
19
+ def initialize(base, count = 30)
20
+ dirty_log
21
+ @base = base
22
+ @count = count
23
+ end
24
+
25
+ def object(objectish)
26
+ dirty_log
27
+ @object = objectish
28
+ return self
29
+ end
30
+
31
+ def author(regex)
32
+ dirty_log
33
+ @author = regex
34
+ return self
35
+ end
36
+
37
+ def grep(regex)
38
+ dirty_log
39
+ @grep = regex
40
+ return self
41
+ end
42
+
43
+ def path(path)
44
+ dirty_log
45
+ @path = path
46
+ return self
47
+ end
48
+
49
+ def since(date)
50
+ dirty_log
51
+ @since = date
52
+ return self
53
+ end
54
+
55
+ def until(date)
56
+ dirty_log
57
+ @until = date
58
+ return self
59
+ end
60
+
61
+ def between(sha1, sha2 = nil)
62
+ dirty_log
63
+ @between = [sha1, sha2]
64
+ return self
65
+ end
66
+
67
+ def to_s
68
+ self.map { |c| c.to_s }.join("\n")
69
+ end
70
+
71
+
72
+ # forces git log to run
73
+
74
+ def size
75
+ check_log
76
+ @commits.size rescue nil
77
+ end
78
+
79
+ def each
80
+ check_log
81
+ @commits.each do |c|
82
+ yield c
83
+ end
84
+ end
85
+
86
+ def first
87
+ check_log
88
+ @commits.first rescue nil
89
+ end
90
+
91
+ private
92
+
93
+ def dirty_log
94
+ @dirty_flag = true
95
+ end
96
+
97
+ def check_log
98
+ if @dirty_flag
99
+ run_log
100
+ @dirty_flag = false
101
+ end
102
+ end
103
+
104
+ # actually run the 'git log' command
105
+ def run_log
106
+ log = @base.lib.full_log_commits(:count => @count, :object => @object,
107
+ :path_limiter => @path, :since => @since,
108
+ :author => @author, :grep => @grep,
109
+ :until => @until, :between => @between)
110
+ @commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) }
111
+ end
112
+
113
+ end
114
+
115
+ end
data/lib/git/object.rb ADDED
@@ -0,0 +1,296 @@
1
+ module Git
2
+
3
+ class GitTagNameDoesNotExist< StandardError
4
+ end
5
+
6
+ # represents a git object
7
+ class Object
8
+
9
+ class AbstractObject
10
+ attr_accessor :objectish, :size, :type, :mode
11
+
12
+ @base = nil
13
+ @contents = nil
14
+ @size = nil
15
+ @sha = nil
16
+
17
+ def initialize(base, objectish)
18
+ @base = base
19
+ @objectish = objectish.to_s
20
+ setup
21
+ end
22
+
23
+ def sha
24
+ @sha || @sha = @base.lib.revparse(@objectish)
25
+ end
26
+
27
+ def size
28
+ @size || @size = @base.lib.object_size(@objectish)
29
+ end
30
+
31
+ # get the object's contents
32
+ # if no block is given, the contents are cached in memory and returned as a string
33
+ # if a block is given, it yields an IO object (via IO::popen) which could be used to
34
+ # read a large file in chunks. use this for large files so that they are not held
35
+ # in memory
36
+ def contents(&block)
37
+ if block_given?
38
+ @base.lib.object_contents(@objectish, &block)
39
+ else
40
+ @contents || @contents = @base.lib.object_contents(@objectish)
41
+ end
42
+ end
43
+
44
+ def contents_array
45
+ self.contents.split("\n")
46
+ end
47
+
48
+ def setup
49
+ raise NotImplementedError
50
+ end
51
+
52
+ def to_s
53
+ @objectish
54
+ end
55
+
56
+ def grep(string, path_limiter = nil, opts = {})
57
+ default = {:object => sha, :path_limiter => path_limiter}
58
+ grep_options = default.merge(opts)
59
+ @base.lib.grep(string, grep_options)
60
+ end
61
+
62
+ def diff(objectish)
63
+ Git::Diff.new(@base, @objectish, objectish)
64
+ end
65
+
66
+ def log(count = 30)
67
+ Git::Log.new(@base, count).object(@objectish)
68
+ end
69
+
70
+ # creates an archive of this object (tree)
71
+ def archive(file = nil, opts = {})
72
+ @base.lib.archive(@objectish, file, opts)
73
+ end
74
+
75
+ def tree?
76
+ @type == 'tree'
77
+ end
78
+
79
+ def blob?
80
+ @type == 'blob'
81
+ end
82
+
83
+ def commit?
84
+ @type == 'commit'
85
+ end
86
+
87
+ def tag?
88
+ @type == 'tag'
89
+ end
90
+
91
+ end
92
+
93
+
94
+ class Blob < AbstractObject
95
+
96
+ def initialize(base, sha, mode = nil)
97
+ super(base, sha)
98
+ @mode = mode
99
+ end
100
+
101
+ private
102
+
103
+ def setup
104
+ @type = 'blob'
105
+ end
106
+ end
107
+
108
+ class Tree < AbstractObject
109
+
110
+ @trees = nil
111
+ @blobs = nil
112
+
113
+ def initialize(base, sha, mode = nil)
114
+ super(base, sha)
115
+ @mode = mode
116
+ end
117
+
118
+ def children
119
+ blobs.merge(subtrees)
120
+ end
121
+
122
+ def blobs
123
+ check_tree
124
+ @blobs
125
+ end
126
+ alias_method :files, :blobs
127
+
128
+ def trees
129
+ check_tree
130
+ @trees
131
+ end
132
+ alias_method :subtrees, :trees
133
+ alias_method :subdirectories, :trees
134
+
135
+ def full_tree
136
+ @base.lib.full_tree(@objectish)
137
+ end
138
+
139
+ def depth
140
+ @base.lib.tree_depth(@objectish)
141
+ end
142
+
143
+ private
144
+
145
+ def setup
146
+ @type = 'tree'
147
+ end
148
+
149
+ # actually run the git command
150
+ def check_tree
151
+ if !@trees
152
+ @trees = {}
153
+ @blobs = {}
154
+ data = @base.lib.ls_tree(@objectish)
155
+ data['tree'].each { |k, d| @trees[k] = Git::Object::Tree.new(@base, d[:sha], d[:mode]) }
156
+ data['blob'].each { |k, d| @blobs[k] = Git::Object::Blob.new(@base, d[:sha], d[:mode]) }
157
+ end
158
+ end
159
+
160
+ end
161
+
162
+ class Commit < AbstractObject
163
+
164
+ @tree = nil
165
+ @parents = nil
166
+ @author = nil
167
+ @committer = nil
168
+ @message = nil
169
+
170
+ def initialize(base, sha, init = nil)
171
+ super(base, sha)
172
+ if init
173
+ set_commit(init)
174
+ end
175
+ end
176
+
177
+ def message
178
+ check_commit
179
+ @message
180
+ end
181
+
182
+ def name
183
+ @base.lib.namerev(sha)
184
+ end
185
+
186
+ def gtree
187
+ check_commit
188
+ Tree.new(@base, @tree)
189
+ end
190
+
191
+ def parent
192
+ parents.first
193
+ end
194
+
195
+ # array of all parent commits
196
+ def parents
197
+ check_commit
198
+ @parents
199
+ end
200
+
201
+ # git author
202
+ def author
203
+ check_commit
204
+ @author
205
+ end
206
+
207
+ def author_date
208
+ author.date
209
+ end
210
+
211
+ # git author
212
+ def committer
213
+ check_commit
214
+ @committer
215
+ end
216
+
217
+ def committer_date
218
+ committer.date
219
+ end
220
+ alias_method :date, :committer_date
221
+
222
+ def diff_parent
223
+ diff(parent)
224
+ end
225
+
226
+ def set_commit(data)
227
+ if data['sha']
228
+ @sha = data['sha']
229
+ end
230
+ @committer = Git::Author.new(data['committer'])
231
+ @author = Git::Author.new(data['author'])
232
+ @tree = Git::Object::Tree.new(@base, data['tree'])
233
+ @parents = data['parent'].map{ |sha| Git::Object::Commit.new(@base, sha) }
234
+ @message = data['message'].chomp
235
+ end
236
+
237
+ private
238
+
239
+ def setup
240
+ @type = 'commit'
241
+ end
242
+
243
+ # see if this object has been initialized and do so if not
244
+ def check_commit
245
+ if !@tree
246
+ data = @base.lib.commit_data(@objectish)
247
+ set_commit(data)
248
+ end
249
+ end
250
+
251
+ end
252
+
253
+ class Tag < AbstractObject
254
+ attr_accessor :name
255
+
256
+ def initialize(base, sha, name)
257
+ super(base, sha)
258
+ @name = name
259
+ end
260
+
261
+ private
262
+
263
+ def setup
264
+ @type = 'tag'
265
+ end
266
+
267
+ end
268
+
269
+ class << self
270
+ # if we're calling this, we don't know what type it is yet
271
+ # so this is our little factory method
272
+ def new(base, objectish, type = nil, is_tag = false)
273
+ if is_tag
274
+ sha = base.lib.tag_sha(objectish)
275
+ if sha == ''
276
+ raise Git::GitTagNameDoesNotExist.new(objectish)
277
+ end
278
+ return Git::Object::Tag.new(base, sha, objectish)
279
+ else
280
+ if !type
281
+ type = base.lib.object_type(objectish)
282
+ end
283
+ end
284
+
285
+ klass =
286
+ case type
287
+ when /blob/: Blob
288
+ when /commit/: Commit
289
+ when /tree/: Tree
290
+ end
291
+ klass::new(base, objectish)
292
+ end
293
+ end
294
+
295
+ end
296
+ end
data/lib/git/path.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Git
2
+ class Path
3
+
4
+ attr_accessor :path
5
+
6
+ def initialize(path, check_path = true)
7
+ if !check_path || File.exists?(path)
8
+ @path = File.expand_path(path)
9
+ else
10
+ raise ArgumentError, "path does not exist", File.expand_path(path)
11
+ end
12
+ end
13
+
14
+ def readable?
15
+ File.readable?(@path)
16
+ end
17
+
18
+ def writable?
19
+ File.writable?(@path)
20
+ end
21
+
22
+ def to_s
23
+ @path
24
+ end
25
+
26
+ end
27
+ end
data/lib/git/remote.rb ADDED
@@ -0,0 +1,42 @@
1
+ module Git
2
+ class Remote < Path
3
+
4
+ attr_accessor :name, :url, :fetch_opts
5
+
6
+ @base = nil
7
+
8
+ def initialize(base, name)
9
+ @base = base
10
+ config = @base.lib.config_remote(name)
11
+ @name = name
12
+ @url = config['url']
13
+ @fetch_opts = config['fetch']
14
+ end
15
+
16
+ def remove
17
+ @base.remote_remove(@name)
18
+ end
19
+
20
+ def fetch
21
+ @base.fetch(@name)
22
+ end
23
+
24
+ # merge this remote locally
25
+ def merge(branch = 'master')
26
+ @base.merge("#{@name}/#{branch}")
27
+ end
28
+
29
+ def branch(branch = 'master')
30
+ Git::Branch.new(@base, "#{@name}/#{branch}")
31
+ end
32
+
33
+ def remove
34
+ @base.lib.remote_remove(@name)
35
+ end
36
+
37
+ def to_s
38
+ @name
39
+ end
40
+
41
+ end
42
+ end