summaryrefslogtreecommitdiff
path: root/lib/gitlab
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-04-27 23:50:17 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-04-27 23:50:17 +0300
commit4f2d2c90f7c32a9113ccce440411ec80374ee385 (patch)
tree312803a008da343b9e773324d8411557a271e918 /lib/gitlab
parentdf96c079ef3e358ea221ce4c43163d478b79a5e0 (diff)
downloadgitlab-ce-4f2d2c90f7c32a9113ccce440411ec80374ee385.tar.gz
Move Gitlab::Git out of gitlab core
Diffstat (limited to 'lib/gitlab')
-rw-r--r--lib/gitlab/git/blame.rb23
-rw-r--r--lib/gitlab/git/blob.rb42
-rw-r--r--lib/gitlab/git/commit.rb127
-rw-r--r--lib/gitlab/git/compare.rb35
-rw-r--r--lib/gitlab/git/diff.rb63
-rw-r--r--lib/gitlab/git/repository.rb221
-rw-r--r--lib/gitlab/git/stats.rb75
-rw-r--r--lib/gitlab/git/tree.rb52
8 files changed, 0 insertions, 638 deletions
diff --git a/lib/gitlab/git/blame.rb b/lib/gitlab/git/blame.rb
deleted file mode 100644
index d7282c587aa..00000000000
--- a/lib/gitlab/git/blame.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-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
diff --git a/lib/gitlab/git/blob.rb b/lib/gitlab/git/blob.rb
deleted file mode 100644
index 57b89912f2d..00000000000
--- a/lib/gitlab/git/blob.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-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
diff --git a/lib/gitlab/git/commit.rb b/lib/gitlab/git/commit.rb
deleted file mode 100644
index a92c21a0da1..00000000000
--- a/lib/gitlab/git/commit.rb
+++ /dev/null
@@ -1,127 +0,0 @@
-# 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
diff --git a/lib/gitlab/git/compare.rb b/lib/gitlab/git/compare.rb
deleted file mode 100644
index e34f204e8bd..00000000000
--- a/lib/gitlab/git/compare.rb
+++ /dev/null
@@ -1,35 +0,0 @@
-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
-
diff --git a/lib/gitlab/git/diff.rb b/lib/gitlab/git/diff.rb
deleted file mode 100644
index 45191dd3f91..00000000000
--- a/lib/gitlab/git/diff.rb
+++ /dev/null
@@ -1,63 +0,0 @@
-# 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
-
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
deleted file mode 100644
index 0218f2fe0e5..00000000000
--- a/lib/gitlab/git/repository.rb
+++ /dev/null
@@ -1,221 +0,0 @@
-# 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
diff --git a/lib/gitlab/git/stats.rb b/lib/gitlab/git/stats.rb
deleted file mode 100644
index c925c653342..00000000000
--- a/lib/gitlab/git/stats.rb
+++ /dev/null
@@ -1,75 +0,0 @@
-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
diff --git a/lib/gitlab/git/tree.rb b/lib/gitlab/git/tree.rb
deleted file mode 100644
index e6b500ba18c..00000000000
--- a/lib/gitlab/git/tree.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-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
-