diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/GitProjectRepo.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/GitProjectRepo.java index 86754f8ab4..54c5a57b86 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/GitProjectRepo.java +++ b/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(); }