Skip to content

Commit 5f3699a

Browse files
Moving Git::Base factory methods into a module
1 parent 0fffb17 commit 5f3699a

File tree

2 files changed

+114
-98
lines changed

2 files changed

+114
-98
lines changed

lib/git/base.rb

Lines changed: 59 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1+
require 'git/base/factories'
2+
13
module Git
24

35
class Base
46

7+
include Git::Base::Factories
8+
59
# opens a bare Git Repository - no working directory options
610
def self.bare(git_dir, opts = {})
711
self.new({:repository => git_dir}.merge(opts))
812
end
913

10-
# opens a new Git Project from a working directory
11-
# you can specify non-standard git_dir and index file in the options
12-
def self.open(working_dir, opts={})
13-
self.new({:working_directory => working_dir}.merge(opts))
14+
# clones a git repository locally
15+
#
16+
# repository - http://repo.or.cz/w/sinatra.git
17+
# name - sinatra
18+
#
19+
# options:
20+
# :repository
21+
#
22+
# :bare
23+
# or
24+
# :working_directory
25+
# :index_file
26+
#
27+
def self.clone(repository, name, opts = {})
28+
# run git-clone
29+
self.new(Git::Lib.new.clone(repository, name, opts))
30+
end
31+
32+
# Returns (and initialize if needed) a Git::Config instance
33+
#
34+
# @return [Git::Config] the current config instance.
35+
def self.config
36+
return @@config ||= Config.new
1437
end
1538

1639
# initializes a git repository
@@ -36,25 +59,13 @@ def self.init(working_dir, opts = {})
3659

3760
self.new(opts)
3861
end
39-
40-
# clones a git repository locally
41-
#
42-
# repository - http://repo.or.cz/w/sinatra.git
43-
# name - sinatra
44-
#
45-
# options:
46-
# :repository
47-
#
48-
# :bare
49-
# or
50-
# :working_directory
51-
# :index_file
52-
#
53-
def self.clone(repository, name, opts = {})
54-
# run git-clone
55-
self.new(Git::Lib.new.clone(repository, name, opts))
62+
63+
# opens a new Git Project from a working directory
64+
# you can specify non-standard git_dir and index file in the options
65+
def self.open(working_dir, opts={})
66+
self.new({:working_directory => working_dir}.merge(opts))
5667
end
57-
68+
5869
def initialize(options = {})
5970
if working_dir = options[:working_directory]
6071
options[:repository] ||= File.join(working_dir, '.git')
@@ -71,36 +82,6 @@ def initialize(options = {})
7182
@repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil
7283
@index = options[:index] ? Git::Index.new(options[:index], false) : nil
7384
end
74-
75-
76-
# returns a reference to the working directory
77-
# @git.dir.path
78-
# @git.dir.writeable?
79-
def dir
80-
@working_directory
81-
end
82-
83-
# returns reference to the git repository directory
84-
# @git.dir.path
85-
def repo
86-
@repository
87-
end
88-
89-
# returns reference to the git index file
90-
def index
91-
@index
92-
end
93-
94-
95-
def set_working(work_dir, check = true)
96-
@lib = nil
97-
@working_directory = Git::WorkingDirectory.new(work_dir.to_s, check)
98-
end
99-
100-
def set_index(index_file, check = true)
101-
@lib = nil
102-
@index = Git::Index.new(index_file.to_s, check)
103-
end
10485

10586
# changes current working directory for a block
10687
# to the git working directory
@@ -117,13 +98,6 @@ def chdir # :yields: the Git::Path
11798
end
11899
end
119100

120-
# returns the repository size in bytes
121-
def repo_size
122-
Dir.chdir(repo.path) do
123-
return `du -s`.chomp.split.first.to_i
124-
end
125-
end
126-
127101
#g.config('user.name', 'Scott Chacon') # sets value
128102
#g.config('user.email', 'email@email.com') # sets value
129103
#g.config('user.name') # returns 'Scott Chacon'
@@ -140,56 +114,43 @@ def config(name = nil, value = nil)
140114
lib.config_list
141115
end
142116
end
143-
144-
def self.config
145-
return @@config ||= Config.new
117+
118+
# returns a reference to the working directory
119+
# @git.dir.path
120+
# @git.dir.writeable?
121+
def dir
122+
@working_directory
146123
end
147124

