From c7881eb6f71ee018f93062514206122c540f73bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Thu, 30 Dec 2021 12:22:50 +0100 Subject: [PATCH] add to batch if new file, or if path changed, or if content changed --- app/services/github.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/services/github.rb b/app/services/github.rb index 2dd82a463..f218accbf 100644 --- a/app/services/github.rb +++ b/app/services/github.rb @@ -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 -- GitLab