Skip to content

Commit 79b72dd

Browse files
Making the project setup easier (Gemfile and development_dependencies)
Updating tests (some of them were based on dates, no longer valids) Updating branch naming policy (Git 1.7.x) Extending the Branches #[] to return the target repo given the full or any of the shorter names
1 parent a308cc9 commit 79b72dd

File tree

11 files changed

+90
-28
lines changed

11 files changed

+90
-28
lines changed

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ PATH
66
GEM
77
remote: https://rubygems.org/
88
specs:
9+
columnize (0.3.6)
910
json (1.7.7)
11+
linecache (0.46)
12+
rbx-require-relative (> 0.0.4)
13+
rbx-require-relative (0.0.9)
1014
rdoc (4.0.1)
1115
json (~> 1.4)
16+
ruby-debug (0.10.4)
17+
columnize (>= 0.1)
18+
ruby-debug-base (~> 0.10.4.0)
19+
ruby-debug-base (0.10.4)
20+
linecache (>= 0.3)
1221
ruby-prof (0.13.0)
1322
test-unit (2.5.4)
1423

@@ -18,5 +27,6 @@ PLATFORMS
1827
DEPENDENCIES
1928
git!
2029
rdoc
30+
ruby-debug
2131
ruby-prof
2232
test-unit

git.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
1515
s.require_paths = ['lib']
1616
s.requirements = ['git 1.6.0.0, or greater']
1717

18+
s.add_development_dependency 'ruby-debug'
1819
s.add_development_dependency 'rdoc'
1920
s.add_development_dependency 'ruby-prof'
2021
s.add_development_dependency 'test-unit'

lib/git/branch.rb

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1+
require 'git/path'
2+
13
module Git
4+
25
class Branch < Path
36

47
attr_accessor :full, :remote, :name
58

69
def initialize(base, name)
7-
@remote = nil
810
@full = name
911
@base = base
1012
@gcommit = nil
1113
@stashes = nil
12-
13-
parts = name.split('/')
14-
if parts[1]
15-
@remote = Git::Remote.new(@base, parts[0])
16-
@name = parts[1]
17-
else
18-
@name = parts[0]
19-
end
14+
@remote, @name = parse_name(name)
2015
end
2116

2217
def gcommit
@@ -100,5 +95,28 @@ def determine_current
10095
@base.lib.branch_current == @name
10196
end
10297

98+
# Given a full branch name return an Array containing the remote and branch names.
99+
#
100+
# Removes 'remotes' from the beggining of the name (if present).
101+
# Takes the second part (splittign by '/') as the remote name.
102+
# Takes the rest as the repo name (can also hold one or more '/').
103+
#
104+
# Example:
105+
# parse_name('master') #=> [nil, 'master']
106+
# parse_name('origin/master') #=> ['origin', 'master']
107+
# parse_name('remotes/origin/master') #=> ['origin', 'master']
108+
# parse_name('origin/master/v2') #=> ['origin', 'master/v2']
109+
#
110+
# param [String] name branch full name.
111+
# return [<Git::Remote,NilClass,String>] an Array containing the remote and branch names.
112+
def parse_name(name)
113+
if name.match /^(?:remotes)?\/([^\/]+)\/(.+)/
114+
return [Git::Remote.new(@base, $1), $2]
115+
end
116+
117+
return [nil, name]
118+
end
119+
103120
end
121+
104122
end

lib/git/branches.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Git
22

33
# object that holds all the available branches
44
class Branches
5+
56
include Enumerable
67

78
def initialize(base)
@@ -32,8 +33,29 @@ def each(&block)
3233
@branches.values.each(&block)
3334
end
3435

35-
def [](symbol)
36-
@branches[symbol.to_s]
36+
# Returns the target branch
37+
#
38+
# Example:
39+
# Given (git branch -a):
40+
# master
41+
# remotes/working/master
42+
#
43+
# g.branches['master'].full #=> 'master'
44+
# g.branches['working/master'].full => 'remotes/working/master'
45+
# g.branches['remotes/working/master'].full => 'remotes/working/master'
46+
#
47+
# @param [#to_s] branch_name the target branch name.
48+
# @return [Git::Branch] the target branch.
49+
def [](branch_name)
50+
@branches.values.inject(@branches) do |branches, branch|
51+
branches[branch.full] ||= branch
52+
53+
# This is how Git (version 1.7.9.5) works.
54+
# Lets you ignore the 'remotes' if its at the beginning of the branch full name (even if is not a real remote branch).
55+
branches[branch.full.sub('remotes/', '')] ||= branch if branch.full =~ /^remotes\/.+/
56+
57+
branches
58+
end[branch_name.to_s]
3759
end
3860

