Skip to content

Commit

Permalink
lockfile: add accessor get_lock_file_path()
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Michael Haggerty authored and Junio C Hamano committed Aug 10, 2015
1 parent c99a4c2 commit b4fb09e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
15 changes: 8 additions & 7 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
struct string_list partial;
struct pathspec pathspec;
int refresh_flags = REFRESH_QUIET;
const char *ret;

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

old_index_env = getenv(INDEX_ENVIRONMENT);
setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);

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

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

commit_style = COMMIT_NORMAL;
return index_lock.filename.buf;
return get_lock_file_path(&index_lock);
}

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

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

discard_cache();
read_cache_from(false_lock.filename.buf);

return false_lock.filename.buf;
ret = get_lock_file_path(&false_lock);
read_cache_from(ret);
return ret;
}

static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
Expand Down
14 changes: 7 additions & 7 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -2056,9 +2056,9 @@ int git_config_set_multivar_in_file(const char *config_filename,
MAP_PRIVATE, in_fd, 0);
close(in_fd);

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

write_err_out:
ret = write_error(lock->filename.buf);
ret = write_error(get_lock_file_path(lock));
goto out_free;

}
Expand Down Expand Up @@ -2239,9 +2239,9 @@ int git_config_rename_section_in_file(const char *config_filename,

fstat(fileno(config_file), &st);

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

Expand All @@ -2262,7 +2262,7 @@ int git_config_rename_section_in_file(const char *config_filename,
}
store.baselen = strlen(new_name);
if (!store_write_section(out_fd, new_name)) {
ret = write_error(lock->filename.buf);
ret = write_error(get_lock_file_path(lock));
goto out;
}
/*
Expand All @@ -2288,7 +2288,7 @@ int git_config_rename_section_in_file(const char *config_filename,
continue;
length = strlen(output);
if (write_in_full(out_fd, output, length) != length) {
ret = write_error(lock->filename.buf);
ret = write_error(get_lock_file_path(lock));
goto out;
}
}
Expand Down
7 changes: 7 additions & 0 deletions lockfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
return lk->fp;
}

const char *get_lock_file_path(struct lock_file *lk)
{
if (!lk->active)
die("BUG: get_lock_file_path() called for unlocked object");
return lk->filename.buf;
}

int get_lock_file_fd(struct lock_file *lk)
{
if (!lk->active)
Expand Down
6 changes: 6 additions & 0 deletions lockfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ extern NORETURN void unable_to_lock_die(const char *path, int err);
*/
extern FILE *fdopen_lock_file(struct lock_file *lk, const char *mode);

/*
* Return the path of the lockfile. The return value is a pointer to a
* field within the lock_file object and should not be freed.
*/
extern const char *get_lock_file_path(struct lock_file *lk);

extern int get_lock_file_fd(struct lock_file *lk);
extern FILE *get_lock_file_fp(struct lock_file *lk);

Expand Down
6 changes: 3 additions & 3 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3184,7 +3184,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
write_in_full(fd, &term, 1) != 1 ||
close_ref(lock) < 0) {
int save_errno = errno;
error("Couldn't write %s", lock->lk->filename.buf);
error("Couldn't write %s", get_lock_file_path(lock->lk));
unlock_ref(lock);
errno = save_errno;
return -1;
Expand Down Expand Up @@ -4241,7 +4241,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
cb.newlog = fdopen_lock_file(&reflog_lock, "w");
if (!cb.newlog) {
error("cannot fdopen %s (%s)",
reflog_lock.filename.buf, strerror(errno));
get_lock_file_path(&reflog_lock), strerror(errno));
goto failure;
}
}
Expand Down Expand Up @@ -4271,7 +4271,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
close_ref(lock) < 0)) {
status |= error("couldn't write %s",
lock->lk->filename.buf);
get_lock_file_path(lock->lk));
rollback_lock_file(&reflog_lock);
} else if (commit_lock_file(&reflog_lock)) {
status |= error("unable to commit reflog '%s' (%s)",
Expand Down
6 changes: 3 additions & 3 deletions shallow.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
if (write_shallow_commits(&sb, 0, extra)) {
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
die_errno("failed to write to %s",
shallow_lock->filename.buf);
*alternate_shallow_file = shallow_lock->filename.buf;
get_lock_file_path(shallow_lock));
*alternate_shallow_file = get_lock_file_path(shallow_lock);
} else
/*
* is_repository_shallow() sees empty string as "no
Expand Down Expand Up @@ -314,7 +314,7 @@ void prune_shallow(int show_only)
if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
die_errno("failed to write to %s",
shallow_lock.filename.buf);
get_lock_file_path(&shallow_lock));
commit_lock_file(&shallow_lock);
} else {
unlink(git_path("shallow"));
Expand Down

0 comments on commit b4fb09e

Please sign in to comment.