revision: mark non-user-given objects instead

Currently, list-objects.c incorrectly treats all root trees of commits as USER_GIVEN. Also, it would be easier to mark objects that are non-user-given instead of user-given, since the places in the code where we access an object through a reference are more obvious than the places where we access an object that was given by the user. Resolve these two problems by introducing a flag NOT_USER_GIVEN that marks blobs and trees that are non-user-given, replacing USER_GIVEN. (Only blobs and trees are marked because this mark is only used when filtering objects, and filtering of other types of objects is not supported yet.) This fixes a bug in that git rev-list behaved differently from git pack-objects. pack-objects would *not* filter objects given explicitly on the command line and rev-list would filter. This was because the two commands used a different function to add objects to the rev_info struct. This seems to have been an oversight, and pack-objects has the correct behavior, so I added a test to make sure that rev-list now behaves properly. Signed-off-by: Matthew DeVore <matvore@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthew DeVore committed Oct 5, 2018 at 14:31 UTC 99c9aa9579ae970c0d273ced8fb8efe9eed70a75
4 files changed +39 -16
list-objects.c
+18 -13
@@ -53,7 +53,7 @@ static void process_blob(struct traversal_context *ctx,
53
54 pathlen = path->len;
55 strbuf_addstr(path, name);
56 - if (!(obj->flags & USER_GIVEN) && ctx->filter_fn)
56 + if ((obj->flags & NOT_USER_GIVEN) && ctx->filter_fn)
57 r = ctx->filter_fn(LOFS_BLOB, obj,
58 path->buf, &path->buf[pathlen],
59 ctx->filter_data);
@@ -120,17 +120,19 @@ static void process_tree_contents(struct traversal_context *ctx,
120 continue;
121 }
122
123 - if (S_ISDIR(entry.mode))
124 - process_tree(ctx,
125 - lookup_tree(the_repository, entry.oid),
126 - base, entry.path);
123 + if (S_ISDIR(entry.mode)) {
124 + struct tree *t = lookup_tree(the_repository, entry.oid);
125 + t->object.flags |= NOT_USER_GIVEN;
126 + process_tree(ctx, t, base, entry.path);
127 + }
128 else if (S_ISGITLINK(entry.mode))
129 process_gitlink(ctx, entry.oid->hash,
130 base, entry.path);
130 - else
131 - process_blob(ctx,
132 - lookup_blob(the_repository, entry.oid),
133 - base, entry.path);
131 + else {
132 + struct blob *b = lookup_blob(the_repository, entry.oid);
133 + b->object.flags |= NOT_USER_GIVEN;
134 + process_blob(ctx, b, base, entry.path);
135 + }
136 }
137 }
138
@@ -171,7 +173,7 @@ static void process_tree(struct traversal_context *ctx,
173 }
174
175 strbuf_addstr(base, name);
174 - if (!(obj->flags & USER_GIVEN) && ctx->filter_fn)
176 + if ((obj->flags & NOT_USER_GIVEN) && ctx->filter_fn)
177 r = ctx->filter_fn(LOFS_BEGIN_TREE, obj,
178 base->buf, &base->buf[baselen],
179 ctx->filter_data);
@@ -185,7 +187,7 @@ static void process_tree(struct traversal_context *ctx,
187 if (!failed_parse)
188 process_tree_contents(ctx, tree, base);
189
188 - if (!(obj->flags & USER_GIVEN) && ctx->filter_fn) {
190 + if ((obj->flags & NOT_USER_GIVEN) && ctx->filter_fn) {
191 r = ctx->filter_fn(LOFS_END_TREE, obj,
192 base->buf, &base->buf[baselen],
193 ctx->filter_data);
@@ -301,8 +303,11 @@ static void do_traverse(struct traversal_context *ctx)
303 * an uninteresting boundary commit may not have its tree
304 * parsed yet, but we are not going to show them anyway
305 */
304 - if (get_commit_tree(commit))
305 - add_pending_tree(ctx->revs, get_commit_tree(commit));
306 + if (get_commit_tree(commit)) {
307 + struct tree *tree = get_commit_tree(commit);
308 + tree->object.flags |= NOT_USER_GIVEN;
309 + add_pending_tree(ctx->revs, tree);
310 + }
311 ctx->show_commit(commit, ctx->show_data);
312
313 if (ctx->revs->tree_blobs_in_commit_order)
revision.c
-1
@@ -175,7 +175,6 @@ static void add_pending_object_with_path(struct rev_info *revs,
175 strbuf_release(&buf);
176 return; /* do not add the commit itself */
177 }
178 - obj->flags |= USER_GIVEN;
178 add_object_array_with_path(obj, name, &revs->pending, mode, path);
179 }
180
revision.h
+9 -2
@@ -20,9 +20,16 @@
20 #define SYMMETRIC_LEFT (1u<<8)
21 #define PATCHSAME (1u<<9)
22 #define BOTTOM (1u<<10)
23 -#define USER_GIVEN (1u<<25) /* given directly by the user */
23 +/*
24 + * Indicates object was reached by traversal. i.e. not given by user on
25 + * command-line or stdin.
26 + * NEEDSWORK: NOT_USER_GIVEN doesn't apply to commits because we only support
27 + * filtering trees and blobs, but it may be useful to support filtering commits
28 + * in the future.
29 + */
30 +#define NOT_USER_GIVEN (1u<<25)
31 #define TRACK_LINEAR (1u<<26)
25 -#define ALL_REV_FLAGS (((1u<<11)-1) | USER_GIVEN | TRACK_LINEAR)
32 +#define ALL_REV_FLAGS (((1u<<11)-1) | NOT_USER_GIVEN | TRACK_LINEAR)
33
34 #define DECORATE_SHORT_REFS 1
35 #define DECORATE_FULL_REFS 2
t/t6112-rev-list-filters-objects.sh
+12
@@ -30,6 +30,18 @@ test_expect_success 'verify blob:none omits all 5 blobs' '
30 test_cmp observed expected
31 '
32
33 +test_expect_success 'specify blob explicitly prevents filtering' '
34 + file_3=$(git -C r1 ls-files -s file.3 |
35 + awk -f print_2.awk) &&
36 +
37 + file_4=$(git -C r1 ls-files -s file.4 |
38 + awk -f print_2.awk) &&
39 +
40 + git -C r1 rev-list --objects --filter=blob:none HEAD $file_3 >observed &&
41 + grep -q "$file_3" observed &&
42 + test_must_fail grep -q "$file_4" observed
43 +'
44 +
45 test_expect_success 'verify emitted+omitted == all' '
46 git -C r1 rev-list HEAD --objects \
47 | awk -f print_1.awk \