include non-packed refs in results from got_ref_list()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
diff --git a/lib/reference.c b/lib/reference.c
index fd2f57f..6305af4 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -17,6 +17,7 @@
#include <sys/types.h>
#include <sys/queue.h>
+#include <dirent.h>
#include <sha1.h>
#include <stdio.h>
#include <stdlib.h>
@@ -240,24 +241,40 @@ open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
static const struct got_error *
open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
- const char *refname)
+ const char *name)
{
const struct got_error *err = NULL;
- char *path_ref;
- char *normpath;
+ char *path = NULL;
+ char *normpath = NULL;
+ char *absname = NULL;
+ int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
+ int ref_is_well_known = is_well_known_ref(name);
+
+ if (ref_is_absolute || ref_is_well_known) {
+ if (asprintf(&path, "%s/%s", path_refs, name) == -1)
+ return got_error_from_errno();
+ absname = (char *)name;
+ } else {
+ if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
+ return got_error_from_errno();
- if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
- return got_error_from_errno();
+ if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ }
- normpath = got_path_normalize(path_ref);
+ normpath = got_path_normalize(path);
if (normpath == NULL) {
err = got_error_from_errno();
goto done;
}
- err = parse_ref_file(ref, refname, normpath);
+ err = parse_ref_file(ref, absname, normpath);
done:
- free(path_ref);
+ if (!ref_is_absolute && !ref_is_well_known)
+ free(absname);
+ free(path);
free(normpath);
return err;
}
@@ -449,11 +466,70 @@ append_ref(struct got_reflist_head *refs, struct got_reference *ref,
return NULL;
}
+static const struct got_error *
+gather_refs(struct got_reflist_head *refs, const char *path_refs,
+ const char *subdir, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ DIR *d = NULL;
+ char *path_subdir;
+
+ if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
+ return got_error_from_errno();
+
+ d = opendir(path_subdir);
+ if (d == NULL)
+ goto done;
+
+ while (1) {
+ struct dirent *dent;
+ struct got_reference *ref;
+ char *child;
+
+ dent = readdir(d);
+ if (dent == NULL)
+ break;
+
+ if (strcmp(dent->d_name, ".") == 0 ||
+ strcmp(dent->d_name, "..") == 0)
+ continue;
+
+ switch (dent->d_type) {
+ case DT_REG:
+ err = open_ref(&ref, path_refs, subdir, dent->d_name);
+ if (err && err->code != GOT_ERR_NOT_REF)
+ goto done;
+ if (ref) {
+ err = append_ref(refs, ref, repo);
+ if (err)
+ goto done;
+ }
+ break;
+ case DT_DIR:
+ if (asprintf(&child, "%s%s%s", subdir,
+ subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
+ err = got_error_from_errno();
+ break;
+ }
+ err = gather_refs(refs, path_refs, child, repo);
+ free(child);
+ break;
+ default:
+ break;
+ }
+ }
+done:
+ if (d)
+ closedir(d);
+ free(path_subdir);
+ return err;
+}
+
const struct got_error *
got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
{
const struct got_error *err;
- char *packed_refs_path, *path_refs;
+ char *packed_refs_path, *path_refs = NULL;
FILE *f;
struct got_reference *ref;
@@ -489,14 +565,21 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
goto done;
}
err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
- free(path_refs);
if (err)
goto done;
err = append_ref(refs, ref, repo);
if (err)
goto done;
+ free(path_refs);
+ path_refs = get_refs_dir_path(repo, "");
+ if (path_refs == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ err = gather_refs(refs, path_refs, "", repo);
done:
+ free(path_refs);
if (f)
fclose(f);
return err;