Skip to content

Commit 0c6bd2b

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a0851ce commit 0c6bd2b

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

path.c

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "abspath.h"
66
#include "environment.h"
77
#include "gettext.h"
8-
#include "hex.h"
98
#include "repository.h"
109
#include "strbuf.h"
1110
#include "string-list.h"
@@ -647,58 +646,6 @@ void strbuf_git_common_path(struct strbuf *sb,
647646
va_end(args);
648647
}
649648

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-
702649
static struct passwd *getpw_str(const char *username, size_t len)
703650
{
704651
struct passwd *pw;

path.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ const char *git_path_fetch_head(struct repository *r);
173173
const char *git_path_shallow(struct repository *r);
174174

175175
int ends_with_path_components(const char *path, const char *components);
176-
int validate_headref(const char *ref);
177176

178177
int calc_shared_perm(int mode);
179178
int adjust_shared_perm(const char *path);

setup.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "environment.h"
55
#include "exec-cmd.h"
66
#include "gettext.h"
7+
#include "hex.h"
78
#include "object-name.h"
89
#include "refs.h"
910
#include "repository.h"
@@ -341,6 +342,58 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
341342
return ret;
342343
}
343344

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+
344397
/*
345398
* Test if it looks like we're at a git directory.
346399
* We want to see:

0 commit comments

Comments
 (0)