1# frozen_string_literal: true
2
3require_relative 'test_env'
4
5# This file is specific to specs in spec/lib/gitlab/git/
6
7SEED_STORAGE_PATH      = Gitlab::GitalyClient::StorageSettings.allow_disk_access { TestEnv.repos_path }
8TEST_REPO_PATH         = 'gitlab-git-test.git'
9TEST_NORMAL_REPO_PATH  = 'not-bare-repo.git'
10TEST_MUTABLE_REPO_PATH = 'mutable-repo.git'
11TEST_BROKEN_REPO_PATH  = 'broken-repo.git'
12TEST_GITATTRIBUTES_REPO_PATH = 'with-git-attributes.git'
13
14module SeedHelper
15  GITLAB_GIT_TEST_REPO_URL = File.expand_path('../gitlab-git-test.git', __dir__)
16
17  def ensure_seeds
18    if File.exist?(SEED_STORAGE_PATH)
19      FileUtils.rm_r(SEED_STORAGE_PATH)
20    end
21
22    FileUtils.mkdir_p(SEED_STORAGE_PATH)
23
24    create_bare_seeds
25    create_normal_seeds
26    create_mutable_seeds
27    create_broken_seeds
28    create_git_attributes
29    create_invalid_git_attributes
30  end
31
32  def create_bare_seeds
33    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{GITLAB_GIT_TEST_REPO_URL}),
34           chdir: SEED_STORAGE_PATH,
35           out:   '/dev/null',
36           err:   '/dev/null')
37  end
38
39  def create_normal_seeds
40    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone #{TEST_REPO_PATH} #{TEST_NORMAL_REPO_PATH}),
41           chdir: SEED_STORAGE_PATH,
42           out: '/dev/null',
43           err: '/dev/null')
44  end
45
46  def create_mutable_seeds
47    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{TEST_REPO_PATH} #{TEST_MUTABLE_REPO_PATH}),
48           chdir: SEED_STORAGE_PATH,
49           out: '/dev/null',
50           err: '/dev/null')
51
52    mutable_repo_full_path = File.join(SEED_STORAGE_PATH, TEST_MUTABLE_REPO_PATH)
53    system(git_env, *%W(#{Gitlab.config.git.bin_path} branch -t feature origin/feature),
54           chdir: mutable_repo_full_path, out: '/dev/null', err: '/dev/null')
55
56    system(git_env, *%W(#{Gitlab.config.git.bin_path} remote add expendable #{GITLAB_GIT_TEST_REPO_URL}),
57           chdir: mutable_repo_full_path, out: '/dev/null', err: '/dev/null')
58  end
59
60  def create_broken_seeds
61    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{TEST_REPO_PATH} #{TEST_BROKEN_REPO_PATH}),
62           chdir: SEED_STORAGE_PATH,
63           out: '/dev/null',
64           err: '/dev/null')
65
66    refs_path = File.join(SEED_STORAGE_PATH, TEST_BROKEN_REPO_PATH, 'refs')
67
68    FileUtils.rm_r(refs_path)
69  end
70
71  def create_git_attributes
72    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{TEST_REPO_PATH} #{TEST_GITATTRIBUTES_REPO_PATH}),
73           chdir: SEED_STORAGE_PATH,
74           out: '/dev/null',
75           err: '/dev/null')
76
77    dir = File.join(SEED_STORAGE_PATH, 'with-git-attributes.git', 'info')
78
79    FileUtils.mkdir_p(dir)
80
81    File.open(File.join(dir, 'attributes'), 'w') do |handle|
82      handle.write <<-EOF.strip
83# This is a comment, it should be ignored.
84
85*.txt     text
86*.jpg     -text
87*.sh      eol=lf gitlab-language=shell
88*.haml.*  gitlab-language=haml
89foo/bar.* foo
90*.cgi     key=value?p1=v1&p2=v2
91/*.png    gitlab-language=png
92*.binary  binary
93/custom-highlighting/*.gitlab-custom gitlab-language=ruby
94/custom-highlighting/*.gitlab-cgi gitlab-language=erb?parent=json
95
96# This uses a tab instead of spaces to ensure the parser also supports this.
97*.md\tgitlab-language=markdown
98bla/bla.txt
99      EOF
100    end
101  end
102
103  def create_invalid_git_attributes
104    dir = File.join(SEED_STORAGE_PATH, 'with-invalid-git-attributes.git', 'info')
105
106    FileUtils.mkdir_p(dir)
107
108    enc = Encoding::UTF_16
109
110    File.open(File.join(dir, 'attributes'), 'w', encoding: enc) do |handle|
111      handle.write('# hello'.encode(enc))
112    end
113  end
114end
115