path: move `validate_headref()` to its only user

While `validate_headref()` is only called from `is_git_directory()` in "setup.c", it is currently implemented in "path.c". Move it over such that it becomes clear that it is only really used during setup in order to discover repositories. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 7, 2024 at 06:52 UTC 0c6bd2b81d1e18dfa1e143c354c554cca34b3685
3 files changed +53 -54
path.c
-53
@@ -5,7 +5,6 @@
5 #include "abspath.h"
6 #include "environment.h"
7 #include "gettext.h"
8 -#include "hex.h"
8 #include "repository.h"
9 #include "strbuf.h"
10 #include "string-list.h"
@@ -647,58 +646,6 @@ void strbuf_git_common_path(struct strbuf *sb,
646 va_end(args);
647 }
648
650 -int validate_headref(const char *path)
651 -{
652 - struct stat st;
653 - char buffer[256];
654 - const char *refname;
655 - struct object_id oid;
656 - int fd;
657 - ssize_t len;
658 -
659 - if (lstat(path, &st) < 0)
660 - return -1;
661 -
662 - /* Make sure it is a "refs/.." symlink */
663 - if (S_ISLNK(st.st_mode)) {
664 - len = readlink(path, buffer, sizeof(buffer)-1);
665 - if (len >= 5 && !memcmp("refs/", buffer, 5))
666 - return 0;
667 - return -1;
668 - }
669 -
670 - /*
671 - * Anything else, just open it and try to see if it is a symbolic ref.
672 - */
673 - fd = open(path, O_RDONLY);
674 - if (fd < 0)
675 - return -1;
676 - len = read_in_full(fd, buffer, sizeof(buffer)-1);
677 - close(fd);
678 -
679 - if (len < 0)
680 - return -1;
681 - buffer[len] = '\0';
682 -
683 - /*
684 - * Is it a symbolic ref?
685 - */
686 - if (skip_prefix(buffer, "ref:", &refname)) {
687 - while (isspace(*refname))
688 - refname++;
689 - if (starts_with(refname, "refs/"))
690 - return 0;
691 - }
692 -
693 - /*
694 - * Is this a detached HEAD?
695 - */
696 - if (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN)
697 - return 0;
698 -
699 - return -1;
700 -}
701 -
649 static struct passwd *getpw_str(const char *username, size_t len)
650 {
651 struct passwd *pw;
path.h
-1
@@ -173,7 +173,6 @@ const char *git_path_fetch_head(struct repository *r);
173 const char *git_path_shallow(struct repository *r);
174
175 int ends_with_path_components(const char *path, const char *components);
176 -int validate_headref(const char *ref);
176
177 int calc_shared_perm(int mode);
178 int adjust_shared_perm(const char *path);
setup.c
+53
@@ -4,6 +4,7 @@
4 #include "environment.h"
5 #include "exec-cmd.h"
6 #include "gettext.h"
7 +#include "hex.h"
8 #include "object-name.h"
9 #include "refs.h"
10 #include "repository.h"
@@ -341,6 +342,58 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
342 return ret;
343 }
344
345 +static int validate_headref(const char *path)
346 +{
347 + struct stat st;
348 + char buffer[256];
349 + const char *refname;
350 + struct object_id oid;
351 + int fd;
352 + ssize_t len;
353 +
354 + if (lstat(path, &st) < 0)
355 + return -1;
356 +
357 + /* Make sure it is a "refs/.." symlink */
358 + if (S_ISLNK(st.st_mode)) {
359 + len = readlink(path, buffer, sizeof(buffer)-1);
360 + if (len >= 5 && !memcmp("refs/", buffer, 5))
361 + return 0;
362 + return -1;
363 + }
364 +
365 + /*
366 + * Anything else, just open it and try to see if it is a symbolic ref.
367 + */
368 + fd = open(path, O_RDONLY);
369 + if (fd < 0)
370 + return -1;
371 + len = read_in_full(fd, buffer, sizeof(buffer)-1);
372 + close(fd);
373 +
374 + if (len < 0)
375 + return -1;
376 + buffer[len] = '\0';
377 +
378 + /*
379 + * Is it a symbolic ref?
380 + */
381 + if (skip_prefix(buffer, "ref:", &refname)) {
382 + while (isspace(*refname))
383 + refname++;
384 + if (starts_with(refname, "refs/"))
385 + return 0;
386 + }
387 +
388 + /*
389 + * Is this a detached HEAD?
390 + */
391 + if (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN)
392 + return 0;
393 +
394 + return -1;
395 +}
396 +
397 /*
398 * Test if it looks like we're at a git directory.
399 * We want to see: