Skip to content

Commit b9badad

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Reviewed-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e929264 commit b9badad

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

refs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ extern char *shorten_unambiguous_ref(const char *refname, int strict);
292292
/** rename ref, return 0 on success **/
293293
extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);
294294

295-
extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg);
295+
extern int create_symref(const char *refname, const char *target, const char *logmsg);
296296

297297
enum action_on_err {
298298
UPDATE_REFS_MSG_ON_ERR,

refs/files-backend.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,72 +2811,71 @@ static int commit_ref_update(struct ref_lock *lock,
28112811
return 0;
28122812
}
28132813

2814-
int create_symref(const char *ref_target, const char *refs_heads_master,
2815-
const char *logmsg)
2814+
int create_symref(const char *refname, const char *target, const char *logmsg)
28162815
{
28172816
char *lockpath = NULL;
2818-
char ref[1000];
2817+
char buf[1000];
28192818
int fd, len, written;
2820-
char *git_HEAD = git_pathdup("%s", ref_target);
2819+
char *ref_path = git_pathdup("%s", refname);
28212820
unsigned char old_sha1[20], new_sha1[20];
28222821
struct strbuf err = STRBUF_INIT;
28232822

2824-
if (logmsg && read_ref(ref_target, old_sha1))
2823+
if (logmsg && read_ref(refname, old_sha1))
28252824
hashclr(old_sha1);
28262825

2827-
if (safe_create_leading_directories(git_HEAD) < 0)
2828-
return error("unable to create directory for %s", git_HEAD);
2826+
if (safe_create_leading_directories(ref_path) < 0)
2827+
return error("unable to create directory for %s", ref_path);
28292828

28302829
#ifndef NO_SYMLINK_HEAD
28312830
if (prefer_symlink_refs) {
2832-
unlink(git_HEAD);
2833-
if (!symlink(refs_heads_master, git_HEAD))
2831+
unlink(ref_path);
2832+
if (!symlink(target, ref_path))
28342833
goto done;
28352834
fprintf(stderr, "no symlink - falling back to symbolic ref\n");
28362835
}
28372836
#endif
28382837

2839-
len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
2840-
if (sizeof(ref) <= len) {
2841-
error("refname too long: %s", refs_heads_master);
2838+
len = snprintf(buf, sizeof(buf), "ref: %s\n", target);
2839+
if (sizeof(buf) <= len) {
2840+
error("refname too long: %s", target);
28422841
goto error_free_return;
28432842
}
2844-
lockpath = mkpathdup("%s.lock", git_HEAD);
2843+
lockpath = mkpathdup("%s.lock", ref_path);
28452844
fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
28462845
if (fd < 0) {
28472846
error("Unable to open %s for writing", lockpath);
28482847
goto error_free_return;
28492848
}
2850-
written = write_in_full(fd, ref, len);
2849+
written = write_in_full(fd, buf, len);
28512850
if (close(fd) != 0 || written != len) {
28522851
error("Unable to write to %s", lockpath);
28532852
goto error_unlink_return;
28542853
}
2855-
if (rename(lockpath, git_HEAD) < 0) {
2856-
error("Unable to create %s", git_HEAD);
2854+
if (rename(lockpath, ref_path) < 0) {
2855+
error("Unable to create %s", ref_path);
28572856
goto error_unlink_return;
28582857
}
2859-
if (adjust_shared_perm(git_HEAD)) {
2858+
if (adjust_shared_perm(ref_path)) {
28602859
error("Unable to fix permissions on %s", lockpath);
28612860
error_unlink_return:
28622861
unlink_or_warn(lockpath);
28632862
error_free_return:
28642863
free(lockpath);
2865-
free(git_HEAD);
2864+
free(ref_path);
28662865
return -1;
28672866
}
28682867
free(lockpath);
28692868

28702869
#ifndef NO_SYMLINK_HEAD
28712870
done:
28722871
#endif
2873-
if (logmsg && !read_ref(refs_heads_master, new_sha1) &&
2874-
log_ref_write(ref_target, old_sha1, new_sha1, logmsg, 0, &err)) {
2872+
if (logmsg && !read_ref(target, new_sha1) &&
2873+
log_ref_write(refname, old_sha1, new_sha1, logmsg, 0, &err)) {
28752874
error("%s", err.buf);
28762875
strbuf_release(&err);
28772876
}
28782877

2879-
free(git_HEAD);
2878+
free(ref_path);
28802879
return 0;
28812880
}
28822881

0 commit comments

Comments
 (0)