From 5c99ec374cbd62fc59e6337d96f8d43414e1117b Mon Sep 17 00:00:00 2001 From: Michael Fellinger Date: Mon, 2 Aug 2021 13:39:48 +0200 Subject: [PATCH] fix git init race condition --- src/libfetchers/git.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc index bc1930138..d89763ccd 100644 --- a/src/libfetchers/git.cc +++ b/src/libfetchers/git.cc @@ -4,6 +4,7 @@ #include "tarfile.hh" #include "store-api.hh" #include "url-parts.hh" +#include "pathlocks.hh" #include #include @@ -317,11 +318,17 @@ struct GitInputScheme : InputScheme Path cacheDir = getCacheDir() + "/nix/gitv3/" + hashString(htSHA256, actualUrl).to_string(Base32, false); repoDir = cacheDir; + Path cacheDirLock = cacheDir + ".lock"; + createDirs(dirOf(cacheDir)); + AutoCloseFD lock = openLockFile(cacheDirLock, true); + lockFile(lock.get(), ltWrite, true); + if (!pathExists(cacheDir)) { - createDirs(dirOf(cacheDir)); runProgram("git", true, { "init", "--bare", repoDir }); } + deleteLockFile(cacheDirLock, lock.get()); + Path localRefFile = input.getRef()->compare(0, 5, "refs/") == 0 ? cacheDir + "/" + *input.getRef()