Skip to content

Commit 33286dc

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: fix UX issue when .lock file exists
We use the lockfile API to avoid multiple Git processes from writing to the commit-graph file in the .git/objects/info directory. In some cases, this directory may not exist, so we check for its existence. The existing code does the following when acquiring the lock: 1. Try to acquire the lock. 2. If it fails, try to create the .git/object/info directory. 3. Try to acquire the lock, failing if necessary. The problem is that if the lockfile exists, then the mkdir fails, giving an error that doesn't help the user: "fatal: cannot mkdir .git/objects/info: File exists" While technically this honors the lockfile, it does not help the user. Instead, do the following: 1. Check for existence of .git/objects/info; create if necessary. 2. Try to acquire the lock, failing if necessary. The new output looks like: fatal: Unable to create '<dir>/.git/objects/info/commit-graph.lock': File exists. Another git process seems to be running in this repository, e.g. an editor opened by 'git commit'. Please make sure all processes are terminated then try again. If it still fails, a git process may have crashed in this repository earlier: remove the file manually to continue. Helped-by: Jeff King <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1472978 commit 33286dc

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

commit-graph.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "config.h"
3+
#include "dir.h"
34
#include "git-compat-util.h"
45
#include "lockfile.h"
56
#include "pack.h"
@@ -640,7 +641,6 @@ void write_commit_graph(const char *obj_dir,
640641
struct hashfile *f;
641642
uint32_t i, count_distinct = 0;
642643
char *graph_name;
643-
int fd;
644644
struct lock_file lk = LOCK_INIT;
645645
uint32_t chunk_ids[5];
646646
uint64_t chunk_offsets[5];
@@ -754,23 +754,11 @@ void write_commit_graph(const char *obj_dir,
754754
compute_generation_numbers(&commits);
755755

756756
graph_name = get_commit_graph_filename(obj_dir);
757-
fd = hold_lock_file_for_update(&lk, graph_name, 0);
758-
759-
if (fd < 0) {
760-
struct strbuf folder = STRBUF_INIT;
761-
strbuf_addstr(&folder, graph_name);
762-
strbuf_setlen(&folder, strrchr(folder.buf, '/') - folder.buf);
763-
764-
if (mkdir(folder.buf, 0777) < 0)
765-
die_errno(_("cannot mkdir %s"), folder.buf);
766-
strbuf_release(&folder);
767-
768-
fd = hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
769-
770-
if (fd < 0)
771-
die_errno("unable to create '%s'", graph_name);
772-
}
757+
if (safe_create_leading_directories(graph_name))
758+
die_errno(_("unable to create leading directories of %s"),
759+
graph_name);
773760

761+
hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
774762
f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
775763

776764
hashwrite_be32(f, GRAPH_SIGNATURE);

0 commit comments

Comments
 (0)