summaryrefslogtreecommitdiff
path: root/spec/support/bare_repo_operations.rb
diff options
context:
space:
mode:
authorSean McGivern <sean@mcgivern.me.uk>2017-11-01 09:43:21 +0000
committerSean McGivern <sean@mcgivern.me.uk>2017-11-01 09:43:21 +0000
commit7300162727b15e8daccd2d0214b2ca527c6c9f55 (patch)
tree99fedb085d865bbb4ab253b758d421f8e93c40ab /spec/support/bare_repo_operations.rb
parent6306e797acca358c79c120e5b12c29a5ec604571 (diff)
parent56e53556c538236a040dcd0e499cc198d66e7cf6 (diff)
downloadgitlab-ce-7300162727b15e8daccd2d0214b2ca527c6c9f55.tar.gz
Merge branch 'feature/migrate-find-wiki-file-to-gitaly' into 'master'
Migrate Gitlab::Git::Wiki#file to Gitaly Closes gitaly#689 See merge request gitlab-org/gitlab-ce!15014
Diffstat (limited to 'spec/support/bare_repo_operations.rb')
-rw-r--r--spec/support/bare_repo_operations.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/support/bare_repo_operations.rb b/spec/support/bare_repo_operations.rb
new file mode 100644
index 00000000000..38d11992dc2
--- /dev/null
+++ b/spec/support/bare_repo_operations.rb
@@ -0,0 +1,60 @@
+require 'zlib'
+
+class BareRepoOperations
+ # The ID of empty tree.
+ # See http://stackoverflow.com/a/40884093/1856239 and https://github.com/git/git/blob/3ad8b5bf26362ac67c9020bf8c30eee54a84f56d/cache.h#L1011-L1012
+ EMPTY_TREE_ID = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'.freeze
+
+ include Gitlab::Popen
+
+ def initialize(path_to_repo)
+ @path_to_repo = path_to_repo
+ end
+
+ # Based on https://stackoverflow.com/a/25556917/1856239
+ def commit_file(file, dst_path, branch = 'master')
+ head_id = execute(['show', '--format=format:%H', '--no-patch', branch], allow_failure: true)[0] || EMPTY_TREE_ID
+
+ execute(['read-tree', '--empty'])
+ execute(['read-tree', head_id])
+
+ blob_id = execute(['hash-object', '--stdin', '-w']) do |stdin|
+ stdin.write(file.read)
+ end
+
+ execute(['update-index', '--add', '--cacheinfo', '100644', blob_id[0], dst_path])
+
+ tree_id = execute(['write-tree'])
+
+ commit_tree_args = ['commit-tree', tree_id[0], '-m', "Add #{dst_path}"]
+ commit_tree_args += ['-p', head_id] unless head_id == EMPTY_TREE_ID
+ commit_id = execute(commit_tree_args)
+
+ execute(['update-ref', "refs/heads/#{branch}", commit_id[0]])
+ end
+
+ private
+
+ def execute(args, allow_failure: false)
+ output, status = popen(base_args + args, nil) do |stdin|
+ yield stdin if block_given?
+ end
+
+ unless status.zero?
+ if allow_failure
+ return []
+ else
+ raise "Got a non-zero exit code while calling out `#{args.join(' ')}`: #{output}"
+ end
+ end
+
+ output.split("\n")
+ end
+
+ def base_args
+ [
+ Gitlab.config.git.bin_path,
+ "--git-dir=#{@path_to_repo}"
+ ]
+ end
+end