Skip to content

Commit

Permalink
Merge pull request git-commit-notifier#161 from stanhu/master
Browse files Browse the repository at this point in the history
Fix bug where repo_name is blank when hooks.emailprefix is not defined to fix issue git-commit-notifier#160.
  • Loading branch information
akzhan committed Sep 19, 2012
2 parents ca71bc1 + ae4c008 commit cdfe055
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
23 changes: 18 additions & 5 deletions lib/git_commit_notifier/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def git_dir
from_shell("git rev-parse --git-dir").strip
end

def toplevel_dir
from_shell("git rev-parse --show-toplevel").strip
end

def rev_parse(param)
from_shell("git rev-parse '#{param}'").strip
end
Expand All @@ -110,6 +114,10 @@ def new_commits(oldrev, newrev, refname, unique_to_current_branch)
# Where B1, B2, ..., are any other branch
a = Array.new

# Zero revision comes in the form:
# "0000000000000000000000000000000000000000"
zero_rev = (oldrev =~ /^0+$/)

# If we want to include only those commits that are
# unique to this branch, then exclude commits that occur on
# other branches
Expand All @@ -118,13 +126,18 @@ def new_commits(oldrev, newrev, refname, unique_to_current_branch)
not_branches = lines_from_shell("git rev-parse --not --branches")
a = not_branches.map { |l| l.chomp }

# Remove the current branch (^BCURRENT) from the set
current_branch = rev_parse(refname)
a.delete_at a.index("^#{current_branch}") unless a.index("^#{current_branch}").nil?
# Remove the current branch (^BCURRENT) from the set, unless oldrev is
# 0. In this case, this is a new branch or an empty repository and we
# will want to keep it excluded, otherwise we will process every
# commit prior to the creation of this branch. Fixes issue #159.
if zero_rev.nil?
current_branch = rev_parse(refname)
a.delete_at a.index("^#{current_branch}") unless a.index("^#{current_branch}").nil?
end
end

# Add not'd oldrev (^oldrev)
a.push("^#{oldrev}") unless oldrev =~ /^0+$/
a.push("^#{oldrev}") unless zero_rev

# Add newrev
a.push(newrev)
Expand Down Expand Up @@ -167,7 +180,7 @@ def repo_name
''
end
return git_prefix unless git_prefix.empty?
File.expand_path(git_dir).split("/").last.sub(/\.git$/, '')
File.expand_path(toplevel_dir).split("/").last.sub(/\.git$/, '')
end

# Gets mailing list address.
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/git_commit_notifier/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@

it "should return folder name if no emailprefix and directory not ended with .git" do
mock(GitCommitNotifier::Git).from_shell("git config hooks.emailprefix") { " " }
mock(GitCommitNotifier::Git).git_dir { "/home/someuser/repositories/myrepo" }
stub(GitCommitNotifier::Git).toplevel_dir { "/home/someuser/repositories/myrepo" }
GitCommitNotifier::Git.repo_name.should == "myrepo"
end

it "should return folder name without extension if no emailprefix and directory ended with .git" do
mock(GitCommitNotifier::Git).from_shell("git config hooks.emailprefix") { " " }
mock(GitCommitNotifier::Git).git_dir { "/home/someuser/repositories/myrepo.git" }
stub(GitCommitNotifier::Git).toplevel_dir { "/home/someuser/repositories/myrepo.git" }
GitCommitNotifier::Git.repo_name.should == "myrepo"
end
end
Expand Down Expand Up @@ -105,8 +105,8 @@
describe :new_empty_branch do
it "should commit an empty branch and output nothing" do
mock(GitCommitNotifier::Git).from_shell("git rev-parse --not --branches") {
"^#{SAMPLE_REV}\n^#{SAMPLE_REV}\n^#{SAMPLE_REV_2}" }
mock(GitCommitNotifier::Git).rev_parse("refs/heads/branch2") { SAMPLE_REV }
"^#{SAMPLE_REV}\n^#{SAMPLE_REV_2}" }
stub(GitCommitNotifier::Git).rev_parse("refs/heads/branch2") { SAMPLE_REV }
stub(GitCommitNotifier::Git).from_shell("git rev-list --reverse #{SAMPLE_REV} ^#{SAMPLE_REV_2}") { SAMPLE_REV }
mock(GitCommitNotifier::Git).from_shell("git rev-list --reverse ^#{SAMPLE_REV} ^#{SAMPLE_REV_2} #{SAMPLE_REV}") { "" }
GitCommitNotifier::Git.new_commits("0000000000000000000000000000000000000000", SAMPLE_REV, "refs/heads/branch2", true).should == []
Expand Down

0 comments on commit cdfe055

Please sign in to comment.