lockfile.c: add a new public function unable_to_lock_message
Introducing a new unable_to_lock_message helper, which has nicer semantics than unable_to_lock_error and cleans up lockfile.c a little. Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Acked-by: Michael Haggerty <mhagger@alum.mit.edu>
Ronnie Sahlberg committed
Jun 20, 2014 at 07:42 UTC
6af926e8bcab15b42faeee9f2fe1c69b5fea1b15
2 files changed
+14
-10
cache.h
+2
@@ -559,6 +559,8 @@ struct lock_file {
559
#define LOCK_DIE_ON_ERROR 1
560
#define LOCK_NODEREF 2
561
extern int unable_to_lock_error(const char *path, int err);
562
+extern void unable_to_lock_message(const char *path, int err,
563
+ struct strbuf *buf);
564
extern NORETURN void unable_to_lock_index_die(const char *path, int err);
565
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
566
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
lockfile.c
+12
-10
@@ -157,33 +157,35 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
157
return lk->fd;
158
}
159
160
-static char *unable_to_lock_message(const char *path, int err)
160
+void unable_to_lock_message(const char *path, int err, struct strbuf *buf)
161
{
162
- struct strbuf buf = STRBUF_INIT;
163
-
162
if (err == EEXIST) {
165
- strbuf_addf(&buf, "Unable to create '%s.lock': %s.\n\n"
163
+ strbuf_addf(buf, "Unable to create '%s.lock': %s.\n\n"
164
"If no other git process is currently running, this probably means a\n"
165
"git process crashed in this repository earlier. Make sure no other git\n"
166
"process is running and remove the file manually to continue.",
167
absolute_path(path), strerror(err));
168
} else
171
- strbuf_addf(&buf, "Unable to create '%s.lock': %s",
169
+ strbuf_addf(buf, "Unable to create '%s.lock': %s",
170
absolute_path(path), strerror(err));
173
- return strbuf_detach(&buf, NULL);
171
}
172
173
int unable_to_lock_error(const char *path, int err)
174
{
178
- char *msg = unable_to_lock_message(path, err);
179
- error("%s", msg);
180
- free(msg);
175
+ struct strbuf buf = STRBUF_INIT;
176
+
177
+ unable_to_lock_message(path, err, &buf);
178
+ error("%s", buf.buf);
179
+ strbuf_release(&buf);
180
return -1;
181
}
182
183
NORETURN void unable_to_lock_index_die(const char *path, int err)
184
{
186
- die("%s", unable_to_lock_message(path, err));
185
+ struct strbuf buf = STRBUF_INIT;
186
+
187
+ unable_to_lock_message(path, err, &buf);
188
+ die("%s", buf.buf);
189
}
190
191
int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)