Просмотр исходного кода

[git-bridge] Added warning on non-fresh repo initialisations (#32432)

* [git-bridge] Added warning on non-fresh repo initialisations

Addresses a TODO by adding a warning when trying to initialise a repo that already exists and is not fresh.

GitOrigin-RevId: f801df9e1c77bfbc8a9a4101f01d5a71c383dc0f
Miguel Serrano 6 месяцев назад
Родитель
Сommit
a6fd4c4f5a

+ 16 - 4
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/GitProjectRepo.java

@@ -66,10 +66,22 @@ public class GitProjectRepo implements ProjectRepo {
     initRepositoryField(repoStore);
     Preconditions.checkState(repository.isPresent());
     Repository repo = this.repository.get();
-    // TODO: assert that this is a fresh repo. At the moment, we can't be
-    // sure whether the repo to be init'd doesn't exist or is just fresh
-    // and we crashed / aborted while committing
-    if (repo.getObjectDatabase().exists()) return;
+    if (repo.getObjectDatabase().exists()) {
+      boolean isFreshRepo = true;
+      try {
+        isFreshRepo = repo.resolve("HEAD") == null;
+      } catch (Exception e) {
+        Log.warn(
+            "[{}] initRepo could not resolve HEAD to determine repo freshness", projectName, e);
+      }
+      if (!isFreshRepo) {
+        // if the repo has commits (due to a previous crash/abort while it was being initialised),
+        // we're returning early, expecting the repo to be reset to a clean state.
+        // Logging a warning to track this scenario.
+        Log.warn("[{}] initRepo called on repo that already has commits", projectName);
+      }
+      return;
+    }
     repo.create();
   }