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>

Jeff King committed Dec 29, 2015 at 00:56 UTC b9badadd06ae307c5e1e0e7c36985a1360cabc22
2 files changed +21 -22
refs.h
+1 -1
@@ -292,7 +292,7 @@ extern char *shorten_unambiguous_ref(const char *refname, int strict);
292 /** rename ref, return 0 on success **/
293 extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);
294
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);
296
297 enum action_on_err {
298 UPDATE_REFS_MSG_ON_ERR,
refs/files-backend.c
+20 -21
@@ -2811,58 +2811,57 @@ static int commit_ref_update(struct ref_lock *lock,
2811 return 0;
2812 }
2813
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)
2815 {
2816 char *lockpath = NULL;
2818 - char ref[1000];
2817 + char buf[1000];
2818 int fd, len, written;
2820 - char *git_HEAD = git_pathdup("%s", ref_target);
2819 + char *ref_path = git_pathdup("%s", refname);
2820 unsigned char old_sha1[20], new_sha1[20];
2821 struct strbuf err = STRBUF_INIT;
2822
2824 - if (logmsg && read_ref(ref_target, old_sha1))
2823 + if (logmsg && read_ref(refname, old_sha1))
2824 hashclr(old_sha1);
2825
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);
2828
2829 #ifndef NO_SYMLINK_HEAD
2830 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))
2833 goto done;
2834 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
2835 }
2836 #endif
2837
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);
2841 goto error_free_return;
2842 }
2844 - lockpath = mkpathdup("%s.lock", git_HEAD);
2843 + lockpath = mkpathdup("%s.lock", ref_path);
2844 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
2845 if (fd < 0) {
2846 error("Unable to open %s for writing", lockpath);
2847 goto error_free_return;
2848 }
2850 - written = write_in_full(fd, ref, len);
2849 + written = write_in_full(fd, buf, len);
2850 if (close(fd) != 0 || written != len) {
2851 error("Unable to write to %s", lockpath);
2852 goto error_unlink_return;
2853 }
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);
2856 goto error_unlink_return;
2857 }
2859 - if (adjust_shared_perm(git_HEAD)) {
2858 + if (adjust_shared_perm(ref_path)) {
2859 error("Unable to fix permissions on %s", lockpath);
2860 error_unlink_return:
2861 unlink_or_warn(lockpath);
2862 error_free_return:
2863 free(lockpath);
2865 - free(git_HEAD);
2864 + free(ref_path);
2865 return -1;
2866 }
2867 free(lockpath);
@@ -2870,13 +2869,13 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
2869 #ifndef NO_SYMLINK_HEAD
2870 done:
2871 #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)) {
2874 error("%s", err.buf);
2875 strbuf_release(&err);
2876 }
2877
2879 - free(git_HEAD);
2878 + free(ref_path);
2879 return 0;
2880 }
2881