Skip to content

Commit c64ed70

Browse files
author
Junio C Hamano
committed
Separate object listing routines out of rev-list
Create a separate file, list-objects.c, and move object listing routines from rev-list to it. The next round will use it in pack-objects directly. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 7bbf88c commit c64ed70

File tree

4 files changed

+130
-99
lines changed

4 files changed

+130
-99
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ XDIFF_LIB=xdiff/lib.a
233233

234234
LIB_H = \
235235
blob.h cache.h commit.h csum-file.h delta.h \
236-
diff.h object.h pack.h pkt-line.h quote.h refs.h \
236+
diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h \
237237
run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
238238
tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h
239239

@@ -250,7 +250,7 @@ LIB_OBJS = \
250250
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
251251
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
252252
fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
253-
write_or_die.o trace.o \
253+
write_or_die.o trace.o list-objects.o \
254254
alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS)
255255

256256
BUILTIN_OBJS = \

builtin-rev-list.c

Lines changed: 13 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "tree-walk.h"
88
#include "diff.h"
99
#include "revision.h"
10+
#include "list-objects.h"
1011
#include "builtin.h"
1112

1213
/* bits #0-15 in revision.h */
@@ -98,104 +99,19 @@ static void show_commit(struct commit *commit)
9899
commit->buffer = NULL;
99100
}
100101

101-
static void process_blob(struct blob *blob,
102-
struct object_array *p,
103-
struct name_path *path,
104-
const char *name)
102+
static void show_object(struct object_array_entry *p)
105103
{
106-
struct object *obj = &blob->object;
107-
108-
if (!revs.blob_objects)
109-
return;
110-
if (obj->flags & (UNINTERESTING | SEEN))
111-
return;
112-
obj->flags |= SEEN;
113-
name = xstrdup(name);
114-
add_object(obj, p, path, name);
115-
}
116-
117-
static void process_tree(struct tree *tree,
118-
struct object_array *p,
119-
struct name_path *path,
120-
const char *name)
121-
{
122-
struct object *obj = &tree->object;
123-
struct tree_desc desc;
124-
struct name_entry entry;
125-
struct name_path me;
126-
127-
if (!revs.tree_objects)
128-
return;
129-
if (obj->flags & (UNINTERESTING | SEEN))
130-
return;
131-
if (parse_tree(tree) < 0)
132-
die("bad tree object %s", sha1_to_hex(obj->sha1));
133-
obj->flags |= SEEN;
134-
name = xstrdup(name);
135-
add_object(obj, p, path, name);
136-
me.up = path;
137-
me.elem = name;
138-
me.elem_len = strlen(name);
139-
140-
desc.buf = tree->buffer;
141-
desc.size = tree->size;
142-
143-
while (tree_entry(&desc, &entry)) {
144-
if (S_ISDIR(entry.mode))
145-
process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
146-
else
147-
process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
148-
}
149-
free(tree->buffer);
150-
tree->buffer = NULL;
151-
}
152-
153-
static void show_commit_list(struct rev_info *revs)
154-
{
155-
int i;
156-
struct commit *commit;
157-
struct object_array objects = { 0, 0, NULL };
158-
159-
while ((commit = get_revision(revs)) != NULL) {
160-
process_tree(commit->tree, &objects, NULL, "");
161-
show_commit(commit);
162-
}
163-
for (i = 0; i < revs->pending.nr; i++) {
164-
struct object_array_entry *pending = revs->pending.objects + i;
165-
struct object *obj = pending->item;
166-
const char *name = pending->name;
167-
if (obj->flags & (UNINTERESTING | SEEN))
168-
continue;
169-
if (obj->type == OBJ_TAG) {
170-
obj->flags |= SEEN;
171-
add_object_array(obj, name, &objects);
172-
continue;
173-
}
174-
if (obj->type == OBJ_TREE) {
175-
process_tree((struct tree *)obj, &objects, NULL, name);
176-
continue;
177-
}
178-
if (obj->type == OBJ_BLOB) {
179-
process_blob((struct blob *)obj, &objects, NULL, name);
180-
continue;
181-
}
182-
die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
183-
}
184-
for (i = 0; i < objects.nr; i++) {
185-
struct object_array_entry *p = objects.objects + i;
186-
187-
/* An object with name "foo\n0000000..." can be used to
188-
* confuse downstream git-pack-objects very badly.
189-
*/
190-
const char *ep = strchr(p->name, '\n');
191-
if (ep) {
192-
printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
193-
(int) (ep - p->name),
194-
p->name);
195-
}
196-
else
197-
printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
104+
/* An object with name "foo\n0000000..." can be used to
105+
* confuse downstream git-pack-objects very badly.
106+
*/
107+
const char *ep = strchr(p->name, '\n');
108+
if (ep) {
109+
printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
110+
(int) (ep - p->name),
111+
p->name);
198112
}
113+
else
114+
printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
199115
}
200116

