siebertm-git_shelve 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,56 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+
6
+ Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
7
+
8
+ This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
9
+
10
+ 0. Additional Definitions.
11
+ As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
12
+
13
+ “The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
14
+
15
+ An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
16
+
17
+ A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
18
+
19
+ The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
20
+
21
+ The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
22
+
23
+ 1. Exception to Section 3 of the GNU GPL.
24
+ You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
25
+
26
+ 2. Conveying Modified Versions.
27
+ If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
28
+
29
+ a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
30
+ b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
31
+ 3. Object Code Incorporating Material from Library Header Files.
32
+ The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
33
+
34
+ a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
35
+ b) Accompany the object code with a copy of the GNU GPL and this license document.
36
+ 4. Combined Works.
37
+ You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
38
+
39
+ a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
40
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
41
+ c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
42
+ d) Do one of the following:
43
+ 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
44
+ 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
45
+ e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
46
+ 5. Combined Libraries.
47
+ You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
48
+
49
+ a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
50
+ b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
51
+ 6. Revised Versions of the GNU Lesser General Public License.
52
+ The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
53
+
54
+ Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
55
+
56
+ If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
data/README.textile ADDED
@@ -0,0 +1,86 @@
1
+ h1. GitShelve
2
+
3
+ GitShelve makes it possible to store arbitrary data in a separate branch
4
+ of a git repository
5
+ This class was inspired and is loosely based upon John Wiegley's
6
+ git_shelve.py and git-issues python scripts (http://github.com/jwiegley/git-issues/tree/master)
7
+
8
+ h2. Installation
9
+
10
+ You can either download the archive via "github":http://github.com/siebertm/git-shelve/tree/master
11
+ or install the git_shelve gem:
12
+
13
+ <pre>
14
+ sudo gem install gem install siebertm-git_shelve --source=http://gems.github.com
15
+ </pre>
16
+
17
+ h2. Basic Usage
18
+
19
+ <pre>
20
+ <code>
21
+ shelve = GitShelve::Shelve.new("mybranch", "/path/to/repository.git")
22
+ sha1 = shelve.put("some data")
23
+
24
+ shelve.get(sha1)
25
+ #-> "some data"
26
+
27
+ sha1 = shelve.put do |f|
28
+ # f is an IO object streaming directly into git!
29
+ f.write("i can ")
30
+ f.write("stream my data ")
31
+ f.write("in chunks!")
32
+ end
33
+
34
+ shelve.get(sha1)
35
+ #-> "i can stream my data in chunks!"
36
+
37
+ shelve.get(sha1) do |f|
38
+ # this works the same with get!
39
+ data = f.read
40
+ end
41
+
42
+ data
43
+ #-> "i can stream my data in chunks!"
44
+ </code>
45
+ </pre>
46
+
47
+ h2. Replication
48
+
49
+ GitShelve now supports replication, which is based on git's
50
+ distribution mechanisms (see git-fetch-pack and git-send-pack
51
+ manpages). Pulling and fentching from remote repositories is being
52
+ implemented by the GitShelve::ReplicatedShelve class, so you could
53
+ use the basic Shelve class if you don't need that stuff.
54
+
55
+ Since I decided to use just the plumbing, replication is not based on
56
+ git remotes, so you'll have to provide ReplicatedShelve with the remotes
57
+ "by hand".
58
+
59
+ Another thing that could cause errors is that you really should use the
60
+ same branch on all remotes. I did not test it with different branches.
61
+
62
+ <pre>
63
+ <code>
64
+ shelve = GitShelve::ReplicatedShelve.new("mybranch", "/path/to/repository")
65
+ shelve.add_remote("git@example.com:remote_repo")
66
+
67
+ # get automatically fetches from remotes if it can't find an object,
68
+ # so this will pull from the remote repository
69
+ data = shelve.get("37295dbe4bb6d980d2d8ce2dc16bfc29ab56526e")
70
+
71
+ # you could also use this
72
+ shelve.fetch
73
+ data = shelve.get("37295dbe4bb6d980d2d8ce2dc16bfc29ab56526e")
74
+
75
+ # now lets create some data and push it to the server
76
+ shelve.put("some data")
77
+ shelve.push
78
+ </code>
79
+ </pre>
80
+
81
+ h2. Author
82
+
83
+ Michael Siebert <"siebertm85@googlemail.com":siebertm85@googlemail.com>
84
+
85
+ This piece of software wouldn't be possible without Git. Thanks
86
+ go out to the people who invented git!
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "git_shelve"
3
+ s.version = "0.1"
4
+ s.date = "2008-05-18"
5
+ s.summary = "Store arbitrary data in a git repository"
6
+ s.authors = ["Michael Siebert"]
7
+ s.email = "siebertm85@googlemail.com"
8
+ s.homepage = "http://www.siebert-wd.de/projects/git-shelve"
9
+ s.description = "GitShelve makes it possible to store arbitrary data in a separate branch of a git repository"
10
+ s.has_rdoc = true
11
+ s.files = %w(git_shelve.gemspec LICENSE README.textile lib/git_shelve.rb lib/git_shelve/shelve.rb lib/git_shelve/replicated_shelve.rb spec/all_git_shelves.rb spec/spec_helper.rb spec/unit/gitshelve_spec.rb spec/unit/replicated_shelve_spec.rb)
12
+ s.test_files = %w(spec/all_git_shelves.rb spec/spec_helper.rb spec/unit/gitshelve_spec.rb spec/unit/replicated_shelve_spec.rb)
13
+ s.rdoc_options = ["--main", "README.textile"]
14
+ s.extra_rdoc_files = ["LICENSE", "README.textile"]
15
+ end
data/lib/git_shelve.rb ADDED
@@ -0,0 +1,5 @@
1
+ module GitShelve
2
+ end
3
+
4
+ require File.join(File.dirname(__FILE__), "git_shelve", "shelve")
5
+ require File.join(File.dirname(__FILE__), "git_shelve", "replicated_shelve")
@@ -0,0 +1,100 @@
1
+ module GitShelve
2
+ class ReplicatedShelve < Shelve
3
+ def initialize(branch, repo = ".", remotes = [])
4
+ @remotes = remotes
5
+
6
+ super(branch, repo)
7
+ end
8
+
9
+
10
+ attr_reader :remotes
11
+
12
+ # Add a remote repository to fetch from or push to
13
+ #
14
+ # ==== Parameters
15
+ # pathspec<String>:: the path to the repository. see http://www.kernel.org/pub/software/scm/git/docs/git-push.html#URLS for URLs git should understand
16
+ #
17
+ # ==== Raises
18
+ # ArgumentError:: when the pathspec is empty
19
+ def add_remote(pathspec)
20
+ raise ArgumentError("pathspec must be valid! (pathspec=#{pathspec.inspect})") if pathspec.nil? || pathspec.empty?
21
+ @remotes << pathspec unless @remotes.include?(pathspec)
22
+ end
23
+
24
+ # tries to get the object with the given sha1.
25
+ # if it does not exist and pull_before is false, it pulls the changes
26
+ # and tries again
27
+ #
28
+ # ==== Parameters
29
+ # sha1<String>:: the SHA1 Hash of the object
30
+ # pull_before<Boolean>:: if true, it pulls from the remote before
31
+ # trying to get the object (do this if you know
32
+ # the objects not local). defaults to false
33
+ # &block:: if you supply a block, it gets yield with the IO object so you can read data directly from that stream
34
+ #
35
+ # ==== Raises
36
+ # ObjectNotFoundError:: if the object cannot be found
37
+ #
38
+ # ==== Returns
39
+ # String:: the object's data if you didn't supply a block, otherwise nothing
40
+ def get_with_fetch(sha1, allow_fetch = true, &block)
41
+ do_fetch = false unless do_fetch
42
+
43
+ if do_fetch
44
+ fetch
45
+ end
46
+
47
+ get_without_fetch(sha1, &block)
48
+ rescue ObjectNotFoundError
49
+ if allow_fetch && do_fetch == false
50
+ do_fetch = true
51
+ retry
52
+ else
53
+ raise
54
+ end
55
+ end
56
+ alias_method :get_without_fetch, :get
57
+ alias_method :get, :get_with_fetch
58
+
59
+ # fetches data from all remote repositories
60
+ def fetch
61
+ remotes.each do |remote|
62
+ fetch_from(remote)
63
+ end
64
+ end
65
+
66
+ # sends its data to all remote repositories
67
+ def push
68
+ remotes.each do |remote|
69
+ fetch_from(remote)
70
+ push_to(remote)
71
+ end
72
+ end
73
+
74
+
75
+ protected
76
+
77
+ def fetch_from(remote)
78
+ git('fetch-pack', '--no-progress', remote, @branch, '2>/dev/null')
79
+
80
+ # get the head
81
+ commit = git('ls-remote', '--heads', remote, @branch).split("\t").first
82
+
83
+ headname = headname_for(remote)
84
+ if git_status('rev-parse', headname, '2>&1') == 0
85
+ git('update-ref', headname, commit, headname)
86
+ else
87
+ git('update-ref', headname, commit)
88
+ end
89
+ end
90
+
91
+ def push_to(remote)
92
+ git('send-pack', '--verbose', remote, "#{@branch}:#{headname_for(remote)}", '2>&1')
93
+ end
94
+
95
+ def headname_for(remote)
96
+ "refs/heads/#{remote.gsub(/[^a-zA-Z0-9_-]/, "_")}"
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,266 @@
1
+ # ==== GitShelve::Shelve
2
+ # GitShelve makes it possible to store arbitrary data in a separate branch
3
+ # of a git repository
4
+ # This class was inspired and is loosely based upon John Wiegley's
5
+ # git_shelve.py and git-issues python scripts (http://github.com/jwiegley/git-issues/tree/master)
6
+ #
7
+ # === Example Usage
8
+ #
9
+ # shelve = GitShelve.new("mybranch", "/path/to/repository.git")
10
+ # sha1 = shelve.put("some data")
11
+ #
12
+ # shelve.get(sha1)
13
+ # #-> "some data"
14
+ #
15
+ # sha1 = shelve.put do |f|
16
+ # # f is an IO object streaming directly into git!
17
+ # f.write("i can ")
18
+ # f.write("stream my data ")
19
+ # f.write("in chunks!")
20
+ # end
21
+ #
22
+ # shelve.get(sha1)
23
+ # #-> "i can stream my data in chunks!"
24
+ #
25
+ # shelve.get(sha1) do |f|
26
+ # # this works the same with get!
27
+ # data = f.read
28
+ # end
29
+ #
30
+ # data
31
+ # #-> "i can stream my data in chunks!"
32
+ #
33
+ # === Author
34
+ # Michael Siebert <siebertm85@googlemail.com>
35
+ #
36
+ # This piece of software wouldn't be possible without Git. Thanks
37
+ # go out to the people who invented git!
38
+ module GitShelve
39
+ class Shelve
40
+
41
+ # Creates and initilializes a new Gitshelve object
42
+ #
43
+ # ==== Parameters
44
+ # branch<String>:: the name of the branch to store objects in
45
+ # repo<String>:: the path to the git repository to work on. Defaults to the current directory
46
+ def initialize(branch, repo = ".")
47
+ @branch = branch
48
+ @repository = File.expand_path(repo)
49
+ @head = nil
50
+ end
51
+
52
+ # get a file from the repository
53
+ #
54
+ # ==== Parameters
55
+ # sha1<String>:: the SHA1 of the object to retrieve
56
+ # &block:: if you supply a block, it gets yield with the IO object so you can read data directly from that stream
57
+ #
58
+ # ==== Raises
59
+ # ObjectNotFoundError:: when the requested object is not in the repository
60
+ #
61
+ # ==== Returns
62
+ # String:: the object's data if you didn't supply a block, otherwise nothing
63
+ def get(sha1, &block)
64
+ if git_status('cat-file', '-e', sha1) != 0
65
+ raise ObjectNotFoundError.new(sha1)
66
+ end
67
+
68
+ git('cat-file', 'blob', sha1, :strip => false, &block)
69
+ end
70
+
71
+ # saves data to the repository
72
+ #
73
+ # ==== Parameters
74
+ # data<String>:: the data to store
75
+ # &block:: if you supply a block, it gets yield with the IO object so you can write data directly to that stream
76
+ #
77
+ # ==== Returns
78
+ # String:: the SHA1 of the saved data (keep this!)
79
+ def put(data = nil, &block)
80
+ # write blob into repo
81
+ sha1 = git('hash-object', '-w', "--stdin", :input => data, &block)
82
+ sha1_first = sha1[0..1]
83
+ sha1_last = sha1[2..-1]
84
+
85
+ # Merge this blob with existing issue blobs that share the same
86
+ # first two hash digits
87
+ ls_tree = git('ls-tree', '-r', current_head, '--', sha1_first)
88
+ ls_tree.gsub!(/\t#{sha1_first}\//, "\t")
89
+ ls_tree.gsub!(/100644 blob #{sha1}\t#{sha1_last}/, '')
90
+ ls_tree += "\n" unless ls_tree.empty?
91
+
92
+ tree = git('mktree', :input => "#{ls_tree}100644 blob #{sha1}\t#{sha1_last}\n")
93
+
94
+ # Merge it into the tree of issues overall
95
+ ls_tree = git('ls-tree', current_head)
96
+ ls_tree.gsub!(/040000 tree [0-9a-f]{40}\t#{sha1_first}\n/, '')
97
+ ls_tree += "\n" unless ls_tree.empty?
98
+
99
+
100
+ tree = git('mktree', :input => "#{ls_tree}040000 tree #{tree}\t#{sha1_first}\n")
101
+
102
+ # Commit the merged tree (though at this moment it's a dangling commit)
103
+ commit = git('commit-tree', tree, '-p', current_head, :input => "Added #{sha1}")
104
+
105
+ # Update the HEAD of the branch to point to the commit we
106
+ # just made.
107
+ update_head(commit)
108
+
109
+ sha1
110
+ end
111
+
112
+ # does a git-fsck
113
+ #
114
+ # ==== Parameters
115
+ # *args:: String arguments to git-fsck
116
+ #
117
+ # ==== Returns
118
+ # String:: whatever git-fsck prints to stdout
119
+ def fsck(*args)
120
+ args << '2>&1'
121
+ git('fsck', *args)
122
+ end
123
+
124
+ protected
125
+
126
+ # returns the sha1 of the branch
127
+ # ==== Returns
128
+ # String:: SHA1-Hash
129
+ def current_head
130
+ git("rev-parse", @branch, "2>&1")
131
+ rescue GitError
132
+ create_branch
133
+ end
134
+
135
+ # Make the current branch point to the new head
136
+ # ==== Parameters
137
+ # new_head<SHA1>
138
+ #
139
+ # ==== Returns
140
+ # SHA1:: new_head
141
+ def update_head(new_head)
142
+ if @head
143
+ git('update-ref', 'refs/heads/%s' % @branch, new_head, @head)
144
+ else
145
+ git('update-ref', 'refs/heads/%s' % @branch, new_head)
146
+ end
147
+
148
+ @head = new_head
149
+ end
150
+
151
+ # try smart when creating the branch.
152
+ # first checks if there is a remote branch and uses this if there is.
153
+ # otherwise, creates a commit and from there the branch
154
+ #
155
+ # ==== Returns
156
+ # String:: the SHA1 hash of the branches HEAD
157
+ def create_branch
158
+ begin
159
+ hash = git('rev-parse', "origin/#{@branch}", "2>&1")
160
+ rescue GitError
161
+ hash = git('hash-object', '-w', '--stdin', :input => "Created #{@branch} branch\n")
162
+ hash = git('mktree', :input => "100644 blob #{hash}\tproject\n")
163
+ hash = git('commit-tree', hash, :input => "created #{@branch} branch")
164
+ end
165
+
166
+ git('branch', @branch, hash)
167
+ hash
168
+ end
169
+
170
+ # passes the command over to git
171
+ #
172
+ # ==== Parameters
173
+ # cmd<String>:: the git command to execute
174
+ # *rest:: any number of String arguments to the command, followed by an options hash
175
+ # &block:: if you supply a block, you can communicate with git throught a pipe. NEVER even think about closing the stream!
176
+ #
177
+ # ==== Options
178
+ # :strip<Boolean>:: true to strip the output String#strip, false not to to it
179
+ #
180
+ # ==== Raises
181
+ # GitError:: if git returns non-null, an Exception is raised
182
+ #
183
+ # ==== Returns
184
+ # String:: if you didn't supply a block, the things git said on STDOUT, otherwise noting
185
+ def git(cmd, *rest, &block)
186
+ result, status = run_git(cmd, *rest, &block)
187
+
188
+ if status != 0
189
+ raise GitError.new("Error: #{cmd} returned #{status}. Result: #{result}")
190
+ end
191
+ result
192
+ end
193
+
194
+
195
+ # passes the command over to git and returns its status ($?)
196
+ #
197
+ # ==== Parameters
198
+ # cmd<String>:: the git command to execute
199
+ # *rest:: any number of String arguments to the command, followed by an options hash
200
+ # &block:: if you supply a block, you can communicate with git throught a pipe. NEVER even think about closing the stream!
201
+ #
202
+ # ==== Returns
203
+ # Integer:: the return status of git
204
+ def git_status(cmd, *rest, &block)
205
+ run_git(cmd, *rest, &block)[1]
206
+ end
207
+
208
+ # passes the command over to git (you should not call this directly)
209
+ #
210
+ # ==== Parameters
211
+ # cmd<String>:: the git command to execute
212
+ # *rest:: any number of String arguments to the command, followed by an options hash
213
+ # &block:: if you supply a block, you can communicate with git throught a pipe. NEVER even think about closing the stream!
214
+ #
215
+ # ==== Options
216
+ # :strip<Boolean>:: true to strip the output String#strip, false not to to it
217
+ #
218
+ # ==== Raises
219
+ # GitError:: if git returns non-null, an Exception is raised
220
+ #
221
+ # ==== Returns
222
+ # Array[String, Integer]:: the first item is the STDOUT of git, the second is the return-status
223
+ def run_git(cmd, *rest, &block)
224
+ if rest.last.kind_of?(Hash)
225
+ options = rest.last
226
+ args = rest[0..-2]
227
+ else
228
+ options = {}
229
+ args = rest
230
+ end
231
+
232
+ options[:strip] = true unless options.key?(:strip)
233
+
234
+ ENV["GIT_DIR"] = @repository
235
+ cmd = "git-#{cmd} #{args.join(' ')}"
236
+
237
+ result = ""
238
+ IO.popen(cmd, "w+") do |f|
239
+ if input = options.delete(:input)
240
+ f.write(input)
241
+ f.close_write
242
+ elsif block_given?
243
+ yield f
244
+ f.close_write
245
+ end
246
+
247
+ result = ""
248
+
249
+ while !f.eof
250
+ result << f.read
251
+ end
252
+ end
253
+ status = $?
254
+
255
+ result.strip! if options[:strip] == true
256
+
257
+ [result, status]
258
+ end
259
+ end
260
+
261
+ class GitError < Exception
262
+ end
263
+
264
+ class ObjectNotFoundError < Exception
265
+ end
266
+ end
@@ -0,0 +1,47 @@
1
+ describe "All GitShelves", :shared => true do
2
+ it "should return an sha1 hash when saving a blob" do
3
+ @shelve.put("hallo").length.should == 40
4
+ end
5
+
6
+ it "should be able to retrieve the data of an existing sha1 hash" do
7
+ sha1 = @shelve.put("hallo test\n123\n")
8
+
9
+ @shelve.get(sha1).should == "hallo test\n123\n"
10
+ end
11
+
12
+ it "should be able to retrieve a (large) file in chunks by supplying a block" do
13
+ orig_data = "hallo test\n123\n"*2000
14
+ sha1 = @shelve.put(orig_data)
15
+
16
+ data = ""
17
+ @shelve.get(sha1) do |f|
18
+ while !f.eof
19
+ data << f.read(200)
20
+ end
21
+ end
22
+
23
+ data.should == orig_data
24
+ end
25
+
26
+ it "should be able to store a (large) file in chunks by supplying a block" do
27
+ chunk = "hallo test\n123\n"
28
+
29
+ sha1 = @shelve.put do |f|
30
+ 2000.times do
31
+ f.write(chunk)
32
+ end
33
+ end
34
+
35
+ @shelve.get(sha1).should == chunk*2000
36
+ end
37
+
38
+ it "should return the same sha1 hash when another object with the same contents is stored" do
39
+ sha1 = @shelve.put("hallo test\n123\n")
40
+ sha2 = @shelve.put("hallo test\n123\n")
41
+
42
+ sha1.should == sha2
43
+ @shelve.fsck.should_not match(/dangling/)
44
+ @shelve.fsck.should_not match(/error/)
45
+
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ require "rubygems"
2
+ require "spec"
3
+ require "mocha"
4
+
5
+ require "lib/git_shelve"
6
+ require File.dirname(__FILE__) + "/all_git_shelves"
7
+
8
+ module GitShelveSpecHelper
9
+ def setup_repo(path)
10
+ `mkdir -p #{path} && GIT_DIR=#{path} git --bare init`
11
+ end
12
+
13
+
14
+ def teardown_repo(path)
15
+ `rm -rf #{path}`
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper.rb"
2
+
3
+ describe GitShelve::Shelve do
4
+ include GitShelveSpecHelper
5
+
6
+ before(:each) do
7
+ @path = File.expand_path(File.dirname(__FILE__) + "/tmpgit")
8
+ setup_repo(@path)
9
+
10
+ @shelve = GitShelve::Shelve.new("mybranch", @path)
11
+ end
12
+
13
+ after(:each) do
14
+ teardown_repo(@path)
15
+ end
16
+
17
+ it_should_behave_like "All GitShelves"
18
+
19
+ describe ".get" do
20
+ it "should raise ObjectNotFoundError when it could not find the specified object" do
21
+ lambda {
22
+ @shelve.get("e30b41f7fbe195d45e702f3cafd0f48ab8d62a50")
23
+ }.should raise_error(GitShelve::ObjectNotFoundError)
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,108 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper.rb"
2
+
3
+ describe GitShelve::ReplicatedShelve do
4
+ include GitShelveSpecHelper
5
+
6
+ before(:each) do
7
+ @path1 = File.expand_path(File.dirname(__FILE__) + "/tmpgit1")
8
+ @path2 = File.expand_path(File.dirname(__FILE__) + "/tmpgit2")
9
+
10
+ setup_repo(@path1)
11
+ setup_repo(@path2)
12
+
13
+ @shelve = GitShelve::ReplicatedShelve.new("mybranch", @path1)
14
+ @shelve2 = GitShelve::ReplicatedShelve.new("mybranch", @path2)
15
+ end
16
+
17
+ after(:each) do
18
+ teardown_repo(@path1)
19
+ teardown_repo(@path2)
20
+ end
21
+
22
+ it_should_behave_like "All GitShelves"
23
+
24
+ describe ".add_remote" do
25
+ it "should add a remote" do
26
+ @shelve.remotes.should be_empty
27
+ @shelve.add_remote("pathspec")
28
+ @shelve.remotes.should include("pathspec")
29
+ end
30
+ end
31
+
32
+ describe ".fetch" do
33
+ it "should fetch objects from a given remote repository" do
34
+ @shelve.add_remote(@path2)
35
+
36
+ # try 3 times to be sure it works :)
37
+ 3.times do |n|
38
+ sha1 = @shelve2.put("this is data number #{n} from remote")
39
+ sha2 = @shelve.put("this is data number #{n} from me")
40
+
41
+ # make sure our object is not already in our "local" repo
42
+ lambda { @shelve.get_without_fetch(sha1) }.should raise_error(GitShelve::ObjectNotFoundError)
43
+
44
+ @shelve.fetch
45
+
46
+ lambda { @shelve.get_without_fetch(sha1) }.should_not raise_error(GitShelve::ObjectNotFoundError)
47
+
48
+ @shelve.get_without_fetch(sha1).should == "this is data number #{n} from remote"
49
+ @shelve.get_without_fetch(sha2).should == "this is data number #{n} from me"
50
+ end
51
+
52
+ @shelve.fsck.should_not match(/dangling/)
53
+ @shelve.fsck.should_not match(/error/)
54
+ end
55
+ end
56
+
57
+ describe ".push" do
58
+ it "should first call fetch, then push" do
59
+ @shelve.add_remote(@path2)
60
+ @shelve2.add_remote(@path1)
61
+
62
+ @shelve2.should_receive(:fetch_from).with(@path1).once
63
+ @shelve2.should_receive(:push_to).with(@path1).once
64
+ @shelve2.push
65
+ end
66
+
67
+ it "should send objects to a remote repository" do
68
+ @shelve.add_remote(@path2)
69
+ @shelve2.add_remote(@path1)
70
+
71
+ # try 3 times to be sure it works :)
72
+ 3.times do |n|
73
+ sha1 = @shelve2.put("this is data number #{n} from remote")
74
+ sha2 = @shelve.put("this is data number #{n} from me")
75
+
76
+ # make sure our object is not already in our "local" repo
77
+ lambda { @shelve.get_without_fetch(sha1) }.should raise_error(GitShelve::ObjectNotFoundError)
78
+
79
+ @shelve2.push
80
+
81
+ lambda { @shelve.get_without_fetch(sha1) }.should_not raise_error(GitShelve::ObjectNotFoundError)
82
+
83
+ @shelve.get_without_fetch(sha1).should == "this is data number #{n} from remote"
84
+ @shelve.get_without_fetch(sha2).should == "this is data number #{n} from me"
85
+ end
86
+
87
+ @shelve.fsck.should_not match(/dangling/)
88
+ @shelve.fsck.should_not match(/error/)
89
+ end
90
+ end
91
+
92
+ describe ".get" do
93
+ it "should try to fetch when it doesn't find the specified objct locally" do
94
+ @shelve.should_receive(:fetch)
95
+ first_time = true
96
+ @shelve.stub!(:get_without_fetch).and_return {
97
+ if first_time
98
+ first_time = false
99
+ raise GitShelve::ObjectNotFoundError
100
+ else
101
+ "test data"
102
+ end
103
+ }
104
+
105
+ @shelve.get("20eda216cefdf21109f8306a5138a820956fd0f3").should == "test data"
106
+ end
107
+ end
108
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siebertm-git_shelve
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Michael Siebert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: GitShelve makes it possible to store arbitrary data in a separate branch of a git repository
17
+ email: siebertm85@googlemail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.textile
25
+ files:
26
+ - git_shelve.gemspec
27
+ - LICENSE
28
+ - README.textile
29
+ - lib/git_shelve.rb
30
+ - lib/git_shelve/shelve.rb
31
+ - lib/git_shelve/replicated_shelve.rb
32
+ - spec/all_git_shelves.rb
33
+ - spec/spec_helper.rb
34
+ - spec/unit/gitshelve_spec.rb
35
+ - spec/unit/replicated_shelve_spec.rb
36
+ has_rdoc: true
37
+ homepage: http://www.siebert-wd.de/projects/git-shelve
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --main
41
+ - README.textile
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.0.1
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Store arbitrary data in a git repository
63
+ test_files:
64
+ - spec/all_git_shelves.rb
65
+ - spec/spec_helper.rb
66
+ - spec/unit/gitshelve_spec.rb
67
+ - spec/unit/replicated_shelve_spec.rb