Skip to content

Commit 4fe1021

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 41d018d commit 4fe1021

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

Documentation/rev-list-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ explicitly.
172172
Pretend as if all objects mentioned by reflogs are listed on the
173173
command line as `<commit>`.
174174

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+
175180
--ignore-missing::
176181
Upon seeing an invalid object name in the input, pretend as if
177182
the bad input was not given.

revision.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "mailmap.h"
1818
#include "commit-slab.h"
1919
#include "dir.h"
20+
#include "cache-tree.h"
2021

2122
volatile show_early_output_fn_t show_early_output;
2223

@@ -1303,6 +1304,53 @@ void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
13031304
for_each_reflog(handle_one_reflog, &cb);
13041305
}
13051306

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+
13061354
static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
13071355
{
13081356
unsigned char sha1[20];
@@ -1653,6 +1701,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
16531701
!strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
16541702
!strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
16551703
!strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1704+
!strcmp(arg, "--indexed-objects") ||
16561705
starts_with(arg, "--exclude=") ||
16571706
starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
16581707
starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
@@ -2082,6 +2131,8 @@ static int handle_revision_pseudo_opt(const char *submodule,
20822131
clear_ref_exclusion(&revs->ref_excludes);
20832132
} else if (!strcmp(arg, "--reflog")) {
20842133
add_reflogs_to_pending(revs, *flags);
2134+
} else if (!strcmp(arg, "--indexed-objects")) {
2135+
add_index_objects_to_pending(revs, *flags);
20852136
} else if (!strcmp(arg, "--not")) {
20862137
*flags ^= UNINTERESTING | BOTTOM;
20872138
} else if (!strcmp(arg, "--no-walk")) {

revision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ extern void add_pending_sha1(struct rev_info *revs,
277277

278278
extern void add_head_to_pending(struct rev_info *);
279279
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);
280281

281282
enum commit_action {
282283
commit_ignore,

t/t6000-rev-list-misc.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,27 @@ test_expect_success 'symleft flag bit is propagated down from tag' '
7373
test_cmp expect actual
7474
'
7575

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+
7699
test_done

0 commit comments

Comments
 (0)