Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-05-07 17:14:24 +0400
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-05-07 17:14:24 +0400
commit4fea8afc4bc507bf94d93c1ef04a9575add900ab (patch)
tree240f39aa61b839e26952ff8799f1f249befa7bdd /lib
parentdd3a55064edb595215c70a77d655e5578ddfcb20 (diff)
Add CompareAction class for collecting commits and diffs using satellites
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/satellite/compare_action.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/gitlab/satellite/compare_action.rb b/lib/gitlab/satellite/compare_action.rb
new file mode 100644
index 00000000000..c923bb9c0f0
--- /dev/null
+++ b/lib/gitlab/satellite/compare_action.rb
@@ -0,0 +1,53 @@
+module Gitlab
+ module Satellite
+ class CompareAction < Action
+ def initialize(user, target_project, target_branch, source_project, source_branch)
+ super user, target_project
+
+ @target_project, @target_branch = target_project, target_branch
+ @source_project, @source_branch = source_project, source_branch
+ end
+
+ # Only show what is new in the source branch compared to the target branch, not the other way around.
+ # The line 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"
+ def diffs
+ in_locked_and_timed_satellite do |target_repo|
+ prepare_satellite!(target_repo)
+ update_satellite_source_and_target!(target_repo)
+ common_commit = target_repo.git.native(:merge_base, default_options, ["origin/#{@target_branch}", "source/#{@source_branch}"]).strip
+ #this method doesn't take default options
+ diffs = target_repo.diff(common_commit, "source/#{@source_branch}")
+ diffs = diffs.map { |diff| Gitlab::Git::Diff.new(diff) }
+ diffs
+ end
+ rescue Grit::Git::CommandFailed => ex
+ handle_exception(ex)
+ end
+
+ # Retrieve an array of commits between the source and the target
+ def commits
+ in_locked_and_timed_satellite do |target_repo|
+ prepare_satellite!(target_repo)
+ update_satellite_source_and_target!(target_repo)
+ commits = target_repo.commits_between("origin/#{@target_branch}", "source/#{@source_branch}")
+ commits = commits.map { |commit| Gitlab::Git::Commit.new(commit, nil) }
+ commits
+ end
+ rescue Grit::Git::CommandFailed => ex
+ handle_exception(ex)
+ end
+
+ private
+
+ # Assumes a satellite exists that is a fresh clone of the projects repo, prepares satellite for diffs
+ def update_satellite_source_and_target!(target_repo)
+ target_repo.remote_add('source', @source_project.repository.path_to_repo)
+ target_repo.remote_fetch('source')
+ target_repo.git.checkout(default_options({b: true}), @target_branch, "origin/#{@target_branch}")
+ rescue Grit::Git::CommandFailed => ex
+ handle_exception(ex)
+ end
+ end
+ end
+end