From 114f7bebe2ef056f99755cc89228a74091696435 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 23 Feb 2022 17:53:51 +0100
Subject: [PATCH] wip

---
 app/models/communication/website/git_file.rb  | 29 ++++++++++++++-----
 test/fixtures/communication/website/posts.yml |  2 ++
 .../communication/website/git_file_test.rb    | 10 ++-----
 3 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb
index 0a38acbe9..e88ce09b8 100644
--- a/app/models/communication/website/git_file.rb
+++ b/app/models/communication/website/git_file.rb
@@ -45,12 +45,14 @@ class Communication::Website::GitFile < ApplicationRecord
   def should_update?
     !should_destroy? &&
     (
-      different_path? || different_sha?
+      different_path? ||
+      different_sha?
     )
   end
 
   def should_destroy?
-    will_be_destroyed || path.nil?
+    will_be_destroyed ||
+    path.nil?
   end
 
   def path
@@ -71,8 +73,16 @@ class Communication::Website::GitFile < ApplicationRecord
   protected
 
   # Real sha on the git repo
+  def git_sha_for(path)
+    website.git_repository.git_sha path
+  end
+
+  def previous_git_sha
+    @previous_git_sha ||= git_sha_for(previous_path)
+  end
+
   def git_sha
-    @git_sha ||= website.git_repository.git_sha previous_path
+    @git_sha ||= git_sha_for(path)
   end
 
   # Based on content, with the provider's algorithm (sha1 or sha256)
@@ -81,14 +91,17 @@ class Communication::Website::GitFile < ApplicationRecord
   end
 
   def exists_on_git?
-    !git_sha.nil?
+    previous_git_sha.present? || # The file exists where it was last time
+    (
+      previous_path.nil? && # Never saved in the database
+      git_sha.present?      # but it exists in the git repo
+    )
   end
 
   def synchronized_with_git?
-    exists_on_git? &&
-    (
-      git_sha == previous_sha || git_sha == previous_sha256
-    )
+    exists_on_git? && # File exists
+    previous_path == path && # at the same place
+    previous_git_sha == previous_sha # with the same content
   end
 
   def different_path?
diff --git a/test/fixtures/communication/website/posts.yml b/test/fixtures/communication/website/posts.yml
index 14b3a688e..c2154ce20 100644
--- a/test/fixtures/communication/website/posts.yml
+++ b/test/fixtures/communication/website/posts.yml
@@ -36,3 +36,5 @@ test_post:
   website: website_with_github
   title: Test
   slug: test
+  published: true
+  published_at: 2010-11-28 00:00:00
diff --git a/test/models/communication/website/git_file_test.rb b/test/models/communication/website/git_file_test.rb
index 4f80fa1cb..bc94fbe1c 100644
--- a/test/models/communication/website/git_file_test.rb
+++ b/test/models/communication/website/git_file_test.rb
@@ -23,14 +23,8 @@
 require "test_helper"
 
 class Communication::Website::GitFileTest < ActiveSupport::TestCase
-  test "github should_create?" do
+  test "should_create? for a new file" do
     file = communication_website_git_files(:git_file_1)
-    # This is wrong! Fixtures must be prepared properly
-    assert_not file.should_create?
-  end
-
-  test "github correct sha" do
-    file = communication_website_git_files(:git_file_1)
-    assert_equal file.sha, '5d387e7e2da68026aeb0cfc2e6a67f509a3e1ff6'
+    assert file.should_create?
   end
 end
-- 
GitLab