@@ -39,7 +39,7 @@ It is also important that you do not specify the `--objects` flag for the
the objects will be walked in a separate way based on those starting
commits.
-`commits`, `blobs`, `trees`::
+`commits`, `blobs`, `trees`, `tags`::
By default, these members are enabled and signal that the path-walk
API should call the `path_fn` on objects of these types. Specialized
applications could disable some options to make it simpler to walk
@@ -13,10 +13,13 @@
#include "revision.h"
#include "string-list.h"
#include "strmap.h"
+#include "tag.h"
#include "trace2.h"
#include "tree.h"
#include "tree-walk.h"
+static const char *root_path = "";
+
struct type_and_oid_list
{
enum object_type type;
@@ -158,9 +161,13 @@ static int walk_path(struct path_walk_context *ctx,
list = strmap_get(&ctx->paths_to_lists, path);
+ if (!list)
+ BUG("provided path '%s' that had no associated list", path);
+
/* Evaluate function pointer on this data, if requested. */
if ((list->type == OBJ_TREE && ctx->info->trees) ||
- (list->type == OBJ_BLOB && ctx->info->blobs))
+ (list->type == OBJ_BLOB && ctx->info->blobs) ||
+ (list->type == OBJ_TAG && ctx->info->tags))
ret = ctx->info->path_fn(path, &list->oids, list->type,
ctx->info->path_fn_data);
@@ -191,6 +198,134 @@ static void clear_strmap(struct strmap *map)
strmap_init(map);
}
+static void setup_pending_objects(struct path_walk_info *info,
+ struct path_walk_context *ctx)
+{
+ struct type_and_oid_list *tags = NULL;
+ struct type_and_oid_list *tagged_blobs = NULL;
+ struct type_and_oid_list *root_tree_list = NULL;
+
+ if (info->tags)
+ CALLOC_ARRAY(tags, 1);
+ if (info->blobs)
+ CALLOC_ARRAY(tagged_blobs, 1);
+ if (info->trees)
+ root_tree_list = strmap_get(&ctx->paths_to_lists, root_path);
+
+ /*
+ * Pending objects include:
+ * * Commits at branch tips.
+ * * Annotated tags at tag tips.
+ * * Any kind of object at lightweight tag tips.
+ * * Trees and blobs in the index (with an associated path).
+ */
+ for (size_t i = 0; i < info->revs->pending.nr; i++) {
+ struct object_array_entry *pending = info->revs->pending.objects + i;
+ struct object *obj = pending->item;
+
+ /* Commits will be picked up by revision walk. */
+ if (obj->type == OBJ_COMMIT)
+ continue;
+
+ /* Navigate annotated tag object chains. */
+ while (obj->type == OBJ_TAG) {
+ struct tag *tag = lookup_tag(info->revs->repo, &obj->oid);
+ if (!tag)
+ break;
+ if (tag->object.flags & SEEN)
+ break;
+ tag->object.flags |= SEEN;
+
+ if (tags)
+ oid_array_append(&tags->oids, &obj->oid);
+ obj = tag->tagged;
+ }
+
+ if (obj->type == OBJ_TAG)
+ continue;
+
+ /* We are now at a non-tag object. */
+ if (obj->flags & SEEN)
+ continue;
+ obj->flags |= SEEN;
+
+ switch (obj->type) {
+ case OBJ_TREE:
+ if (!info->trees)
+ continue;
+ if (pending->path) {
+ struct type_and_oid_list *list;
+ char *path = *pending->path ? xstrfmt("%s/", pending->path)
+ : xstrdup("");
+ if (!(list = strmap_get(&ctx->paths_to_lists, path))) {
+ CALLOC_ARRAY(list, 1);
+ list->type = OBJ_TREE;
+ strmap_put(&ctx->paths_to_lists, path, list);
+ }
+ oid_array_append(&list->oids, &obj->oid);
+ free(path);
+ } else {
+ /* assume a root tree, such as a lightweight tag. */
+ oid_array_append(&root_tree_list->oids, &obj->oid);
+ }
+ break;
+
+ case OBJ_BLOB:
+ if (!info->blobs)
+ continue;
+ if (pending->path) {
+ struct type_and_oid_list *list;
+ char *path = pending->path;
+ if (!(list = strmap_get(&ctx->paths_to_lists, path))) {
+ CALLOC_ARRAY(list, 1);
+ list->type = OBJ_BLOB;
+ strmap_put(&ctx->paths_to_lists, path, list);
+ }
+ oid_array_append(&list->oids, &obj->oid);
+ } else {
+ /* assume a root tree, such as a lightweight tag. */
+ oid_array_append(&tagged_blobs->oids, &obj->oid);
+ }
+ break;
+
+ case OBJ_COMMIT:
+ /* Make sure it is in the object walk */
+ if (obj != pending->item)
+ add_pending_object(info->revs, obj, "");
+ break;
+
+ default:
+ BUG("should not see any other type here");
+ }
+ }
+
+ /*
+ * Add tag objects and tagged blobs if they exist.
+ */
+ if (tagged_blobs) {
+ if (tagged_blobs->oids.nr) {
+ const char *tagged_blob_path = "/tagged-blobs";
+ tagged_blobs->type = OBJ_BLOB;
+ push_to_stack(ctx, tagged_blob_path);
+ strmap_put(&ctx->paths_to_lists, tagged_blob_path, tagged_blobs);
+ } else {
+ oid_array_clear(&tagged_blobs->oids);
+ free(tagged_blobs);
+ }
+ }
+ if (tags) {
+ if (tags->oids.nr) {
+ const char *tag_path = "/tags";
+ tags->type = OBJ_TAG;
+ push_to_stack(ctx, tag_path);
+ strmap_put(&ctx->paths_to_lists, tag_path, tags);
+ } else {
+ oid_array_clear(&tags->oids);
+ free(tags);
+ }
+ }
+}
+
/**
* Given the configuration of 'info', walk the commits based on 'info->revs' and
* call 'info->path_fn' on each discovered path.
@@ -199,7 +334,6 @@ static void clear_strmap(struct strmap *map)
*/
int walk_objects_by_path(struct path_walk_info *info)
{
- const char *root_path = "";
int ret = 0;
size_t commits_nr = 0, paths_nr = 0;
struct commit *c;
@@ -219,15 +353,31 @@ int walk_objects_by_path(struct path_walk_info *info)
CALLOC_ARRAY(commit_list, 1);
commit_list->type = OBJ_COMMIT;
+ if (info->tags)
+ info->revs->tag_objects = 1;
+
/* Insert a single list for the root tree into the paths. */
CALLOC_ARRAY(root_tree_list, 1);
root_tree_list->type = OBJ_TREE;
strmap_put(&ctx.paths_to_lists, root_path, root_tree_list);
push_to_stack(&ctx, root_path);
+ /*
+ * Set these values before preparing the walk to catch
+ * lightweight tags pointing to non-commits and indexed objects.
+ */
+ info->revs->blob_objects = info->blobs;
+ info->revs->tree_objects = info->trees;
+
if (prepare_revision_walk(info->revs))
die(_("failed to setup revision walk"));
+ info->revs->blob_objects = info->revs->tree_objects = 0;
+
+ trace2_region_enter("path-walk", "pending-walk", info->revs->repo);
+ setup_pending_objects(info, &ctx);
+ trace2_region_leave("path-walk", "pending-walk", info->revs->repo);
+
while ((c = get_revision(info->revs))) {
struct object_id *oid;
struct tree *t;
@@ -275,6 +425,27 @@ int walk_objects_by_path(struct path_walk_info *info)
free(path);
}
+
+ /* Are there paths remaining? Likely they are from indexed objects. */
+ if (!strmap_empty(&ctx.paths_to_lists)) {
+ struct hashmap_iter iter;
+ struct strmap_entry *entry;
+
+ strmap_for_each_entry(&ctx.paths_to_lists, &iter, entry) {
+ push_to_stack(&ctx, entry->key);
+ }
+
+ while (!ret && ctx.path_stack.nr) {
+ char *path = ctx.path_stack.items[ctx.path_stack.nr - 1].string;
+ ctx.path_stack.nr--;
+ paths_nr++;
+
+ ret = walk_path(&ctx, path);
+
+ free(path);
+ }
+ }
+
trace2_data_intmax("path-walk", ctx.repo, "paths", paths_nr);
trace2_region_leave("path-walk", "path-walk", info->revs->repo);
@@ -38,12 +38,14 @@ struct path_walk_info {
int commits;
int trees;
int blobs;
+ int tags;
};
#define PATH_WALK_INFO_INIT { \
.blobs = 1, \
.trees = 1, \
.commits = 1, \
+ .tags = 1, \
}
/**
@@ -21,6 +21,7 @@ struct path_walk_test_data {
uintmax_t commit_nr;
uintmax_t tree_nr;
uintmax_t blob_nr;
+ uintmax_t tag_nr;
};
static int emit_block(const char *path, struct oid_array *oids,
@@ -45,6 +46,11 @@ static int emit_block(const char *path, struct oid_array *oids,
tdata->blob_nr += oids->nr;
break;
+ case OBJ_TAG:
+ typestr = "TAG";
+ tdata->tag_nr += oids->nr;
+ break;
+
default:
BUG("we do not understand this type");
}
@@ -66,6 +72,8 @@ int cmd__path_walk(int argc, const char **argv)
N_("toggle inclusion of blob objects")),
OPT_BOOL(0, "commits", &info.commits,
N_("toggle inclusion of commit objects")),
+ OPT_BOOL(0, "tags", &info.tags,
+ N_("toggle inclusion of tag objects")),
OPT_BOOL(0, "trees", &info.trees,
N_("toggle inclusion of tree objects")),
OPT_END(),
@@ -92,8 +100,9 @@ int cmd__path_walk(int argc, const char **argv)
printf("commits:%" PRIuMAX "\n"
"trees:%" PRIuMAX "\n"
- "blobs:%" PRIuMAX "\n",
- data.commit_nr, data.tree_nr, data.blob_nr);
+ "blobs:%" PRIuMAX "\n"
+ "tags:%" PRIuMAX "\n",
+ data.commit_nr, data.tree_nr, data.blob_nr, data.tag_nr);
return res;
}
@@ -7,24 +7,55 @@ test_description='direct path-walk API tests'
test_expect_success 'setup test repository' '
git checkout -b base &&
+ # Make some objects that will only be reachable
+ # via non-commit tags.
+ mkdir child &&
+ echo file >child/file &&
+ git add child &&
+ git commit -m "will abandon" &&
+ git tag -a -m "tree" tree-tag HEAD^{tree} &&
+ echo file2 >file2 &&
+ git add file2 &&
+ git commit --amend -m "will abandon" &&
+ git tag tree-tag2 HEAD^{tree} &&
+
+ echo blob >file &&
+ blob_oid=$(git hash-object -t blob -w --stdin <file) &&
+ git tag -a -m "blob" blob-tag "$blob_oid" &&
+ echo blob2 >file2 &&
+ blob2_oid=$(git hash-object -t blob -w --stdin <file2) &&
+ git tag blob-tag2 "$blob2_oid" &&
+
+ rm -fr child file file2 &&
+
mkdir left &&
mkdir right &&
echo a >a &&
echo b >left/b &&
echo c >right/c &&
git add . &&
- git commit -m "first" &&
+ git commit --amend -m "first" &&
+ git tag -m "first" first HEAD &&
echo d >right/d &&
git add right &&
git commit -m "second" &&
+ git tag -a -m "second (under)" second.1 HEAD &&
+ git tag -a -m "second (top)" second.2 second.1 &&
+ # Set up file/dir collision in history.
+ rm a &&
+ mkdir a &&
+ echo a >a/a &&
echo bb >left/b &&
- git commit -a -m "third" &&
+ git add a left &&
+ git commit -m "third" &&
+ git tag -a -m "third" third &&
git checkout -b topic HEAD~1 &&
echo cc >right/c &&
- git commit -a -m "topic"
+ git commit -a -m "topic" &&
+ git tag -a -m "fourth" fourth
'
test_expect_success 'all' '
@@ -40,19 +71,104 @@ test_expect_success 'all' '
TREE::$(git rev-parse base^{tree})
TREE::$(git rev-parse base~1^{tree})
TREE::$(git rev-parse base~2^{tree})
+ TREE::$(git rev-parse refs/tags/tree-tag^{})
+ TREE::$(git rev-parse refs/tags/tree-tag2^{})
+ TREE:a/:$(git rev-parse base:a)
TREE:left/:$(git rev-parse base:left)
TREE:left/:$(git rev-parse base~2:left)
TREE:right/:$(git rev-parse topic:right)
TREE:right/:$(git rev-parse base~1:right)
TREE:right/:$(git rev-parse base~2:right)
- trees:9
+ TREE:child/:$(git rev-parse refs/tags/tree-tag^{}:child)
+ trees:13
BLOB:a:$(git rev-parse base~2:a)
+ BLOB:file2:$(git rev-parse refs/tags/tree-tag2^{}:file2)
BLOB:left/b:$(git rev-parse base~2:left/b)
BLOB:left/b:$(git rev-parse base:left/b)
BLOB:right/c:$(git rev-parse base~2:right/c)
BLOB:right/c:$(git rev-parse topic:right/c)
BLOB:right/d:$(git rev-parse base~1:right/d)
- blobs:6
+ BLOB:/tagged-blobs:$(git rev-parse refs/tags/blob-tag^{})
+ BLOB:/tagged-blobs:$(git rev-parse refs/tags/blob-tag2^{})
+ BLOB:child/file:$(git rev-parse refs/tags/tree-tag^{}:child/file)
+ blobs:10
+ TAG:/tags:$(git rev-parse refs/tags/first)
+ TAG:/tags:$(git rev-parse refs/tags/second.1)
+ TAG:/tags:$(git rev-parse refs/tags/second.2)
+ TAG:/tags:$(git rev-parse refs/tags/third)
+ TAG:/tags:$(git rev-parse refs/tags/fourth)
+ TAG:/tags:$(git rev-parse refs/tags/tree-tag)
+ TAG:/tags:$(git rev-parse refs/tags/blob-tag)
+ tags:7
+ EOF
+
+ test_cmp_sorted expect out
+'
+
+test_expect_success 'indexed objects' '
+ test_when_finished git reset --hard &&
+
+ # stage change into index, adding a blob but
+ # also invalidating the cache-tree for the root
+ # and the "left" directory.
+ echo bogus >left/c &&
+ git add left &&
+
+ test-tool path-walk -- --indexed-objects >out &&
+
+ cat >expect <<-EOF &&
+ commits:0
+ TREE:right/:$(git rev-parse topic:right)
+ trees:1
+ BLOB:a:$(git rev-parse HEAD:a)
+ BLOB:left/b:$(git rev-parse HEAD:left/b)
+ BLOB:left/c:$(git rev-parse :left/c)
+ BLOB:right/c:$(git rev-parse HEAD:right/c)
+ BLOB:right/d:$(git rev-parse HEAD:right/d)
+ blobs:5
+ tags:0
+ EOF
+
+ test_cmp_sorted expect out
+'
+
+test_expect_success 'branches and indexed objects mix well' '
+ test_when_finished git reset --hard &&
+
+ # stage change into index, adding a blob but
+ # also invalidating the cache-tree for the root
+ # and the "right" directory.
+ echo fake >right/d &&
+ git add right &&
+
+ test-tool path-walk -- --indexed-objects --branches >out &&
+
+ cat >expect <<-EOF &&
+ COMMIT::$(git rev-parse topic)
+ COMMIT::$(git rev-parse base)
+ COMMIT::$(git rev-parse base~1)
+ COMMIT::$(git rev-parse base~2)
+ commits:4
+ TREE::$(git rev-parse topic^{tree})
+ TREE::$(git rev-parse base^{tree})
+ TREE::$(git rev-parse base~1^{tree})
+ TREE::$(git rev-parse base~2^{tree})
+ TREE:a/:$(git rev-parse base:a)
+ TREE:left/:$(git rev-parse base:left)
+ TREE:left/:$(git rev-parse base~2:left)
+ TREE:right/:$(git rev-parse topic:right)
+ TREE:right/:$(git rev-parse base~1:right)
+ TREE:right/:$(git rev-parse base~2:right)
+ trees:10
+ BLOB:a:$(git rev-parse base~2:a)
+ BLOB:left/b:$(git rev-parse base:left/b)
+ BLOB:left/b:$(git rev-parse base~2:left/b)
+ BLOB:right/c:$(git rev-parse base~2:right/c)
+ BLOB:right/c:$(git rev-parse topic:right/c)
+ BLOB:right/d:$(git rev-parse base~1:right/d)
+ BLOB:right/d:$(git rev-parse :right/d)
+ blobs:7
+ tags:0
EOF
test_cmp_sorted expect out
@@ -80,6 +196,7 @@ test_expect_success 'topic only' '
BLOB:right/c:$(git rev-parse topic:right/c)
BLOB:right/d:$(git rev-parse base~1:right/d)
blobs:5
+ tags:0
EOF
test_cmp_sorted expect out
@@ -100,6 +217,7 @@ test_expect_success 'topic, not base' '
BLOB:right/c:$(git rev-parse topic:right/c)
BLOB:right/d:$(git rev-parse topic:right/d)
blobs:4
+ tags:0
EOF
test_cmp_sorted expect out
@@ -117,6 +235,7 @@ test_expect_success 'topic, not base, only blobs' '
BLOB:right/c:$(git rev-parse topic:right/c)
BLOB:right/d:$(git rev-parse topic:right/d)
blobs:4
+ tags:0
EOF
test_cmp_sorted expect out
@@ -133,6 +252,7 @@ test_expect_success 'topic, not base, only commits' '
commits:1
trees:0
blobs:0
+ tags:0
EOF
test_cmp_sorted expect out
@@ -149,6 +269,7 @@ test_expect_success 'topic, not base, only trees' '
TREE:right/:$(git rev-parse topic:right)
trees:3
blobs:0
+ tags:0
EOF
test_cmp_sorted expect out
@@ -173,9 +294,32 @@ test_expect_success 'topic, not base, boundary' '
BLOB:right/c:$(git rev-parse topic:right/c)
BLOB:right/d:$(git rev-parse base~1:right/d)
blobs:5
+ tags:0
EOF
test_cmp_sorted expect out
'
+test_expect_success 'trees are reported exactly once' '
+ test_when_finished "rm -rf unique-trees" &&
+ test_create_repo unique-trees &&
+ (
+ cd unique-trees &&
+ mkdir initial &&
+ test_commit initial/file &&
+
+ git switch -c move-to-top &&
+ git mv initial/file.t ./ &&
+ test_tick &&
+ git commit -m moved &&
+
+ git update-ref refs/heads/other HEAD
+ ) &&
+
+ test-tool -C unique-trees path-walk -- --all >out &&
+ tree=$(git -C unique-trees rev-parse HEAD:) &&
+ grep "$tree" out >out-filtered &&
+ test_line_count = 1 out-filtered
+'
+
test_done