From 99d87d8698437f87c21ced1d5a7fab4f9d253493 Mon Sep 17 00:00:00 2001
From: James Lopez <james@jameslopez.es>
Date: Tue, 8 Mar 2016 18:17:57 +0100
Subject: [PATCH] WIP - added wiki repo bundler

---
 .../import_export/command_line_util.rb        |  6 ++++
 .../import_export/wiki_repo_bundler.rb        | 30 +++++++++++++++++++
 .../import_export/wiki_repo_bundler_spec.rb   | 25 ++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 app/services/projects/import_export/wiki_repo_bundler.rb
 create mode 100644 spec/services/projects/import_export/wiki_repo_bundler_spec.rb

diff --git a/app/services/projects/import_export/command_line_util.rb b/app/services/projects/import_export/command_line_util.rb
index 7b0cc08763b..3ca49e76c7f 100644
--- a/app/services/projects/import_export/command_line_util.rb
+++ b/app/services/projects/import_export/command_line_util.rb
@@ -6,6 +6,12 @@ module Projects
         _output, status = Gitlab::Popen.popen(cmd)
         status.zero?
       end
+
+      def git_bundle(git_bin_path: Gitlab.config.git.bin_path, repo_path:, bundle_path:)
+        cmd = %W(#{git_bin_path} --git-dir=#{repo_path} bundle create #{bundle_path} --all)
+        _output, status = Gitlab::Popen.popen(cmd)
+        status.zero?
+      end
     end
   end
 end
diff --git a/app/services/projects/import_export/wiki_repo_bundler.rb b/app/services/projects/import_export/wiki_repo_bundler.rb
new file mode 100644
index 00000000000..793c8efd142
--- /dev/null
+++ b/app/services/projects/import_export/wiki_repo_bundler.rb
@@ -0,0 +1,30 @@
+module Projects
+  module ImportExport
+    class WikiRepoBundler < RepoBundler
+      def bundle
+        @wiki = ProjectWiki.new(@project)
+        return false if !wiki?
+        @full_path = File.join(@export_path, project_filename)
+        bundle_to_disk
+      end
+
+      def bundle_to_disk
+        FileUtils.mkdir_p(@export_path)
+        git_bundle(repo_path: path_to_repo, bundle_path: @full_path)
+      rescue
+        #TODO: handle error
+        false
+      end
+
+      private
+
+      def path_to_repo
+        @wiki.repository.path_to_repo
+      end
+
+      def wiki?
+        File.exists?(@wiki.repository.path_to_repo) && !@wiki.repository.empty?
+      end
+    end
+  end
+end
diff --git a/spec/services/projects/import_export/wiki_repo_bundler_spec.rb b/spec/services/projects/import_export/wiki_repo_bundler_spec.rb
new file mode 100644
index 00000000000..351419d0619
--- /dev/null
+++ b/spec/services/projects/import_export/wiki_repo_bundler_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe Projects::ImportExport::WikiRepoBundler, services: true do
+  describe :bundle do
+
+    let(:user) { create(:user) }
+    let!(:project) { create(:project, :public, name: 'searchable_project') }
+    let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" }
+    let(:shared) { Projects::ImportExport::Shared.new(relative_path: project.path_with_namespace) }
+    let(:wiki_bundler) { Projects::ImportExport::WikiRepoBundler.new(project: project, shared: shared) }
+
+    before(:each) do
+      project.team << [user, :master]
+      allow_any_instance_of(Projects::ImportExport).to receive(:storage_path).and_return(export_path)
+    end
+
+    after(:each) do
+      FileUtils.rm_rf(export_path)
+    end
+
+    it 'bundles the repo successfully' do
+      expect(wiki_bundler.bundle).to be true
+    end
+  end
+end
-- 
GitLab