1# frozen_string_literal: true 2 3module RepoHelpers 4 extend self 5 6 # Text file in repo 7 # 8 # Ex. 9 # 10 # # Get object 11 # blob = RepoHelpers.text_blob 12 # 13 # blob.path # => 'files/js/commit.js.coffee' 14 # blob.data # => 'class Commit...' 15 # 16 # Build the options hash that's passed to Rugged::Commit#create 17 18 def sample_blob 19 OpenStruct.new( 20 oid: '5f53439ca4b009096571d3c8bc3d09d30e7431b3', 21 path: "files/js/commit.js.coffee", 22 data: <<eos 23class Commit 24 constructor: -> 25 $('.files .diff-file').each -> 26 new CommitFile(this) 27 28@Commit = Commit 29eos 30 ) 31 end 32 33 def sample_commit 34 OpenStruct.new( 35 id: "570e7b2abdd848b95f2f578043fc23bd6f6fd24d", 36 sha: "570e7b2abdd848b95f2f578043fc23bd6f6fd24d", 37 parent_id: '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9', 38 author_full_name: "Dmitriy Zaporozhets", 39 author_email: "dmitriy.zaporozhets@gmail.com", 40 files_changed_count: 2, 41 line_code: '2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_14', 42 line_code_path: 'files/ruby/popen.rb', 43 del_line_code: '2f6fcd96b88b36ce98c38da085c795a27d92a3dd_13_13', 44 message: <<eos 45Change some files 46Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> 47eos 48 ) 49 end 50 51 def another_sample_commit 52 OpenStruct.new( 53 id: "e56497bb5f03a90a51293fc6d516788730953899", 54 sha: "e56497bb5f03a90a51293fc6d516788730953899", 55 parent_id: '4cd80ccab63c82b4bad16faa5193fbd2aa06df40', 56 author_full_name: "Sytse Sijbrandij", 57 author_email: "sytse@gitlab.com", 58 files_changed_count: 1, 59 message: <<eos 60Add directory structure for tree_helper spec 61 62This directory structure is needed for a testing the method flatten_tree(tree) in the TreeHelper module 63 64See [merge request #275](https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/275#note_732774) 65 66See merge request !2 67eos 68 ) 69 end 70 71 def sample_big_commit 72 OpenStruct.new( 73 id: "913c66a37b4a45b9769037c55c2d238bd0942d2e", 74 sha: "913c66a37b4a45b9769037c55c2d238bd0942d2e", 75 author_full_name: "Dmitriy Zaporozhets", 76 author_email: "dmitriy.zaporozhets@gmail.com", 77 message: <<eos 78Files, encoding and much more 79Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> 80eos 81 ) 82 end 83 84 def sample_image_commit 85 OpenStruct.new( 86 id: "2f63565e7aac07bcdadb654e253078b727143ec4", 87 sha: "2f63565e7aac07bcdadb654e253078b727143ec4", 88 author_full_name: "Dmitriy Zaporozhets", 89 author_email: "dmitriy.zaporozhets@gmail.com", 90 old_blob_id: '33f3729a45c02fc67d00adb1b8bca394b0e761d9', 91 new_blob_id: '2f63565e7aac07bcdadb654e253078b727143ec4', 92 message: <<eos 93Modified image 94Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> 95eos 96 ) 97 end 98 99 def sample_compare(extra_changes = []) 100 changes = [ 101 { 102 line_code: 'a5cc2925ca8258af241be7e5b0381edf30266302_20_20', 103 file_path: '.gitignore' 104 }, 105 { 106 line_code: '7445606fbf8f3683cd42bdc54b05d7a0bc2dfc44_4_6', 107 file_path: '.gitmodules' 108 } 109 ] + extra_changes 110 111 commits = %w( 112 5937ac0a7beb003549fc5fd26fc247adbce4a52e 113 570e7b2abdd848b95f2f578043fc23bd6f6fd24d 114 6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9 115 d14d6c0abdd253381df51a723d58691b2ee1ab08 116 c1acaa58bbcbc3eafe538cb8274ba387047b69f8 117 ).reverse # last commit is recent one 118 119 OpenStruct.new( 120 source_branch: 'master', 121 target_branch: 'feature', 122 changes: changes, 123 commits: commits 124 ) 125 end 126 127 def create_file_in_repo( 128 project, start_branch, branch_name, filename, content, 129 commit_message: 'Add new content') 130 Files::CreateService.new( 131 project, 132 project.owner, 133 commit_message: commit_message, 134 start_branch: start_branch, 135 branch_name: branch_name, 136 file_path: filename, 137 file_content: content 138 ).execute 139 end 140 141 def commit_options(repo, index, target, ref, message) 142 options = {} 143 options[:tree] = index.write_tree(repo) 144 options[:author] = { 145 email: "test@example.com", 146 name: "Test Author", 147 time: Time.gm(2014, "mar", 3, 20, 15, 1) 148 } 149 options[:committer] = { 150 email: "test@example.com", 151 name: "Test Author", 152 time: Time.gm(2014, "mar", 3, 20, 15, 1) 153 } 154 options[:message] ||= message 155 options[:parents] = repo.empty? ? [] : [target].compact 156 options[:update_ref] = ref 157 158 options 159 end 160 161 # Writes a new commit to the repo and returns a Rugged::Commit. Replaces the 162 # contents of CHANGELOG with a single new line of text. 163 def new_commit_edit_old_file(repo) 164 oid = repo.write("I replaced the changelog with this text", :blob) 165 index = repo.index 166 index.read_tree(repo.head.target.tree) 167 index.add(path: "CHANGELOG", oid: oid, mode: 0100644) 168 169 options = commit_options( 170 repo, 171 index, 172 repo.head.target, 173 "HEAD", 174 "Edit CHANGELOG in its original location" 175 ) 176 177 sha = Rugged::Commit.create(repo, options) 178 repo.lookup(sha) 179 end 180 181 # Writes a new commit to the repo and returns a Rugged::Commit. Replaces the 182 # contents of the specified file_path with new text. 183 def new_commit_edit_new_file(repo, file_path, commit_message, text, branch = repo.head) 184 oid = repo.write(text, :blob) 185 index = repo.index 186 index.read_tree(branch.target.tree) 187 index.add(path: file_path, oid: oid, mode: 0100644) 188 options = commit_options(repo, index, branch.target, branch.canonical_name, commit_message) 189 sha = Rugged::Commit.create(repo, options) 190 repo.lookup(sha) 191 end 192 193 # Writes a new commit to the repo and returns a Rugged::Commit. Replaces the 194 # contents of encoding/CHANGELOG with new text. 195 def new_commit_edit_new_file_on_branch(repo, file_path, branch_name, commit_message, text) 196 branch = repo.branches[branch_name] 197 new_commit_edit_new_file(repo, file_path, commit_message, text, branch) 198 end 199 200 # Writes a new commit to the repo and returns a Rugged::Commit. Moves the 201 # CHANGELOG file to the encoding/ directory. 202 def new_commit_move_file(repo) 203 blob_oid = repo.head.target.tree.detect { |i| i[:name] == "CHANGELOG" }[:oid] 204 file_content = repo.lookup(blob_oid).content 205 oid = repo.write(file_content, :blob) 206 index = repo.index 207 index.read_tree(repo.head.target.tree) 208 index.add(path: "encoding/CHANGELOG", oid: oid, mode: 0100644) 209 index.remove("CHANGELOG") 210 211 options = commit_options(repo, index, repo.head.target, "HEAD", "Move CHANGELOG to encoding/") 212 213 sha = Rugged::Commit.create(repo, options) 214 repo.lookup(sha) 215 end 216end 217