rev-list: add --indexed-objects option
There is currently no easy way to ask the revision traversal machinery to include objects reachable from the index (e.g., blobs and trees that have not yet been committed). This patch adds an option to do so. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Oct 16, 2014 at 20:44 UTC
4fe10219bca6b010e2103116c653928992d5141b
4 files changed
+80
Documentation/rev-list-options.txt
+5
@@ -172,6 +172,11 @@ explicitly.
172
Pretend as if all objects mentioned by reflogs are listed on the
173
command line as `<commit>`.
174
175
+--indexed-objects::
176
+ Pretend as if all trees and blobs used by the index are listed
177
+ on the command line. Note that you probably want to use
178
+ `--objects`, too.
179
+
180
--ignore-missing::
181
Upon seeing an invalid object name in the input, pretend as if
182
the bad input was not given.
revision.c
+51
@@ -17,6 +17,7 @@
17
#include "mailmap.h"
18
#include "commit-slab.h"
19
#include "dir.h"
20
+#include "cache-tree.h"
21
22
volatile show_early_output_fn_t show_early_output;
23
@@ -1303,6 +1304,53 @@ void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1304
for_each_reflog(handle_one_reflog, &cb);
1305
}
1306
1307
+static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1308
+ struct strbuf *path)
1309
+{
1310
+ size_t baselen = path->len;
1311
+ int i;
1312
+
1313
+ if (it->entry_count >= 0) {
1314
+ struct tree *tree = lookup_tree(it->sha1);
1315
+ add_pending_object_with_path(revs, &tree->object, "",
1316
+ 040000, path->buf);
1317
+ }
1318
+
1319
+ for (i = 0; i < it->subtree_nr; i++) {
1320
+ struct cache_tree_sub *sub = it->down[i];
1321
+ strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1322
+ add_cache_tree(sub->cache_tree, revs, path);
1323
+ strbuf_setlen(path, baselen);
1324
+ }
1325
+
1326
+}
1327
+
1328
+void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1329
+{
1330
+ int i;
1331
+
1332
+ read_cache();
1333
+ for (i = 0; i < active_nr; i++) {
1334
+ struct cache_entry *ce = active_cache[i];
1335
+ struct blob *blob;
1336
+
1337
+ if (S_ISGITLINK(ce->ce_mode))
1338
+ continue;
1339
+
1340
+ blob = lookup_blob(ce->sha1);
1341
+ if (!blob)
1342
+ die("unable to add index blob to traversal");
1343
+ add_pending_object_with_path(revs, &blob->object, "",
1344
+ ce->ce_mode, ce->name);
1345
+ }
1346
+
1347
+ if (active_cache_tree) {
1348
+ struct strbuf path = STRBUF_INIT;
1349
+ add_cache_tree(active_cache_tree, revs, &path);
1350
+ strbuf_release(&path);
1351
+ }
1352
+}
1353
+
1354
static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1355
{
1356
unsigned char sha1[20];
@@ -1653,6 +1701,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1701
!strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1702
!strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1703
!strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1704
+ !strcmp(arg, "--indexed-objects") ||
1705
starts_with(arg, "--exclude=") ||
1706
starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1707
starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
@@ -2082,6 +2131,8 @@ static int handle_revision_pseudo_opt(const char *submodule,
2131
clear_ref_exclusion(&revs->ref_excludes);
2132
} else if (!strcmp(arg, "--reflog")) {
2133
add_reflogs_to_pending(revs, *flags);
2134
+ } else if (!strcmp(arg, "--indexed-objects")) {
2135
+ add_index_objects_to_pending(revs, *flags);
2136
} else if (!strcmp(arg, "--not")) {
2137
*flags ^= UNINTERESTING | BOTTOM;
2138
} else if (!strcmp(arg, "--no-walk")) {
revision.h
+1
@@ -277,6 +277,7 @@ extern void add_pending_sha1(struct rev_info *revs,
277
278
extern void add_head_to_pending(struct rev_info *);
279
extern void add_reflogs_to_pending(struct rev_info *, unsigned int flags);
280
+extern void add_index_objects_to_pending(struct rev_info *, unsigned int flags);
281
282
enum commit_action {
283
commit_ignore,
t/t6000-rev-list-misc.sh
+23
@@ -73,4 +73,27 @@ test_expect_success 'symleft flag bit is propagated down from tag' '
73
test_cmp expect actual
74
'
75
76
+test_expect_success 'rev-list can show index objects' '
77
+ # Of the blobs and trees in the index, note:
78
+ #
79
+ # - we do not show two/three, because it is the
80
+ # same blob as "one", and we show objects only once
81
+ #
82
+ # - we do show the tree "two", because it has a valid cache tree
83
+ # from the last commit
84
+ #
85
+ # - we do not show the root tree; since we updated the index, it
86
+ # does not have a valid cache tree
87
+ #
88
+ cat >expect <<-\EOF
89
+ 8e4020bb5a8d8c873b25de15933e75cc0fc275df one
90
+ d9d3a7417b9605cfd88ee6306b28dadc29e6ab08 only-in-index
91
+ 9200b628cf9dc883a85a7abc8d6e6730baee589c two
92
+ EOF
93
+ echo only-in-index >only-in-index &&
94
+ git add only-in-index &&
95
+ git rev-list --objects --indexed-objects >actual &&
96
+ test_cmp expect actual
97
+'
98
+
99
test_done