148-
# factory methods
149-
150-
# returns a Git::Object of the appropriate type
151-
# you can also call @git.gtree('tree'), but that's
152-
# just for readability. If you call @git.gtree('HEAD') it will
153-
# still return a Git::Object::Commit object.
154-
#
155-
# @git.object calls a factory method that will run a rev-parse
156-
# on the objectish and determine the type of the object and return
157-
# an appropriate object for that type
158-
def object(objectish)
159-
Git::Object.new(self, objectish)
125+
# returns reference to the git index file
126+
def index
127+
@index
160128
end
161-
162-
def gtree(objectish)
163-
Git::Object.new(self, objectish, 'tree')
129+
130+
# returns reference to the git repository directory
131+
# @git.dir.path
132+
def repo
133+
@repository
164134
end
165135

166-
def gcommit(objectish)
167-
Git::Object.new(self, objectish, 'commit')
136+
# returns the repository size in bytes
137+
def repo_size
138+
Dir.chdir(repo.path) do
139+
return `du -s`.chomp.split.first.to_i
140+
end
168141
end
169142

170-
def gblob(objectish)
171-
Git::Object.new(self, objectish, 'blob')
143+
def set_index(index_file, check = true)
144+
@lib = nil
145+
@index = Git::Index.new(index_file.to_s, check)
172146
end
173147

174-
# returns a Git::Log object with count commits
175-
def log(count = 30)
176-
Git::Log.new(self, count)
148+
def set_working(work_dir, check = true)
149+
@lib = nil
150+
@working_directory = Git::WorkingDirectory.new(work_dir.to_s, check)
177151
end
178152

179-
# returns a Git::Status object
180-
def status
181-
Git::Status.new(self)
182-
end
183-
184-
# returns a Git::Branches object of all the Git::Branch objects for this repo
185-
def branches
186-
Git::Branches.new(self)
187-
end
188-
189-
# returns a Git::Branch object for branch_name
190-
def branch(branch_name = 'master')
191-
Git::Branch.new(self, branch_name)
192-
end
153+
193154

194155
# returns +true+ if the branch exists locally
195156
def is_local_branch?(branch)

lib/git/base/factories.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module Git
2+
3+
class Base
4+
5+
module Factories
6+
7+
# returns a Git::Object of the appropriate type
8+
# you can also call @git.gtree('tree'), but that's
9+
# just for readability. If you call @git.gtree('HEAD') it will
10+
# still return a Git::Object::Commit object.
11+
#
12+
# @git.object calls a factory method that will run a rev-parse
13+
# on the objectish and determine the type of the object and return
14+
# an appropriate object for that type
15+
def object(objectish)
16+
Git::Object.new(self, objectish)
17+
end
18+
19+
def gtree(objectish)
20+
Git::Object.new(self, objectish, 'tree')
21+
end
22+
23+
def gcommit(objectish)
24+
Git::Object.new(self, objectish, 'commit')
25+
end
26+
27+
def gblob(objectish)
28+
Git::Object.new(self, objectish, 'blob')
29+
end
30+
31+
# returns a Git::Log object with count commits
32+
def log(count = 30)
33+
Git::Log.new(self, count)
34+
end
35+
36+
# returns a Git::Status object
37+
def status
38+
Git::Status.new(self)
39+
end
40+
41+
# returns a Git::Branches object of all the Git::Branch objects for this repo
42+
def branches
43+
Git::Branches.new(self)
44+
end
45+
46+
# returns a Git::Branch object for branch_name
47+
def branch(branch_name = 'master')
48+
Git::Branch.new(self, branch_name)
49+
end
50+
51+
end
52+
53+
end
54+
55+
end

0 commit comments

Comments
 (0)