Skip to content

Commit b4fb09e

Browse files
mhaggergitster
authored andcommitted
lockfile: add accessor get_lock_file_path()
Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c99a4c2 commit b4fb09e

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

builtin/commit.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
324324
struct string_list partial;
325325
struct pathspec pathspec;
326326
int refresh_flags = REFRESH_QUIET;
327+
const char *ret;
327328

328329
if (is_status)
329330
refresh_flags |= REFRESH_UNMERGED;
@@ -344,7 +345,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
344345
die(_("unable to create temporary index"));
345346

346347
old_index_env = getenv(INDEX_ENVIRONMENT);
347-
setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
348+
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
348349

349350
if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
350351
die(_("interactive add failed"));
@@ -355,7 +356,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
355356
unsetenv(INDEX_ENVIRONMENT);
356357

357358
discard_cache();
358-
read_cache_from(index_lock.filename.buf);
359+
read_cache_from(get_lock_file_path(&index_lock));
359360
if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
360361
if (reopen_lock_file(&index_lock) < 0)
361362
die(_("unable to write index file"));
@@ -365,7 +366,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
365366
warning(_("Failed to update main cache tree"));
366367

367368
commit_style = COMMIT_NORMAL;
368-
return index_lock.filename.buf;
369+
return get_lock_file_path(&index_lock);
369370
}
370371

371372
/*
@@ -388,7 +389,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
388389
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
389390
die(_("unable to write new_index file"));
390391
commit_style = COMMIT_NORMAL;
391-
return index_lock.filename.buf;
392+
return get_lock_file_path(&index_lock);
392393
}
393394

394395
/*
@@ -475,9 +476,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
475476
die(_("unable to write temporary index file"));
476477

477478
discard_cache();
478-
read_cache_from(false_lock.filename.buf);
479-
480-
return false_lock.filename.buf;
479+
ret = get_lock_file_path(&false_lock);
480+
read_cache_from(ret);
481+
return ret;
481482
}
482483

483484
static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,

config.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,9 +2056,9 @@ int git_config_set_multivar_in_file(const char *config_filename,
20562056
MAP_PRIVATE, in_fd, 0);
20572057
close(in_fd);
20582058

2059-
if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
2059+
if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
20602060
error("chmod on %s failed: %s",
2061-
lock->filename.buf, strerror(errno));
2061+
get_lock_file_path(lock), strerror(errno));
20622062
ret = CONFIG_NO_WRITE;
20632063
goto out_free;
20642064
}
@@ -2138,7 +2138,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
21382138
return ret;
21392139

21402140
write_err_out:
2141-
ret = write_error(lock->filename.buf);
2141+
ret = write_error(get_lock_file_path(lock));
21422142
goto out_free;
21432143

21442144
}
@@ -2239,9 +2239,9 @@ int git_config_rename_section_in_file(const char *config_filename,
22392239

22402240
fstat(fileno(config_file), &st);
22412241

2242-
if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
2242+
if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
22432243
ret = error("chmod on %s failed: %s",
2244-
lock->filename.buf, strerror(errno));
2244+
get_lock_file_path(lock), strerror(errno));
22452245
goto out;
22462246
}
22472247

@@ -2262,7 +2262,7 @@ int git_config_rename_section_in_file(const char *config_filename,
22622262
}
22632263
store.baselen = strlen(new_name);
22642264
if (!store_write_section(out_fd, new_name)) {
2265-
ret = write_error(lock->filename.buf);
2265+
ret = write_error(get_lock_file_path(lock));
22662266
goto out;
22672267
}
22682268
/*
@@ -2288,7 +2288,7 @@ int git_config_rename_section_in_file(const char *config_filename,
22882288
continue;
22892289
length = strlen(output);
22902290
if (write_in_full(out_fd, output, length) != length) {
2291-
ret = write_error(lock->filename.buf);
2291+
ret = write_error(get_lock_file_path(lock));
22922292
goto out;
22932293
}
22942294
}

lockfile.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
364364
return lk->fp;
365365
}
366366

367+
const char *get_lock_file_path(struct lock_file *lk)
368+
{
369+
if (!lk->active)
370+
die("BUG: get_lock_file_path() called for unlocked object");
371+
return lk->filename.buf;
372+
}
373+
367374
int get_lock_file_fd(struct lock_file *lk)
368375
{
369376
if (!lk->active)

lockfile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ extern NORETURN void unable_to_lock_die(const char *path, int err);
203203
*/
204204
extern FILE *fdopen_lock_file(struct lock_file *lk, const char *mode);
205205

206+
/*
207+
* Return the path of the lockfile. The return value is a pointer to a
208+
* field within the lock_file object and should not be freed.
209+
*/
210+
extern const char *get_lock_file_path(struct lock_file *lk);
211+
206212
extern int get_lock_file_fd(struct lock_file *lk);
207213
extern FILE *get_lock_file_fp(struct lock_file *lk);
208214

refs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,7 +3184,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
31843184
write_in_full(fd, &term, 1) != 1 ||
31853185
close_ref(lock) < 0) {
31863186
int save_errno = errno;
3187-
error("Couldn't write %s", lock->lk->filename.buf);
3187+
error("Couldn't write %s", get_lock_file_path(lock->lk));
31883188
unlock_ref(lock);
31893189
errno = save_errno;
31903190
return -1;
@@ -4241,7 +4241,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
42414241
cb.newlog = fdopen_lock_file(&reflog_lock, "w");
42424242
if (!cb.newlog) {
42434243
error("cannot fdopen %s (%s)",
4244-
reflog_lock.filename.buf, strerror(errno));
4244+
get_lock_file_path(&reflog_lock), strerror(errno));
42454245
goto failure;
42464246
}
42474247
}
@@ -4271,7 +4271,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
42714271
write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
42724272
close_ref(lock) < 0)) {
42734273
status |= error("couldn't write %s",
4274-
lock->lk->filename.buf);
4274+
get_lock_file_path(lock->lk));
42754275
rollback_lock_file(&reflog_lock);
42764276
} else if (commit_lock_file(&reflog_lock)) {
42774277
status |= error("unable to commit reflog '%s' (%s)",

shallow.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
267267
if (write_shallow_commits(&sb, 0, extra)) {
268268
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
269269
die_errno("failed to write to %s",
270-
shallow_lock->filename.buf);
271-
*alternate_shallow_file = shallow_lock->filename.buf;
270+
get_lock_file_path(shallow_lock));
271+
*alternate_shallow_file = get_lock_file_path(shallow_lock);
272272
} else
273273
/*
274274
* is_repository_shallow() sees empty string as "no
@@ -314,7 +314,7 @@ void prune_shallow(int show_only)
314314
if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
315315
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
316316
die_errno("failed to write to %s",
317-
shallow_lock.filename.buf);
317+
get_lock_file_path(&shallow_lock));
318318
commit_lock_file(&shallow_lock);
319319
} else {
320320
unlink(git_path("shallow"));

0 commit comments

Comments
 (0)