commit a72597083f64cda4978a3bce3eb4dba6ae2b6d32 from: Stefan Sperling via: Thomas Adam date: Sat Mar 30 17:17:56 2024 UTC make output of 'got ref -l' more consistent Ensure that got ref -l provides consistent output regardless of whether references are packed or not. Problem reported by naddy@ Also make 'got ref -l name' work consistently when the provided argument is the name of a reference, rather than a ref-prefix. ok naddy commit - c1c948027e25f932dc9cc311722d3a4b44fcaf64 commit + a72597083f64cda4978a3bce3eb4dba6ae2b6d32 blob - 0eccf9a0d4a076a217aaada72bc6bf41f3a186f2 blob + 2a5a6702d514b6a921362edba623fc8a3d056f46 --- lib/reference.c +++ lib/reference.c @@ -856,6 +856,8 @@ gather_on_disk_refs(struct got_reflist_head *refs, con const struct got_error *err = NULL; DIR *d = NULL; char *path_subdir; + struct got_reference *ref; + struct got_reflist_entry *new; while (subdir[0] == '/') subdir++; @@ -864,12 +866,40 @@ gather_on_disk_refs(struct got_reflist_head *refs, con return got_error_from_errno("asprintf"); d = opendir(path_subdir); - if (d == NULL) - goto done; + if (d == NULL) { + char *refname; + if (errno != ENOTDIR) + goto done; + + /* This could be a regular on-disk reference file. */ + free(path_subdir); + err = got_path_dirname(&path_subdir, subdir); + if (err) + return err; + err = got_path_basename(&refname, subdir); + if (err) { + free(path_subdir); + return err; + } + err = open_ref(&ref, path_refs, path_subdir, refname, + 0, got_repo_get_object_format(repo)); + free(path_subdir); + free(refname); + if (err) { + if (err->code == GOT_ERR_NOT_REF) + return NULL; + return err; + } + err = got_reflist_insert(&new, refs, ref, + cmp_cb, cmp_arg); + if (err || new == NULL /* duplicate */) + got_ref_close(ref); + return err; + } + for (;;) { struct dirent *dent; - struct got_reference *ref; char *child; int type; @@ -894,7 +924,6 @@ gather_on_disk_refs(struct got_reflist_head *refs, con if (err) goto done; if (ref) { - struct got_reflist_entry *new; err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg); if (err || new == NULL /* duplicate */) @@ -922,6 +951,33 @@ done: closedir(d); free(path_subdir); return err; +} + +static int +match_packed_ref(struct got_reference *ref, const char *ref_namespace) +{ + const char *name = got_ref_get_name(ref); + int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0); + + if (namespace_is_absolute) { + return (strcmp(name, ref_namespace) == 0 || + got_path_is_child(name, ref_namespace, + strlen(ref_namespace))); + } + + /* Match all "subdirectories" as we do with on-disk refs. */ + while (*name != '\0') { + while (*name == '/') + name++; + if (strcmp(name, ref_namespace) == 0 || + got_path_is_child(name, ref_namespace, + strlen(ref_namespace))) + return 1; + while (*name != '\0' && *name != '/') + name++; + } + + return 0; } const struct got_error * @@ -954,14 +1010,7 @@ got_ref_list(struct got_reflist_head *refs, struct got goto done; } else { /* Try listing a single reference. */ - const char *refname = ref_namespace; - path_refs = get_refs_dir_path(repo, refname); - if (path_refs == NULL) { - err = got_error_from_errno("get_refs_dir_path"); - goto done; - } - err = open_ref(&ref, path_refs, "", refname, 0, - got_repo_get_object_format(repo)); + err = got_ref_open(&ref, repo, ref_namespace, 0); if (err) { if (err->code != GOT_ERR_NOT_REF) goto done; @@ -1048,15 +1097,10 @@ got_ref_list(struct got_reflist_head *refs, struct got if (err) goto done; if (ref) { - if (ref_namespace) { - const char *name; - name = got_ref_get_name(ref); - if (!got_path_is_child(name, - ref_namespace, - strlen(ref_namespace))) { - got_ref_close(ref); - continue; - } + if (ref_namespace && + !match_packed_ref(ref, ref_namespace)) { + got_ref_close(ref); + continue; } err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg); blob - f19cabc30854c8e77db6d827d2b8ed078c8362f5 blob + 12ee9f0325d237ae1d8a5a962b5aa23fb7261605 --- regress/cmdline/ref.sh +++ regress/cmdline/ref.sh @@ -436,6 +436,91 @@ test_ref_list() { done test_done "$testroot" "$ret" +} + +test_ref_list_packed_refs() { + local testroot=`test_init ref_list_packed_refs` + local commit_id=`git_show_head $testroot/repo` + local tag=1.0.0 + local tag2=2.0.0 + + # create tag with Git + git -C $testroot/repo tag -a -m 'test' $tag + # create tag with Got + (cd $testroot/repo && got tag -m 'test' $tag2 > /dev/null) + + tag_id=`got ref -r $testroot/repo -l \ + | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2` + local tagger_time=`git_show_tagger_time $testroot/repo $tag` + d1=`date -u -r $tagger_time +"%a %b %e %X %Y UTC"` + tag_id2=`got ref -r $testroot/repo -l \ + | grep "^refs/tags/$tag2" | tr -d ' ' | cut -d: -f2` + local tagger_time2=`git_show_tagger_time $testroot/repo $tag2` + d2=`date -u -r $tagger_time2 +"%a %b %e %X %Y UTC"` + + for i in 1 2; do + if [ $i -eq 2 ]; then + # Move all refs into the packed-refs file + git -C $testroot/repo pack-refs --all + fi + + got ref -r $testroot/repo -l > $testroot/stdout + + cat > $testroot/stdout.expected < $testroot/stdout + + cat > $testroot/stdout.expected < $testroot/stdout + + cat > $testroot/stdout.expected < $testroot/stdout + cat > $testroot/stdout.expected <