Skip to content

Commit 1b018fd

Browse files
Miklos Vajnaspearce
authored andcommitted
git branch -D: give a better error message when lockfile creation fails
Previously the old error message just told the user that it was not possible to delete the ref from the packed-refs file. Give instructions on how to resolve the problem. Signed-off-by: Miklos Vajna <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 6bbfd1f commit 1b018fd

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ struct lock_file {
489489
};
490490
#define LOCK_DIE_ON_ERROR 1
491491
#define LOCK_NODEREF 2
492+
extern int unable_to_lock_error(const char *path, int err);
492493
extern NORETURN void unable_to_lock_index_die(const char *path, int err);
493494
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
494495
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);

lockfile.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,32 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
155155
return lk->fd;
156156
}
157157

158-
159-
NORETURN void unable_to_lock_index_die(const char *path, int err)
158+
static char *unable_to_lock_message(const char *path, int err)
160159
{
160+
struct strbuf buf = STRBUF_INIT;
161+
161162
if (err == EEXIST) {
162-
die("Unable to create '%s.lock': %s.\n\n"
163+
strbuf_addf(&buf, "Unable to create '%s.lock': %s.\n\n"
163164
"If no other git process is currently running, this probably means a\n"
164165
"git process crashed in this repository earlier. Make sure no other git\n"
165166
"process is running and remove the file manually to continue.",
166167
path, strerror(err));
167-
} else {
168-
die("Unable to create '%s.lock': %s", path, strerror(err));
169-
}
168+
} else
169+
strbuf_addf(&buf, "Unable to create '%s.lock': %s", path, strerror(err));
170+
return strbuf_detach(&buf, NULL);
171+
}
172+
173+
int unable_to_lock_error(const char *path, int err)
174+
{
175+
char *msg = unable_to_lock_message(path, err);
176+
error("%s", msg);
177+
free(msg);
178+
return -1;
179+
}
180+
181+
NORETURN void unable_to_lock_index_die(const char *path, int err)
182+
{
183+
die("%s", unable_to_lock_message(path, err));
170184
}
171185

172186
int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)

refs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,10 @@ static int repack_without_ref(const char *refname)
972972
if (!found)
973973
return 0;
974974
fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0);
975-
if (fd < 0)
975+
if (fd < 0) {
976+
unable_to_lock_error(git_path("packed-refs"), errno);
976977
return error("cannot delete '%s' from packed refs", refname);
978+
}
977979

978980
for (list = packed_ref_list; list; list = list->next) {
979981
char line[PATH_MAX + 100];

0 commit comments

Comments
 (0)