rhomobile-grit 2.4.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/grit/index.rb ADDED
@@ -0,0 +1,197 @@
1
+ module Grit
2
+
3
+ class Index
4
+ # Public: Gets/Sets the Grit::Repo to which this index belongs.
5
+ attr_accessor :repo
6
+
7
+ # Public: Gets/Sets the Hash tree map that holds the changes to be made
8
+ # in the next commit.
9
+ attr_accessor :tree
10
+
11
+ # Public: Gets/Sets the Grit::Tree object representing the tree upon
12
+ # which the next commit will be based.
13
+ attr_accessor :current_tree
14
+
15
+ # Initialize a new Index object.
16
+ #
17
+ # repo - The Grit::Repo to which the index belongs.
18
+ #
19
+ # Returns the newly initialized Grit::Index.
20
+ def initialize(repo)
21
+ self.repo = repo
22
+ self.tree = {}
23
+ self.current_tree = nil
24
+ end
25
+
26
+ # Public: Add a file to the index.
27
+ #
28
+ # path - The String file path including filename (no slash prefix).
29
+ # data - The String binary contents of the file.
30
+ #
31
+ # Returns nothing.
32
+ def add(path, data)
33
+ path = path.split('/')
34
+ filename = path.pop
35
+
36
+ current = self.tree
37
+
38
+ path.each do |dir|
39
+ current[dir] ||= {}
40
+ node = current[dir]
41
+ current = node
42
+ end
43
+
44
+ current[filename] = data
45
+ end
46
+
47
+ # Public: Delete the given file from the index.
48
+ #
49
+ # path - The String file path including filename (no slash prefix).
50
+ #
51
+ # Returns nothing.
52
+ def delete(path)
53
+ add(path, false)
54
+ end
55
+
56
+ # Public: Read the contents of the given Tree into the index to use as a
57
+ # starting point for the index.
58
+ #
59
+ # tree - The String branch/tag/sha of the Git tree object.
60
+ #
61
+ # Returns nothing.
62
+ def read_tree(tree)
63
+ self.current_tree = self.repo.tree(tree)
64
+ end
65
+
66
+ # Public: Commit the contents of the index. This method supports two
67
+ # formats for arguments:
68
+ #
69
+ # message - The String commit message.
70
+ # options - An optional Hash of index options.
71
+ # :parents - Array of String commit SHA1s or Grit::Commit
72
+ # objects to attach this commit to to form a
73
+ # new head (default: nil).
74
+ # :actor - The Grit::Actor details of the user making
75
+ # the commit (default: nil).
76
+ # :last_tree - The String SHA1 of a tree to compare with
77
+ # in order to avoid making empty commits
78
+ # (default: nil).
79
+ # :head - The String branch name to write this head to
80
+ # (default: "master").
81
+ # :committed_date - The Time that the commit was made.
82
+ # (Default: Time.now)
83
+ # :authored_date - The Time that the commit was authored.
84
+ # (Default: committed_date)
85
+ #
86
+ # The legacy argument style looks like:
87
+ #
88
+ # message - The String commit message.
89
+ # parents - Array of String commit SHA1s or Grit::Commit objects to
90
+ # attach this commit to to form a new head (default: nil).
91
+ # actor - The Grit::Actor details of the user making the commit
92
+ # (default: nil).
93
+ # last_tree - The String SHA1 of a tree to compare with in order to avoid
94
+ # making empty commits (default: nil).
95
+ # head - The String branch name to write this head to
96
+ # (default: "master").
97
+ #
98
+ # Returns a String of the SHA1 of the new commit.
99
+ def commit(message, parents = nil, actor = nil, last_tree = nil, head = 'master')
100
+ if parents.is_a?(Hash)
101
+ actor = parents[:actor]
102
+ committer = parents[:committer]
103
+ author = parents[:author]
104
+ last_tree = parents[:last_tree]
105
+ head = parents[:head]
106
+ committed_date = parents[:committed_date]
107
+ authored_date = parents[:authored_date]
108
+ parents = parents[:parents]
109
+ end
110
+
111
+ committer ||= actor
112
+ author ||= committer
113
+
114
+ tree_sha1 = write_tree(self.tree, self.current_tree)
115
+
116
+ # don't write identical commits
117
+ return false if tree_sha1 == last_tree
118
+
119
+ contents = []
120
+ contents << ['tree', tree_sha1].join(' ')
121
+ parents.each do |p|
122
+ contents << ['parent', p].join(' ')
123
+ end if parents
124
+
125
+ committer ||= begin
126
+ config = Config.new(self.repo)
127
+ Actor.new(config['user.name'], config['user.email'])
128
+ end
129
+ author ||= committer
130
+ committed_date ||= Time.now
131
+ authored_date ||= committed_date
132
+
133
+ contents << ['author', author.output(authored_date)].join(' ')
134
+ contents << ['committer', committer.output(committed_date)].join(' ')
135
+ contents << ''
136
+ contents << message
137
+
138
+ commit_sha1 = self.repo.git.put_raw_object(contents.join("\n"), 'commit')
139
+
140
+ self.repo.update_ref(head, commit_sha1)
141
+ end
142
+
143
+ # Recursively write a tree to the index.
144
+ #
145
+ # tree - The Hash tree map:
146
+ # key - The String directory or filename.
147
+ # val - The Hash submap or the String contents of the file.
148
+ # now_tree - The Grit::Tree representing the a previous tree upon which
149
+ # this tree will be based (default: nil).
150
+ #
151
+ # Returns the String SHA1 String of the tree.
152
+ def write_tree(tree, now_tree = nil)
153
+ tree_contents = {}
154
+
155
+ # fill in original tree
156
+ now_tree.contents.each do |obj|
157
+ sha = [obj.id].pack("H*")
158
+ k = obj.name
159
+ k += '/' if (obj.class == Grit::Tree)
160
+ tmode = obj.mode.to_i.to_s ## remove zero-padding
161
+ tree_contents[k] = "%s %s\0%s" % [tmode, obj.name, sha]
162
+ end if now_tree
163
+
164
+ # overwrite with new tree contents
165
+ tree.each do |k, v|
166
+ case v
167
+ when String
168
+ sha = write_blob(v)
169
+ sha = [sha].pack("H*")
170
+ str = "%s %s\0%s" % ['100644', k, sha]
171
+ tree_contents[k] = str
172
+ when Hash
173
+ ctree = now_tree/k if now_tree
174
+ sha = write_tree(v, ctree)
175
+ sha = [sha].pack("H*")
176
+ str = "%s %s\0%s" % ['40000', k, sha]
177
+ tree_contents[k + '/'] = str
178
+ when false
179
+ tree_contents.delete(k)
180
+ end
181
+ end
182
+
183
+ tr = tree_contents.sort.map { |k, v| v }.join('')
184
+ self.repo.git.put_raw_object(tr, 'tree')
185
+ end
186
+
187
+ # Write a blob to the index.
188
+ #
189
+ # data - The String data to write.
190
+ #
191
+ # Returns the String SHA1 of the new blob.
192
+ def write_blob(data)
193
+ self.repo.git.put_raw_object(data, 'blob')
194
+ end
195
+ end # Index
196
+
197
+ end # Grit
data/lib/grit/jruby.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'grit/process'
2
+
3
+ module Grit
4
+ # Override the Grit::Process class's popen4 and waitpid methods to work around
5
+ # various quirks in JRuby.
6
+ class Process
7
+ # Use JRuby's built in IO.popen4 but emulate the special spawn env
8
+ # and options arguments as best we can.
9
+ def popen4(*argv)
10
+ env = (argv.shift if argv[0].is_a?(Hash)) || {}
11
+ opt = (argv.pop if argv[-1].is_a?(Hash)) || {}
12
+
13
+ # emulate :chdir option
14
+ if opt[:chdir]
15
+ previous_dir = Dir.pwd
16
+ Dir.chdir(opt[:chdir])
17
+ else
18
+ previous_dir = nil
19
+ end
20
+
21
+ # emulate :env option
22
+ if env.size > 0
23
+ previous_env = ENV
24
+ ENV.merge!(env)
25
+ else
26
+ previous_env = nil
27
+ end
28
+
29
+ pid, stdin, stdout, stderr = IO.popen4(*argv)
30
+ ensure
31
+ ENV.replace(previous_env) if previous_env
32
+ Dir.chdir(previous_dir) if previous_dir
33
+ end
34
+
35
+ # JRuby always raises ECHILD on pids returned from its IO.popen4 method
36
+ # for some reason. Return a fake Process::Status object.
37
+ FakeStatus = Struct.new(:pid, :exitstatus, :success?, :fake?)
38
+ def waitpid(pid)
39
+ ::Process::waitpid(pid)
40
+ $?
41
+ rescue Errno::ECHILD
42
+ FakeStatus.new(pid, 0, true, true)
43
+ end
44
+ end
45
+ end
data/lib/grit/lazy.rb ADDED
@@ -0,0 +1,35 @@
1
+ ##
2
+ # Allows attributes to be declared as lazy, meaning that they won't be
3
+ # computed until they are asked for.
4
+ #
5
+ # Works by delegating each lazy_reader to a cached lazy_source method.
6
+ #
7
+ # class Person
8
+ # lazy_reader :eyes
9
+ #
10
+ # def lazy_source
11
+ # OpenStruct.new(:eyes => 2)
12
+ # end
13
+ # end
14
+ #
15
+ # >> Person.new.eyes
16
+ # => 2
17
+ #
18
+ module Lazy
19
+ def self.extended(klass)
20
+ klass.send(:attr_writer, :lazy_source)
21
+ end
22
+
23
+ def lazy_reader(*args)
24
+ args.each do |arg|
25
+ ivar = "@#{arg}"
26
+ define_method(arg) do
27
+ if instance_variable_defined?(ivar)
28
+ val = instance_variable_get(ivar)
29
+ return val if val
30
+ end
31
+ instance_variable_set(ivar, (@lazy_source ||= lazy_source).send(arg))
32
+ end
33
+ end
34
+ end
35
+ end
data/lib/grit/merge.rb ADDED
@@ -0,0 +1,45 @@
1
+ module Grit
2
+
3
+ class Merge
4
+
5
+ STATUS_BOTH = 'both'
6
+ STATUS_OURS = 'ours'
7
+ STATUS_THEIRS = 'theirs'
8
+
9
+ attr_reader :conflicts, :text, :sections
10
+
11
+ def initialize(str)
12
+ status = STATUS_BOTH
13
+
14
+ section = 1
15
+ @conflicts = 0
16
+ @text = {}
17
+
18
+ lines = str.split("\n")
19
+ lines.each do |line|
20
+ if /^<<<<<<< (.*?)/.match(line)
21
+ status = STATUS_OURS
22
+ @conflicts += 1
23
+ section += 1
24
+ elsif line == '======='
25
+ status = STATUS_THEIRS
26
+ elsif /^>>>>>>> (.*?)/.match(line)
27
+ status = STATUS_BOTH
28
+ section += 1
29
+ else
30
+ @text[section] ||= {}
31
+ @text[section][status] ||= []
32
+ @text[section][status] << line
33
+ end
34
+ end
35
+ @text = @text.values
36
+ @sections = @text.size
37
+ end
38
+
39
+ # Pretty object inspection
40
+ def inspect
41
+ %Q{#<Grit::Merge}
42
+ end
43
+ end # Merge
44
+
45
+ end # Grit
@@ -0,0 +1,294 @@
1
+ module Grit
2
+ # Grit::Process includes logic for executing child processes and
3
+ # reading/writing from their standard input, output, and error streams.
4
+ #
5
+ # Create an run a process to completion:
6
+ #
7
+ # >> process = Grit::Process.new(['git', '--help'])
8
+ #
9
+ # Retrieve stdout or stderr output:
10
+ #
11
+ # >> process.out
12
+ # => "usage: git [--version] [--exec-path[=GIT_EXEC_PATH]]\n ..."
13
+ # >> process.err
14
+ # => ""
15
+ #
16
+ # Check process exit status information:
17
+ #
18
+ # >> process.status
19
+ # => #<Process::Status: pid=80718,exited(0)>
20
+ #
21
+ # Grit::Process is designed to take all input in a single string and
22
+ # provides all output as single strings. It is therefore not well suited
23
+ # to streaming large quantities of data in and out of commands.
24
+ #
25
+ # Q: Why not use popen3 or hand-roll fork/exec code?
26
+ #
27
+ # - It's more efficient than popen3 and provides meaningful process
28
+ # hierarchies because it performs a single fork/exec. (popen3 double forks
29
+ # to avoid needing to collect the exit status and also calls
30
+ # Process::detach which creates a Ruby Thread!!!!).
31
+ #
32
+ # - It's more portable than hand rolled pipe, fork, exec code because
33
+ # fork(2) and exec(2) aren't available on all platforms. In those cases,
34
+ # Grit::Process falls back to using whatever janky substitutes the platform
35
+ # provides.
36
+ #
37
+ # - It handles all max pipe buffer hang cases, which is non trivial to
38
+ # implement correctly and must be accounted for with either popen3 or
39
+ # hand rolled fork/exec code.
40
+ class Process
41
+ # Create and execute a new process.
42
+ #
43
+ # argv - Array of [command, arg1, ...] strings to use as the new
44
+ # process's argv. When argv is a String, the shell is used
45
+ # to interpret the command.
46
+ # env - The new process's environment variables. This is merged with
47
+ # the current environment as if by ENV.merge(env).
48
+ # options - Additional options:
49
+ # :input => str to write str to the process's stdin.
50
+ # :timeout => int number of seconds before we given up.
51
+ # :max => total number of output bytes
52
+ # A subset of Process:spawn options are also supported on all
53
+ # platforms:
54
+ # :chdir => str to start the process in different working dir.
55
+ #
56
+ # Returns a new Process instance that has already executed to completion.
57
+ # The out, err, and status attributes are immediately available.
58
+ def initialize(argv, env={}, options={})
59
+ @argv = argv
60
+ @env = env
61
+
62
+ @options = options.dup
63
+ @input = @options.delete(:input)
64
+ @timeout = @options.delete(:timeout)
65
+ @max = @options.delete(:max)
66
+ @options.delete(:chdir) if @options[:chdir].nil?
67
+
68
+ exec!
69
+ end
70
+
71
+ # All data written to the child process's stdout stream as a String.
72
+ attr_reader :out
73
+
74
+ # All data written to the child process's stderr stream as a String.
75
+ attr_reader :err
76
+
77
+ # A Process::Status object with information on how the child exited.
78
+ attr_reader :status
79
+
80
+ # Total command execution time (wall-clock time)
81
+ attr_reader :runtime
82
+
83
+ # Determine if the process did exit with a zero exit status.
84
+ def success?
85
+ @status && @status.success?
86
+ end
87
+
88
+ private
89
+ # Execute command, write input, and read output. This is called
90
+ # immediately when a new instance of this object is initialized.
91
+ def exec!
92
+ # when argv is a string, use /bin/sh to interpret command
93
+ argv = @argv
94
+ argv = ['/bin/sh', '-c', argv.to_str] if argv.respond_to?(:to_str)
95
+
96
+ # spawn the process and hook up the pipes
97
+ pid, stdin, stdout, stderr = popen4(@env, *(argv + [@options]))
98
+
99
+ # async read from all streams into buffers
100
+ @out, @err = read_and_write(@input, stdin, stdout, stderr, @timeout, @max)
101
+
102
+ # grab exit status
103
+ @status = waitpid(pid)
104
+ rescue Object => boom
105
+ [stdin, stdout, stderr].each { |fd| fd.close rescue nil }
106
+ if @status.nil?
107
+ ::Process.kill('TERM', pid) rescue nil
108
+ @status = waitpid(pid) rescue nil
109
+ end
110
+ raise
111
+ end
112
+
113
+ # Exception raised when the total number of bytes output on the command's
114
+ # stderr and stdout streams exceeds the maximum output size (:max option).
115
+ class MaximumOutputExceeded < StandardError
116
+ end
117
+
118
+ # Exception raised when timeout is exceeded.
119
+ class TimeoutExceeded < StandardError
120
+ end
121
+
122
+ # Maximum buffer size for reading
123
+ BUFSIZE = (32 * 1024)
124
+
125
+ # Start a select loop writing any input on the child's stdin and reading
126
+ # any output from the child's stdout or stderr.
127
+ #
128
+ # input - String input to write on stdin. May be nil.
129
+ # stdin - The write side IO object for the child's stdin stream.
130
+ # stdout - The read side IO object for the child's stdout stream.
131
+ # stderr - The read side IO object for the child's stderr stream.
132
+ # timeout - An optional Numeric specifying the total number of seconds
133
+ # the read/write operations should occur for.
134
+ #
135
+ # Returns an [out, err] tuple where both elements are strings with all
136
+ # data written to the stdout and stderr streams, respectively.
137
+ # Raises TimeoutExceeded when all data has not been read / written within
138
+ # the duration specified in the timeout argument.
139
+ # Raises MaximumOutputExceeded when the total number of bytes output
140
+ # exceeds the amount specified by the max argument.
141
+ def read_and_write(input, stdin, stdout, stderr, timeout=nil, max=nil)
142
+ input ||= ''
143
+ max = nil if max && max <= 0
144
+ out, err = '', ''
145
+ offset = 0
146
+
147
+ timeout = nil if timeout && timeout <= 0.0
148
+ @runtime = 0.0
149
+ start = Time.now
150
+
151
+ writers = [stdin]
152
+ readers = [stdout, stderr]
153
+ t = timeout
154
+ while readers.any? || writers.any?
155
+ ready = IO.select(readers, writers, readers + writers, t)
156
+ raise TimeoutExceeded if ready.nil?
157
+
158
+ # write to stdin stream
159
+ ready[1].each do |fd|
160
+ begin
161
+ boom = nil
162
+ size = fd.write_nonblock(input)
163
+ input = input[size, input.size]
164
+ rescue Errno::EPIPE => boom
165
+ rescue Errno::EAGAIN, Errno::EINTR
166
+ end
167
+ if boom || input.size == 0
168
+ stdin.close
169
+ writers.delete(stdin)
170
+ end
171
+ end
172
+
173
+ # read from stdout and stderr streams
174
+ ready[0].each do |fd|
175
+ buf = (fd == stdout) ? out : err
176
+ begin
177
+ buf << fd.readpartial(BUFSIZE)
178
+ rescue Errno::EAGAIN, Errno::EINTR
179
+ rescue EOFError
180
+ readers.delete(fd)
181
+ fd.close
182
+ end
183
+ end
184
+
185
+ # keep tabs on the total amount of time we've spent here
186
+ @runtime = Time.now - start
187
+ if timeout
188
+ t = timeout - @runtime
189
+ raise TimeoutExceeded if t < 0.0
190
+ end
191
+
192
+ # maybe we've hit our max output
193
+ if max && ready[0].any? && (out.size + err.size) > max
194
+ raise MaximumOutputExceeded
195
+ end
196
+ end
197
+
198
+ [out, err]
199
+ end
200
+
201
+ # Spawn a child process, perform IO redirection and environment prep, and
202
+ # return the running process's pid.
203
+ #
204
+ # This method implements a limited subset of Ruby 1.9's Process::spawn.
205
+ # The idea is that we can just use that when available, since most platforms
206
+ # will eventually build in special (and hopefully good) support for it.
207
+ #
208
+ # env - Hash of { name => val } environment variables set in the child
209
+ # process.
210
+ # argv - New process's argv as an Array. When this value is a string,
211
+ # the command may be run through the system /bin/sh or
212
+ # options - Supports a subset of Process::spawn options, including:
213
+ # :chdir => str to change the directory to str in the child
214
+ # FD => :close to close a file descriptor in the child
215
+ # :in => FD to redirect child's stdin to FD
216
+ # :out => FD to redirect child's stdout to FD
217
+ # :err => FD to redirect child's stderr to FD
218
+ #
219
+ # Returns the pid of the new process as an integer. The process exit status
220
+ # must be obtained using Process::waitpid.
221
+ def spawn(env, *argv)
222
+ options = (argv.pop if argv[-1].kind_of?(Hash)) || {}
223
+ fork do
224
+ # { fd => :close } in options means close that fd
225
+ options.each { |k,v| k.close if v == :close && !k.closed? }
226
+
227
+ # reopen stdin, stdout, and stderr on provided fds
228
+ STDIN.reopen(options[:in])
229
+ STDOUT.reopen(options[:out])
230
+ STDERR.reopen(options[:err])
231
+
232
+ # setup child environment
233
+ env.each { |k, v| ENV[k] = v }
234
+
235
+ # { :chdir => '/' } in options means change into that dir
236
+ ::Dir.chdir(options[:chdir]) if options[:chdir]
237
+
238
+ # do the deed
239
+ ::Kernel::exec(*argv)
240
+ exit! 1
241
+ end
242
+ end
243
+
244
+ # Start a process with spawn options and return
245
+ # popen4([env], command, arg1, arg2, [opt])
246
+ #
247
+ # env - The child process's environment as a Hash.
248
+ # command - The command and zero or more arguments.
249
+ # options - An options hash.
250
+ #
251
+ # See Ruby 1.9 IO.popen and Process::spawn docs for more info:
252
+ # http://www.ruby-doc.org/core-1.9/classes/IO.html#M001640
253
+ #
254
+ # Returns a [pid, stdin, stderr, stdout] tuple where pid is the child
255
+ # process's pid, stdin is a writeable IO object, and stdout + stderr are
256
+ # readable IO objects.
257
+ def popen4(*argv)
258
+ # create some pipes (see pipe(2) manual -- the ruby docs suck)
259
+ ird, iwr = IO.pipe
260
+ ord, owr = IO.pipe
261
+ erd, ewr = IO.pipe
262
+
263
+ # spawn the child process with either end of pipes hooked together
264
+ opts =
265
+ ((argv.pop if argv[-1].is_a?(Hash)) || {}).merge(
266
+ # redirect fds # close other sides
267
+ :in => ird, iwr => :close,
268
+ :out => owr, ord => :close,
269
+ :err => ewr, erd => :close
270
+ )
271
+ pid = spawn(*(argv + [opts]))
272
+
273
+ [pid, iwr, ord, erd]
274
+ ensure
275
+ # we're in the parent, close child-side fds
276
+ [ird, owr, ewr].each { |fd| fd.close }
277
+ end
278
+
279
+ # Wait for the child process to exit
280
+ #
281
+ # Returns the Process::Status object obtained by reaping the process.
282
+ def waitpid(pid)
283
+ ::Process::waitpid(pid)
284
+ $?
285
+ end
286
+
287
+ # Use native Process::spawn implementation on Ruby 1.9.
288
+ if ::Process.respond_to?(:spawn)
289
+ def spawn(*argv)
290
+ ::Process.spawn(*argv)
291
+ end
292
+ end
293
+ end
294
+ end