create_symref: modernize variable names

Once upon a time, create_symref() was used only to point
HEAD at a branch name, and the variable names reflect that
(e.g., calling the path git_HEAD). However, it is much more
generic these days (and has been for some time). Let's
update the variable names to make it easier to follow:

  - `ref_target` is now just `refname`. This is closer to
    the `ref` that is already in `cache.h`, but with the
    extra twist that "name" makes it clear this is the name
    and not a ref struct. Dropping "target" hopefully makes
    it clear that we are talking about the symref itself,
    not what it points to.

  - `git_HEAD` is now `ref_path`; the on-disk path
    corresponding to `ref`.

  - `refs_heads_master` is now just `target`; i.e., what the
    symref points at. This term also matches what is in
    the symlink(2) manpage (at least on Linux).

  - the buffer to hold the symref file's contents was simply
    called `ref`. It's now `buf` (admittedly also generic,
    but at least not actively introducing confusion with the
    other variable holding the refname).

Signed-off-by: Jeff King <peff@peff.net>
Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2015-12-29 00:56:44 -05:00 committed by Junio C Hamano
parent e929264e8d
commit b9badadd06
2 changed files with 21 additions and 22 deletions

2
refs.h
View File

@ -292,7 +292,7 @@ extern char *shorten_unambiguous_ref(const char *refname, int strict);
/** rename ref, return 0 on success **/ /** rename ref, return 0 on success **/
extern int rename_ref(const char *oldref, const char *newref, const char *logmsg); extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);
extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg); extern int create_symref(const char *refname, const char *target, const char *logmsg);
enum action_on_err { enum action_on_err {
UPDATE_REFS_MSG_ON_ERR, UPDATE_REFS_MSG_ON_ERR,

View File

@ -2811,58 +2811,57 @@ static int commit_ref_update(struct ref_lock *lock,
return 0; return 0;
} }
int create_symref(const char *ref_target, const char *refs_heads_master, int create_symref(const char *refname, const char *target, const char *logmsg)
const char *logmsg)
{ {
char *lockpath = NULL; char *lockpath = NULL;
char ref[1000]; char buf[1000];
int fd, len, written; int fd, len, written;
char *git_HEAD = git_pathdup("%s", ref_target); char *ref_path = git_pathdup("%s", refname);
unsigned char old_sha1[20], new_sha1[20]; unsigned char old_sha1[20], new_sha1[20];
struct strbuf err = STRBUF_INIT; struct strbuf err = STRBUF_INIT;
if (logmsg && read_ref(ref_target, old_sha1)) if (logmsg && read_ref(refname, old_sha1))
hashclr(old_sha1); hashclr(old_sha1);
if (safe_create_leading_directories(git_HEAD) < 0) if (safe_create_leading_directories(ref_path) < 0)
return error("unable to create directory for %s", git_HEAD); return error("unable to create directory for %s", ref_path);
#ifndef NO_SYMLINK_HEAD #ifndef NO_SYMLINK_HEAD
if (prefer_symlink_refs) { if (prefer_symlink_refs) {
unlink(git_HEAD); unlink(ref_path);
if (!symlink(refs_heads_master, git_HEAD)) if (!symlink(target, ref_path))
goto done; goto done;
fprintf(stderr, "no symlink - falling back to symbolic ref\n"); fprintf(stderr, "no symlink - falling back to symbolic ref\n");
} }
#endif #endif
len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master); len = snprintf(buf, sizeof(buf), "ref: %s\n", target);
if (sizeof(ref) <= len) { if (sizeof(buf) <= len) {
error("refname too long: %s", refs_heads_master); error("refname too long: %s", target);
goto error_free_return; goto error_free_return;
} }
lockpath = mkpathdup("%s.lock", git_HEAD); lockpath = mkpathdup("%s.lock", ref_path);
fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
if (fd < 0) { if (fd < 0) {
error("Unable to open %s for writing", lockpath); error("Unable to open %s for writing", lockpath);
goto error_free_return; goto error_free_return;
} }
written = write_in_full(fd, ref, len); written = write_in_full(fd, buf, len);
if (close(fd) != 0 || written != len) { if (close(fd) != 0 || written != len) {
error("Unable to write to %s", lockpath); error("Unable to write to %s", lockpath);
goto error_unlink_return; goto error_unlink_return;
} }
if (rename(lockpath, git_HEAD) < 0) { if (rename(lockpath, ref_path) < 0) {
error("Unable to create %s", git_HEAD); error("Unable to create %s", ref_path);
goto error_unlink_return; goto error_unlink_return;
} }
if (adjust_shared_perm(git_HEAD)) { if (adjust_shared_perm(ref_path)) {
error("Unable to fix permissions on %s", lockpath); error("Unable to fix permissions on %s", lockpath);
error_unlink_return: error_unlink_return:
unlink_or_warn(lockpath); unlink_or_warn(lockpath);
error_free_return: error_free_return:
free(lockpath); free(lockpath);
free(git_HEAD); free(ref_path);
return -1; return -1;
} }
free(lockpath); free(lockpath);
@ -2870,13 +2869,13 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
#ifndef NO_SYMLINK_HEAD #ifndef NO_SYMLINK_HEAD
done: done:
#endif #endif
if (logmsg && !read_ref(refs_heads_master, new_sha1) && if (logmsg && !read_ref(target, new_sha1) &&
log_ref_write(ref_target, old_sha1, new_sha1, logmsg, 0, &err)) { log_ref_write(refname, old_sha1, new_sha1, logmsg, 0, &err)) {
error("%s", err.buf); error("%s", err.buf);
strbuf_release(&err); strbuf_release(&err);
} }
free(git_HEAD); free(ref_path);
return 0; return 0;
} }