BigW Consortium Gitlab

workhorse.rb 1.65 KB
Newer Older
1 2 3 4 5
require 'base64'
require 'json'

module Gitlab
  class Workhorse
Jacob Vosmaer committed
6
    SEND_DATA_HEADER = 'Gitlab-Workhorse-Send-Data'
7

Jacob Vosmaer committed
8
    class << self
9 10
      def git_http_ok(repository, user)
        {
11
          'GL_ID' => Gitlab::GlId.gl_id(user),
12 13 14 15
          'RepoPath' => repository.path_to_repo,
        }
      end

16
      def send_git_blob(repository, blob)
17
        params = {
18 19 20 21 22
          'RepoPath' => repository.path_to_repo,
          'BlobId' => blob.id,
        }

        [
23
          SEND_DATA_HEADER,
Douwe Maan committed
24
          "git-blob:#{encode(params)}"
25 26
        ]
      end
27

28
      def send_git_archive(repository, ref:, format:)
29 30
        format ||= 'tar.gz'
        format.downcase!
31
        params = repository.archive_metadata(ref, Gitlab.config.gitlab.repository_downloads_path, format)
32 33 34 35
        raise "Repository or ref not found" if params.empty?

        [
          SEND_DATA_HEADER,
Douwe Maan committed
36
          "git-archive:#{encode(params)}"
37 38
        ]
      end
39

Douwe Maan committed
40
      def send_git_diff(repository, diff_refs)
41
        params = {
Douwe Maan committed
42
          'RepoPath'  => repository.path_to_repo,
43 44
          'ShaFrom'   => diff_refs.start_sha,
          'ShaTo'     => diff_refs.head_sha
45 46 47 48 49
        }

        [
          SEND_DATA_HEADER,
          "git-diff:#{encode(params)}"
50 51
        ]
      end
52

Douwe Maan committed
53
      def send_git_patch(repository, diff_refs)
54
        params = {
55
          'RepoPath'  => repository.path_to_repo,
Douwe Maan committed
56 57
          'ShaFrom'   => diff_refs.start_sha,
          'ShaTo'     => diff_refs.head_sha
58 59 60
        }

        [
61
          SEND_DATA_HEADER,
62 63 64 65
          "git-format-patch:#{encode(params)}"
        ]
      end

66
      protected
67

68 69 70
      def encode(hash)
        Base64.urlsafe_encode64(JSON.dump(hash))
      end
71 72
    end
  end
Jacob Vosmaer committed
73
end