From 3e97068c64034a29bbf1e33948a56f67cf6635a1 Mon Sep 17 00:00:00 2001
From: tiagonbotelho <tiagonbotelho@hotmail.com>
Date: Mon, 4 Jul 2016 16:46:37 +0100
Subject: [PATCH] refactors blob_controller

---
 Gemfile                                     |  2 +-
 Gemfile.lock                                | 17 +++++++----
 app/controllers/concerns/creates_commit.rb  |  8 +++++
 app/controllers/projects/blob_controller.rb |  4 +--
 app/models/repository.rb                    | 33 +++++++++++++++++++++
 app/services/files/update_service.rb        |  5 +++-
 6 files changed, 59 insertions(+), 10 deletions(-)

diff --git a/Gemfile b/Gemfile
index e409e66aab0..0ba6bbdb4b1 100644
--- a/Gemfile
+++ b/Gemfile
@@ -52,7 +52,7 @@ gem "browser", '~> 2.2'
 
 # Extracting information from a git repository
 # Provide access to Gitlab::Git library
-gem "gitlab_git", '~> 10.2'
+gem "gitlab_git", '~> 10.2', path: "~/src/Gitlab/gitlab_git"
 
 # LDAP Auth
 # GitLab fork with several improvements to original library. For full list of changes
diff --git a/Gemfile.lock b/Gemfile.lock
index 35c3770d42c..4e976370de4 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,3 +1,12 @@
+PATH
+  remote: ~/src/Gitlab/gitlab_git
+  specs:
+    gitlab_git (10.3.0)
+      activesupport (~> 4.0)
+      charlock_holmes (~> 0.7.3)
+      github-linguist (~> 4.7.0)
+      rugged (~> 0.24.0)
+
 GEM
   remote: https://rubygems.org/
   specs:
@@ -276,11 +285,6 @@ GEM
       posix-spawn (~> 0.3)
     gitlab_emoji (0.3.1)
       gemojione (~> 2.2, >= 2.2.1)
-    gitlab_git (10.2.3)
-      activesupport (~> 4.0)
-      charlock_holmes (~> 0.7.3)
-      github-linguist (~> 4.7.0)
-      rugged (~> 0.24.0)
     gitlab_meta (7.0)
     gitlab_omniauth-ldap (1.2.1)
       net-ldap (~> 0.9)
@@ -391,6 +395,7 @@ GEM
     mail_room (0.8.0)
     method_source (0.8.2)
     mime-types (2.99.2)
+    mime-types-data (3.2016.0521)
     mimemagic (0.3.0)
     mini_portile2 (2.1.0)
     minitest (5.7.0)
@@ -863,7 +868,7 @@ DEPENDENCIES
   github-markup (~> 1.3.1)
   gitlab-flowdock-git-hook (~> 1.0.1)
   gitlab_emoji (~> 0.3.0)
-  gitlab_git (~> 10.2)
+  gitlab_git (~> 10.2)!
   gitlab_meta (= 7.0)
   gitlab_omniauth-ldap (~> 1.2.1)
   gollum-lib (~> 4.1.0)
diff --git a/app/controllers/concerns/creates_commit.rb b/app/controllers/concerns/creates_commit.rb
index 84b4a30c6d5..dc157121106 100644
--- a/app/controllers/concerns/creates_commit.rb
+++ b/app/controllers/concerns/creates_commit.rb
@@ -11,8 +11,16 @@ module CreatesCommit
       file_path: @path
     )
 
+    puts "#" * 10
+    puts @previous_path
+    puts "#" * 10
+
     result = service.new(@tree_edit_project, current_user, commit_params).execute
 
+    puts "#" * 30
+    puts result[:status]
+    puts "#" * 30
+
     if result[:status] == :success
       update_flash_notice(success_notice)
 
diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb
index 2bd86a1f126..e2ddda1474b 100644
--- a/app/controllers/projects/blob_controller.rb
+++ b/app/controllers/projects/blob_controller.rb
@@ -43,8 +43,8 @@ class Projects::BlobController < Projects::ApplicationController
         diffs_namespace_project_merge_request_path(from_merge_request.target_project.namespace, from_merge_request.target_project, from_merge_request) +
           "#file-path-#{hexdigest(@path)}"
       else
-        unless params[:file_name] == @path
-          previous_path = @path
+        unless params[:file_name].empty?
+          @previous_path = @path
           @path = params[:file_name]
         end
 
diff --git a/app/models/repository.rb b/app/models/repository.rb
index 078ca8f4e13..75071c65efb 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -741,6 +741,39 @@ class Repository
     end
   end
 
+  def update_file(user, path, previous_path, content, message, branch, update)
+    commit_with_hooks(user, branch) do |ref|
+      committer = user_to_committer(user)
+      options = {}
+      options[:committer] = committer
+      options[:author] = committer
+      options[:commit] = {
+        message: message,
+        branch: ref
+      }
+
+      options[:file] = {
+        content: content,
+        path: path,
+        update: update
+      }
+
+      if previous_path
+        options[:file].merge!(previous_path: previous_path)
+
+        puts "#" * 90
+        puts "Hello"
+        puts "#" * 90
+        Gitlab::Git::Blob.rename(raw_repository, options)
+      else
+        puts "#" * 90
+        puts "World"
+        puts "#" * 90
+        Gitlab::Git::Blob.commit(raw_repository, options)
+      end
+    end
+  end
+
   def remove_file(user, path, message, branch)
     commit_with_hooks(user, branch) do |ref|
       committer = user_to_committer(user)
diff --git a/app/services/files/update_service.rb b/app/services/files/update_service.rb
index 52451d72b57..7b7bce20662 100644
--- a/app/services/files/update_service.rb
+++ b/app/services/files/update_service.rb
@@ -4,7 +4,10 @@ module Files
   class UpdateService < Files::BaseService
     def commit
       # Need to update file_path with the new filename
-      repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch, true)
+      puts "#" * 10
+      puts @previous_path
+      puts "#" * 10
+      repository.update_file(current_user, @file_path, @previous_path, @file_content, @commit_message, @target_branch, true)
     end
   end
 end
-- 
GitLab