201117
/*
@@ -389,7 +305,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
389305
if (bisect_list)
390306
revs.commits = find_bisection(revs.commits);
391307

392-
show_commit_list(&revs);
308+
traverse_commit_list(&revs, show_commit, show_object);
393309

394310
return 0;
395311
}

list-objects.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include "cache.h"
2+
#include "tag.h"
3+
#include "commit.h"
4+
#include "tree.h"
5+
#include "blob.h"
6+
#include "diff.h"
7+
#include "tree-walk.h"
8+
#include "revision.h"
9+
#include "list-objects.h"
10+
11+
static void process_blob(struct rev_info *revs,
12+
struct blob *blob,
13+
struct object_array *p,
14+
struct name_path *path,
15+
const char *name)
16+
{
17+
struct object *obj = &blob->object;
18+
19+
if (!revs->blob_objects)
20+
return;
21+
if (obj->flags & (UNINTERESTING | SEEN))
22+
return;
23+
obj->flags |= SEEN;
24+
name = xstrdup(name);
25+
add_object(obj, p, path, name);
26+
}
27+
28+
static void process_tree(struct rev_info *revs,
29+
struct tree *tree,
30+
struct object_array *p,
31+
struct name_path *path,
32+
const char *name)
33+
{
34+
struct object *obj = &tree->object;
35+
struct tree_desc desc;
36+
struct name_entry entry;
37+
struct name_path me;
38+
39+
if (!revs->tree_objects)
40+
return;
41+
if (obj->flags & (UNINTERESTING | SEEN))
42+
return;
43+
if (parse_tree(tree) < 0)
44+
die("bad tree object %s", sha1_to_hex(obj->sha1));
45+
obj->flags |= SEEN;
46+
name = xstrdup(name);
47+
add_object(obj, p, path, name);
48+
me.up = path;
49+
me.elem = name;
50+
me.elem_len = strlen(name);
51+
52+
desc.buf = tree->buffer;
53+
desc.size = tree->size;
54+
55+
while (tree_entry(&desc, &entry)) {
56+
if (S_ISDIR(entry.mode))
57+
process_tree(revs,
58+
lookup_tree(entry.sha1),
59+
p, &me, entry.path);
60+
else
61+
process_blob(revs,
62+
lookup_blob(entry.sha1),
63+
p, &me, entry.path);
64+
}
65+
free(tree->buffer);
66+
tree->buffer = NULL;
67+
}
68+
69+
void traverse_commit_list(struct rev_info *revs,
70+
void (*show_commit)(struct commit *),
71+
void (*show_object)(struct object_array_entry *))
72+
{
73+
int i;
74+
struct commit *commit;
75+
struct object_array objects = { 0, 0, NULL };
76+
77+
while ((commit = get_revision(revs)) != NULL) {
78+
process_tree(revs, commit->tree, &objects, NULL, "");
79+
show_commit(commit);
80+
}
81+
for (i = 0; i < revs->pending.nr; i++) {
82+
struct object_array_entry *pending = revs->pending.objects + i;
83+
struct object *obj = pending->item;
84+
const char *name = pending->name;
85+
if (obj->flags & (UNINTERESTING | SEEN))
86+
continue;
87+
if (obj->type == OBJ_TAG) {
88+
obj->flags |= SEEN;
89+
add_object_array(obj, name, &objects);
90+
continue;
91+
}
92+
if (obj->type == OBJ_TREE) {
93+
process_tree(revs, (struct tree *)obj, &objects,
94+
NULL, name);
95+
continue;
96+
}
97+
if (obj->type == OBJ_BLOB) {
98+
process_blob(revs, (struct blob *)obj, &objects,
99+
NULL, name);
100+
continue;
101+
}
102+
die("unknown pending object %s (%s)",
103+
sha1_to_hex(obj->sha1), name);
104+
}
105+
for (i = 0; i < objects.nr; i++)
106+
show_object(&objects.objects[i]);
107+
}

list-objects.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef LIST_OBJECTS_H
2+
#define LIST_OBJECTS_H
3+
4+
void traverse_commit_list(struct rev_info *revs,
5+
void (*show_commit)(struct commit *),
6+
void (*show_object)(struct object_array_entry *));
7+
8+
#endif

0 commit comments

Comments
 (0)