3961
def to_s
@@ -45,4 +67,5 @@ def to_s
4567
end
4668

4769
end
70+
4871
end

lib/git/repository.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module Git
2+
23
class Repository < Path
34
end
5+
46
end

tests/test_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
require 'test/unit'
21
require 'fileutils'
32
require 'logger'
3+
require 'test/unit'
4+
require 'ruby-debug'
45

56
require "#{File.expand_path(File.dirname(__FILE__))}/../lib/git"
67

tests/units/test_branch.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ def test_branches_remote
3030
end
3131

3232
def test_branches_single
33-
b = @git.branches[:test_object]
34-
assert_equal('test_object', b.name)
33+
branch = @git.branches[:test_object]
34+
assert_equal('test_object', branch.name)
3535

36-
b = @git.branches['working/master']
37-
assert_equal('master', b.name)
38-
assert_equal('working/master', b.full)
39-
assert_equal('working', b.remote.name)
40-
assert_equal('+refs/heads/*:refs/remotes/working/*', b.remote.fetch_opts)
41-
assert_equal('../working.git', b.remote.url)
36+
%w{working/master remotes/working/master}.each do |branch_name|
37+
branch = @git.branches[branch_name]
38+
39+
assert_equal('master', branch.name)
40+
assert_equal('remotes/working/master', branch.full)
41+
assert_equal('working', branch.remote.name)
42+
assert_equal('+refs/heads/*:refs/remotes/working/*', branch.remote.fetch_opts)
43+
assert_equal('../working.git', branch.remote.url)
44+
end
4245
end
4346

4447
def test_branch_commit
@@ -89,4 +92,4 @@ def test_branch_create_and_switch
8992
end
9093
end
9194

92-
end
95+
end

tests/units/test_lib.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_log_commits
3232
assert(a.first.is_a?(String))
3333
assert_equal(10, a.size)
3434

35-
a = @lib.log_commits :count => 20, :since => '3 years ago'
35+
a = @lib.log_commits :count => 20, :since => "#{Date.today.year - 2007} years ago"
3636
assert(a.first.is_a?(String))
3737
assert_equal(20, a.size)
3838

@@ -165,4 +165,4 @@ def test_grep
165165
assert_equal(2, match.size)
166166
end
167167

168-
end
168+
end

tests/units/test_log.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_get_log_since
3636
l = @git.log.since("2 seconds ago")
3737
assert_equal(0, l.size)
3838

39-
l = @git.log.since("2 years ago")
39+
l = @git.log.since("#{Date.today.year - 2007} years ago")
4040
assert_equal(30, l.size)
4141
end
4242

tests/units/test_logger.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_logger
1919
@git.branches.size
2020

2121
logc = File.read(log.path)
22-
assert(/INFO -- : git branch '-a'/.match(logc))
22+
assert(/INFO -- : git branch "-a"/.match(logc))
2323
assert(/DEBUG -- : \* git_grep/.match(logc))
2424

2525
log = Tempfile.new('logfile')
@@ -31,7 +31,7 @@ def test_logger
3131
@git.branches.size
3232

3333
logc = File.read(log.path)
34-
assert(/INFO -- : git branch '-a'/.match(logc))
34+
assert(/INFO -- : git branch "-a"/.match(logc))
3535
assert(!/DEBUG -- : \* git_grep/.match(logc))
3636
end
3737

tests/units/test_tree_ops.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ def test_read_tree
6565
tr = nil
6666
g.with_temp_working do
6767
tr = g.with_temp_index do
68-
assert_raises Git::GitExecuteError do
69-
g.add # add whats in our working tree - should be nothing
68+
begin
69+
g.add
70+
rescue Exception => e
71+
# Adding nothig is now validd on Git 1.7.x
72+
# If an error ocurres (Git 1.6.x) it MUST rise Git::GitExecuteError
73+
assert_equal(e.class, Git::GitExecuteError)
7074
end
7175
g.read_tree('testbranch1', :prefix => 'b1/')
7276
g.read_tree('testbranch3', :prefix => 'b1/b3/')

0 commit comments

Comments
 (0)