Skip to content
Snippets Groups Projects
Unverified Commit c7881eb6 authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

add to batch if new file, or if path changed, or if content changed

parent 5960f55c
No related branches found
No related tags found
No related merge requests found
......@@ -48,7 +48,8 @@ class Github
type: 'blob',
content: data
}
else # Existing file
elsif previous_path != path || file_sha(previous_path) != local_file_sha(data)
# Different path or content
@batch << {
path: previous_path,
mode: file[:mode],
......@@ -65,9 +66,11 @@ class Github
end
def commit_batch(commit_message)
new_tree = client.create_tree repository, @batch, base_tree: tree[:sha]
commit = client.create_commit repository, commit_message, new_tree[:sha], branch_sha
client.update_branch repository, default_branch, commit[:sha]
unless @batch.empty?
new_tree = client.create_tree repository, @batch, base_tree: tree[:sha]
commit = client.create_commit repository, commit_message, new_tree[:sha], branch_sha
client.update_branch repository, default_branch, commit[:sha]
end
@tree = nil
true
end
......@@ -170,6 +173,12 @@ class Github
sha
end
def local_file_sha(data)
# Git SHA-1 is calculated from the String "blob <length>\x00<contents>"
# Source: https://alblue.bandlem.com/2011/08/git-tip-of-week-objects.html
OpenSSL::Digest::SHA1.hexdigest "blob #{data.bytesize}\x00#{data}"
end
def default_branch
@default_branch ||= client.repo(repository)[:default_branch]
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment