Commit 4f2d2c90 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Move Gitlab::Git out of gitlab core

parent df96c079
......@@ -26,6 +26,8 @@ gem 'omniauth-github'
gem "grit", '~> 2.5.0', git: 'https://github.com/gitlabhq/grit.git', ref: '42297cdcee16284d2e4eff23d41377f52fc28b9d'
gem 'grit_ext', '~> 0.8.1'
gem 'gitlab_git', path: '../gitlab_git'
# Ruby/Rack Git Smart-HTTP Server Handler
gem 'gitlab-grack', '~> 1.0.0', require: 'grack'
......
......@@ -32,6 +32,14 @@ GIT
faye-websocket (~> 0.4, >= 0.4.4)
http_parser.rb (~> 0.5.3)
PATH
remote: ../gitlab_git
specs:
gitlab_git (1.0.0)
activesupport (~> 3.2.13)
github-linguist (~> 2.3.4)
grit (~> 2.5.0)
GEM
remote: https://rubygems.org/
specs:
......@@ -501,6 +509,7 @@ DEPENDENCIES
github-markup (~> 0.7.4)
gitlab-grack (~> 1.0.0)
gitlab-pygments.rb (~> 0.3.2)
gitlab_git!
gitlab_meta (= 5.0)
gitlab_omniauth-ldap (= 1.0.2)
gollum-lib (~> 1.0.0)
......
......@@ -6,3 +6,6 @@ require Rails.root.join("lib", "gitlab", "backend", "shell")
# GitLab shell adapter
require Rails.root.join("lib", "gitlab", "backend", "shell_adapter")
# Gitlab Git repos path
Gitlab::Git::Repository.repos_path = Gitlab.config.gitlab_shell.repos_path
module Gitlab
module Git
class Blame
attr_accessor :repository, :sha, :path
def initialize(repository, sha, path)
@repository, @sha, @path = repository, sha, path
end
def each
raw_blame = Grit::Blob.blame(repository.repo, sha, path)
raw_blame.each do |commit, lines|
next unless commit
commit = Gitlab::Git::Commit.new(commit)
yield(commit, lines)
end
end
end
end
end
module Gitlab
module Git
class Blob
include Linguist::BlobHelper
attr_accessor :raw_blob
delegate :name, to: :raw_blob
def initialize(repository, sha, ref, path)
@repository, @sha, @ref = repository, sha, ref
@commit = @repository.commit(sha)
@raw_blob = @repository.tree(@commit, path)
end
def data
if raw_blob
raw_blob.data
else
nil
end
end
def exists?
raw_blob
end
def empty?
data.blank?
end
def mode
raw_blob.mode
end
def size
raw_blob.size
end
end
end
end
# Gitlab::Git::Commit is a wrapper around native Grit::Commit object
# We dont want to use grit objects inside app/
# It helps us easily migrate to rugged in future
module Gitlab
module Git
class Commit
attr_accessor :raw_commit, :head, :refs,
:id, :authored_date, :committed_date, :message,
:author_name, :author_email, :parent_ids,
:committer_name, :committer_email
delegate :parents, :tree, :stats, :to_patch,
to: :raw_commit
def initialize(raw_commit, head = nil)
raise "Nil as raw commit passed" unless raw_commit
if raw_commit.is_a?(Hash)
init_from_hash(raw_commit)
else
init_from_grit(raw_commit)
end
@head = head
end
def serialize_keys
@serialize_keys ||= %w(id authored_date committed_date author_name author_email committer_name committer_email message parent_ids).map(&:to_sym)
end
def sha
id
end
def short_id(length = 10)
id.to_s[0..length]
end
def safe_message
@safe_message ||= message
end
def created_at
committed_date
end
# Was this commit committed by a different person than the original author?
def different_committer?
author_name != committer_name || author_email != committer_email
end
def parent_id
parent_ids.first
end
# Shows the diff between the commit's parent and the commit.
#
# Cuts out the header and stats from #to_patch and returns only the diff.
def to_diff
# see Grit::Commit#show
patch = to_patch
# discard lines before the diff
lines = patch.split("\n")
while !lines.first.start_with?("diff --git") do
lines.shift
end
lines.pop if lines.last =~ /^[\d.]+$/ # Git version
lines.pop if lines.last == "-- " # end of diff
lines.join("\n")
end
def has_zero_stats?
stats.total.zero?
rescue
true
end
def no_commit_message
"--no commit message"
end
def to_hash
hash = {}
keys = serialize_keys
keys.each do |key|
hash[key] = send(key)
end
hash
end
def date
committed_date
end
def diffs
raw_commit.diffs.map { |diff| Gitlab::Git::Diff.new(diff) }
end
private
def init_from_grit(grit)
@raw_commit = grit
@id = grit.id
@message = grit.message
@authored_date = grit.authored_date
@committed_date = grit.committed_date
@author_name = grit.author.name
@author_email = grit.author.email
@committer_name = grit.committer.name
@committer_email = grit.committer.email
@parent_ids = grit.parents.map(&:id)
end
def init_from_hash(hash)
raw_commit = hash.symbolize_keys
serialize_keys.each do |key|
send(:"#{key}=", raw_commit[key.to_sym])
end
end
end
end
end
module Gitlab
module Git
class Compare
attr_accessor :commits, :commit, :diffs, :same
def initialize(repository, from, to)
@commits, @diffs = [], []
@commit = nil
@same = false
return unless from && to
first = repository.commit(to.try(:strip))
last = repository.commit(from.try(:strip))
return unless first && last
if first.id == last.id
@same = true
return
end
@commit = first
@commits = repository.commits_between(last.id, first.id)
@diffs = if @commits.size > 100
[]
else
repository.repo.diff(last.id, first.id) rescue []
end
end
end
end
end
# Gitlab::Git::Diff is a wrapper around native Grit::Diff object
# We dont want to use grit objects inside app/
# It helps us easily migrate to rugged in future
module Gitlab
module Git
class Diff
BROKEN_DIFF = "--broken-diff"
attr_accessor :raw_diff
# Diff properties
attr_accessor :old_path, :new_path, :a_mode, :b_mode, :diff
# Stats properties
attr_accessor :new_file, :renamed_file, :deleted_file
def initialize(raw_diff)
raise "Nil as raw diff passed" unless raw_diff
if raw_diff.is_a?(Hash)
init_from_hash(raw_diff)
else
init_from_grit(raw_diff)
end
end
def serialize_keys
@serialize_keys ||= %w(diff new_path old_path a_mode b_mode new_file renamed_file deleted_file).map(&:to_sym)
end
def to_hash
hash = {}
keys = serialize_keys
keys.each do |key|
hash[key] = send(key)
end
hash
end
private
def init_from_grit(grit)
@raw_diff = grit
serialize_keys.each do |key|
send(:"#{key}=", grit.send(key))
end
end
def init_from_hash(hash)
raw_diff = hash.symbolize_keys
serialize_keys.each do |key|
send(:"#{key}=", raw_diff[key.to_sym])
end
end
end
end
end
# Gitlab::Git::Gitlab::Git::Commit is a wrapper around native Grit::Repository object
# We dont want to use grit objects inside app/
# It helps us easily migrate to rugged in future
module Gitlab
module Git
class Repository
include Gitlab::Popen
class NoRepository < StandardError; end
# Repository directory name with namespace direcotry
# Examples:
# gitlab/gitolite
# diaspora
#
attr_accessor :path_with_namespace
# Grit repo object
attr_accessor :repo
# Default branch in the repository
attr_accessor :root_ref
def initialize(path_with_namespace, root_ref = 'master')
@root_ref = root_ref || "master"
@path_with_namespace = path_with_namespace
# Init grit repo object
repo
end
def raw
repo
end
def path_to_repo
@path_to_repo ||= File.join(repos_path, "#{path_with_namespace}.git")
end
def repos_path
Gitlab.config.gitlab_shell.repos_path
end
def repo
@repo ||= Grit::Repo.new(path_to_repo)
rescue Grit::NoSuchPathError
raise NoRepository.new('no repository for such path')
end
def commit(commit_id = nil)
commit = if commit_id
# Find repo.refs first,
# because if commit_id is "tag name",
# repo.commit(commit_id) returns wrong commit sha
# that is git tag object sha.
ref = repo.refs.find {|r| r.name == commit_id}
if ref
ref.commit
else
repo.commit(commit_id)
end
else
repo.commits(root_ref).first
end
decorate_commit(commit) if commit
end
def commits_with_refs(n = 20)
commits = repo.branches.map { |ref| decorate_commit(ref.commit, ref) }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0..n]
end
def commits(ref, path = nil, limit = nil, offset = nil)
if path.present?
repo.log(ref, path, max_count: limit, skip: offset, follow: true)
elsif limit && offset
repo.commits(ref, limit.to_i, offset.to_i)
else
repo.commits(ref)
end.map{ |c| decorate_commit(c) }
end
def commits_between(from, to)
repo.commits_between(from, to).map { |c| decorate_commit(c) }
end
def last_commit_for(ref, path = nil)
commits(ref, path, 1).first
end
# Returns an Array of branch names
# sorted by name ASC
def branch_names
branches.map(&:name)
end
# Returns an Array of Branches
def branches
repo.branches.sort_by(&:name)
end
# Returns an Array of tag names
def tag_names
repo.tags.collect(&:name).sort.reverse
end
# Returns an Array of Tags
def tags
repo.tags.sort_by(&:name).reverse
end
# Returns an Array of branch and tag names
def ref_names
[branch_names + tag_names].flatten
end
def heads
@heads ||= repo.heads
end
def tree(fcommit, path = nil)
fcommit = commit if fcommit == :head
tree = fcommit.tree
path ? (tree / path) : tree
end
def has_commits?
!!commit
rescue Grit::NoSuchPathError
false
end
def empty?
!has_commits?
end
# Discovers the default branch based on the repository's available branches
#
# - If no branches are present, returns nil
# - If one branch is present, returns its name
# - If two or more branches are present, returns the one that has a name
# matching root_ref (default_branch or 'master' if default_branch is nil)
def discover_default_branch
if branch_names.length == 0
nil
elsif branch_names.length == 1
branch_names.first
else
branch_names.select { |v| v == root_ref }.first
end
end
# Archive Project to .tar.gz
#
# Already packed repo archives stored at
# app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
#
def archive_repo(ref)
ref = ref || self.root_ref
commit = self.commit(ref)
return nil unless commit
# Build file path
file_name = self.path_with_namespace.gsub("/","_") + "-" + commit.id.to_s + ".tar.gz"
storage_path = Rails.root.join("tmp", "repositories")
file_path = File.join(storage_path, self.path_with_namespace, file_name)
# Put files into a directory before archiving
prefix = File.basename(self.path_with_namespace) + "/"
# Create file if not exists
unless File.exists?(file_path)
FileUtils.mkdir_p File.dirname(file_path)
file = self.repo.archive_to_file(ref, prefix, file_path)
end
file_path
end
# Return repo size in megabytes
# Cached in redis
def size
Rails.cache.fetch(cache_key(:size)) do
size = popen('du -s', path_to_repo).first.strip.to_i
(size.to_f / 1024).round(2)
end
end
def expire_cache
Rails.cache.delete(cache_key(:size))
end
def cache_key(type)
"#{type}:#{path_with_namespace}"
end
def diffs_between(source_branch, target_branch)
# Only show what is new in the source branch compared to the target branch, not the other way around.
# The linex below with merge_base is equivalent to diff with three dots (git diff branch1...branch2)
# From the git documentation: "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B"
common_commit = repo.git.native(:merge_base, {}, [target_branch, source_branch]).strip
repo.diff(common_commit, source_branch).map { |diff| Gitlab::Git::Diff.new(diff) }
rescue Grit::Git::GitTimeout
[Gitlab::Git::Diff::BROKEN_DIFF]
end
protected
def decorate_commit(commit, ref = nil)
Gitlab::Git::Commit.new(commit, ref)
end
end
end
end
module Gitlab
module Git
class Stats
attr_accessor :repo, :ref
def initialize repo, ref
@repo, @ref = repo, ref
end
def authors
@authors ||= collect_authors
end
def commits_count
@commits_count ||= repo.commit_count(ref)
end
def files_count
args = [ref, '-r', '--name-only' ]
repo.git.run(nil, 'ls-tree', nil, {}, args).split("\n").count
end
def authors_count
authors.size
end
def graph
@graph ||= build_graph
end
protected
def collect_authors
shortlog = repo.git.shortlog({e: true, s: true }, ref)
authors = []
lines = shortlog.split("\n")
lines.each do |line|
data = line.split("\t")
commits = data.first
author = Grit::Actor.from_string(data.last)
authors << OpenStruct.new(
name: author.name,
email: author.email,
commits: commits.to_i
)
end
authors.sort_by(&:commits).reverse
end
def build_graph n = 4
from, to = (Date.today - n.weeks), Date.today
args = ['--all', "--since=#{from.to_s(:date)}", '--format=%ad' ]
rev_list = repo.git.run(nil, 'rev-list', nil, {}, args).split("\n")
commits_dates = rev_list.values_at(* rev_list.each_index.select {|i| i.odd?})
commits_dates = commits_dates.map { |date_str| Time.parse(date_str).to_date.to_s(:date) }
commits_per_day = from.upto(to).map do |day|
commits_dates.count(day.to_date.to_s(:date))
end
OpenStruct.new(
labels: from.upto(to).map { |day| day.stamp('Aug 23') },
commits: commits_per_day,
weeks: n
)
end
end
end
end
module Gitlab
module Git
class Tree
attr_accessor :repository, :sha, :path, :ref, :raw_tree, :id
def initialize(repository, sha, ref = nil, path = nil)
@repository, @sha, @ref, @path = repository, sha, ref, path
@path = nil if @path.blank?
# Load tree from repository
@commit = @repository.commit(@sha)
@raw_tree = @repository.tree(@commit, @path)
end
def exists?
raw_tree
end
def empty?
data.blank?
end
def trees
entries.select { |t| t.is_a?(Grit::Tree) }
end
def blobs
entries.select { |t| t.is_a?(Grit::Blob) }
end
def is_blob?
raw_tree.is_a?(Grit::Blob)
end
def up_dir?
path.present?
end
def readme
@readme ||= blobs.find { |c| c.name =~ /^readme/i }
end
protected
def entries
raw_tree.contents
end
end
end
end
require "spec_helper"
describe Gitlab::Git::Commit do
let(:commit) { create(:project_with_code).repository.commit }
describe "Commit info" do
before do
@committer = double(
email: 'mike@smith.com',
name: 'Mike Smith'
)
@author = double(
email: 'john@smith.com',
name: 'John Smith'
)
@raw_commit = double(
id: "bcf03b5de6abcf03b5de6c",
author: @author,
committer: @committer,
committed_date: Date.yesterday,
authored_date: Date.yesterday,
parents: [],
message: 'Refactoring specs'
)
@commit = Gitlab::Git::Commit.new(@raw_commit)
end
it { @commit.short_id.should == "bcf03b5de6a" }
it { @commit.safe_message.should == @raw_commit.message }
it { @commit.created_at.should == @raw_commit.committed_date }
it { @commit.author_email.should == @author.email }
it { @commit.author_name.should == @author.name }
it { @commit.committer_name.should == @committer.name }
it { @commit.committer_email.should == @committer.email }
it { @commit.different_committer?.should be_true }
end
end
require "spec_helper"
describe Gitlab::Git::Diff do
before do
@raw_diff_hash = {
diff: 'Hello world',
new_path: 'temp.rb',
old_path: 'test.rb',
a_mode: '100644',
b_mode: '100644',
new_file: false,
renamed_file: true,
deleted_file: false,
}
@grit_diff = double('Grit::Diff', @raw_diff_hash)
end
context 'init from grit' do
before do
@diff = Gitlab::Git::Diff.new(@raw_diff_hash)
end
it { @diff.to_hash.should == @raw_diff_hash }
end
context 'init from hash' do
before do
@diff = Gitlab::Git::Diff.new(@grit_diff)
end
it { @diff.to_hash.should == @raw_diff_hash }
end
end
require "spec_helper"
describe Gitlab::Git::Repository do
let(:repository) { Gitlab::Git::Repository.new('gitlabhq', 'master') }
describe "Respond to" do
subject { repository }
it { should respond_to(:repo) }
it { should respond_to(:tree) }
it { should respond_to(:root_ref) }
it { should respond_to(:tags) }
it { should respond_to(:commit) }
it { should respond_to(:commits) }
it { should respond_to(:commits_between) }
it { should respond_to(:commits_with_refs) }
end
describe "#discover_default_branch" do
let(:master) { 'master' }
let(:stable) { 'stable' }
it "returns 'master' when master exists" do
repository.should_receive(:branch_names).at_least(:once).and_return([stable, master])
repository.discover_default_branch.should == 'master'
end
it "returns non-master when master exists but default branch is set to something else" do
repository.root_ref = 'stable'
repository.should_receive(:branch_names).at_least(:once).and_return([stable, master])
repository.discover_default_branch.should == 'stable'
end
it "returns a non-master branch when only one exists" do
repository.should_receive(:branch_names).at_least(:once).and_return([stable])
repository.discover_default_branch.should == 'stable'
end
it "returns nil when no branch exists" do
repository.should_receive(:branch_names).at_least(:once).and_return([])
repository.discover_default_branch.should be_nil
end
end
describe :commit do
it "should return first head commit if without params" do
repository.commit.id.should == repository.repo.commits.first.id
end
it "should return valid commit" do
repository.commit(ValidCommit::ID).should be_valid_commit
end
it "should return nil" do
repository.commit("+123_4532530XYZ").should be_nil
end
end
describe :tree do
before do
@commit = repository.commit(ValidCommit::ID)
end
it "should raise error w/o arguments" do
lambda { repository.tree }.should raise_error
end
it "should return root tree for commit" do
tree = repository.tree(@commit)
tree.contents.size.should == ValidCommit::FILES_COUNT
tree.contents.map(&:name).should == ValidCommit::FILES
end
it "should return root tree for commit with correct path" do
tree = repository.tree(@commit, ValidCommit::C_FILE_PATH)
tree.contents.map(&:name).should == ValidCommit::C_FILES
end
it "should return root tree for commit with incorrect path" do
repository.tree(@commit, "invalid_path").should be_nil
end
end
describe "commits" do
subject do
commits = repository.commits('master', 'app', 3, 1)
commits.map { |c| c.id }
end
it { should have(3).elements }
it { should include("8716fc78f3c65bbf7bcf7b574febd583bc5d2812") }
it { should_not include("bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a") }
end
describe "commits_between" do
subject do
commits = repository.commits_between("3a4b4fb4cde7809f033822a171b9feae19d41fff",
"8470d70da67355c9c009e4401746b1d5410af2e3")
commits.map { |c| c.id }
end
it { should have(3).elements }
it { should include("f0f14c8eaba69ebddd766498a9d0b0e79becd633") }
it { should_not include("bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a") }
end
describe "branch names" do
subject { repository.branch_names }
it { should have(32).elements }
it { should include("master") }
it { should_not include("branch-from-space") }
end
describe "tag names" do
subject { repository.tag_names }
it { should have(16).elements }
it { should include("v1.2.0") }
it { should_not include("v5.0.0") }
end
end
require "spec_helper"
describe Gitlab::Git::Stats do
let(:repository) { Gitlab::Git::Repository.new('gitlabhq', 'master') }
before do
@stats = Gitlab::Git::Stats.new(repository.raw, 'master')
end
describe :authors do
let(:author) { @stats.authors.first }
it { author.name.should == 'Dmitriy Zaporozhets' }
it { author.email.should == 'dmitriy.zaporozhets@gmail.com' }
it { author.commits.should == 254 }
end
describe :graph do
let(:graph) { @stats.graph }
it { graph.labels.should include Date.today.stamp('Aug 23') }
it { graph.commits.should be_kind_of(Array) }
it { graph.weeks.should == 4 }
end
it { @stats.commits_count.should == 918 }
it { @stats.files_count.should == 550 }
end
......@@ -32,6 +32,7 @@ module TestEnv
# Use tmp dir for FS manipulations
repos_path = Rails.root.join('tmp', 'test-git-base-path')
Gitlab.config.gitlab_shell.stub(repos_path: repos_path)
Gitlab::Git::Repository.stub(repos_path: repos_path)
GollumWiki.any_instance.stub(:init_repo) do |path|
create_temp_repo(File.join(repos_path, "#{path}.git"))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment