summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-11-06 13:32:40 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-11-06 13:32:40 +0200
commitbd144a604f3c72552cd112fdffc3a402e69b628c (patch)
tree406390779e73fd20ef886915d6a43030cb906d3f
parent1cc5704a00784f71ec2f4392160479fb3044f4d2 (diff)
downloadgitlab-ci-bd144a604f3c72552cd112fdffc3a402e69b628c.tar.gz
Refactor commit sha
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--app/helpers/gitlab_helper.rb2
-rw-r--r--app/models/commit.rb16
-rw-r--r--app/services/create_commit_service.rb2
-rw-r--r--app/views/commits/show.html.haml5
-rw-r--r--lib/git.rb3
5 files changed, 22 insertions, 6 deletions
diff --git a/app/helpers/gitlab_helper.rb b/app/helpers/gitlab_helper.rb
index a5d3192..7910a04 100644
--- a/app/helpers/gitlab_helper.rb
+++ b/app/helpers/gitlab_helper.rb
@@ -15,6 +15,6 @@ module GitlabHelper
def gitlab_commit_link project, sha
gitlab_url = project.gitlab_url.dup
gitlab_url << "/commit/#{sha}"
- link_to sha[0...10], gitlab_url
+ link_to Commit.truncate_sha(sha), gitlab_url
end
end
diff --git a/app/models/commit.rb b/app/models/commit.rb
index 33087b3..3532035 100644
--- a/app/models/commit.rb
+++ b/app/models/commit.rb
@@ -22,6 +22,10 @@ class Commit < ActiveRecord::Base
validates_presence_of :ref, :sha, :before_sha, :push_data
validate :valid_commit_sha
+ def self.truncate_sha(sha)
+ sha[0...8]
+ end
+
def to_param
sha
end
@@ -31,13 +35,17 @@ class Commit < ActiveRecord::Base
end
def valid_commit_sha
- if self.sha =~ /\A00000000/
+ if self.sha == Git::BLANK_SHA
self.errors.add(:sha, " cant be 00000000 (branch removal)")
end
end
+ def new_branch?
+ before_sha == Git::BLANK_SHA
+ end
+
def compare?
- gitlab? && before_sha
+ gitlab? && !new_branch?
end
def gitlab?
@@ -61,11 +69,11 @@ class Commit < ActiveRecord::Base
end
def short_before_sha
- before_sha[0..8]
+ Commit.truncate_sha(before_sha)
end
def short_sha
- sha[0..8]
+ Commit.truncate_sha(sha)
end
def commit_data
diff --git a/app/services/create_commit_service.rb b/app/services/create_commit_service.rb
index ee951c0..64d86d3 100644
--- a/app/services/create_commit_service.rb
+++ b/app/services/create_commit_service.rb
@@ -9,7 +9,7 @@ class CreateCommitService
end
# Skip branch removal
- if sha =~ /\A0+\Z/
+ if sha == Git::BLANK_SHA
return false
end
diff --git a/app/views/commits/show.html.haml b/app/views/commits/show.html.haml
index 07d7eb9..d42b338 100644
--- a/app/views/commits/show.html.haml
+++ b/app/views/commits/show.html.haml
@@ -21,6 +21,11 @@
%p
%span.attr-name Compare:
#{gitlab_compare_link(@project, @commit.short_before_sha, @commit.short_sha)}
+ - else
+ %p
+ %span.attr-name Commit:
+ #{gitlab_commit_link(@project, @commit.sha)}
+
%p
%span.attr-name Branch:
#{gitlab_ref_link(@project, @commit.ref)}
diff --git a/lib/git.rb b/lib/git.rb
new file mode 100644
index 0000000..c6b9f6b
--- /dev/null
+++ b/lib/git.rb
@@ -0,0 +1,3 @@
+module Git
+ BLANK_SHA = '0' * 40
+end