Skip to content

Commit f817f2f

Browse files
committed
Merge branch 'jc/traverse-commit-list'
* jc/traverse-commit-list: revision.c: update show_object_with_name() without using malloc() revision.c: add show_object_with_name() helper function rev-list: fix finish_object() call
2 parents cd4093b + beba25a commit f817f2f

File tree

4 files changed

+46
-29
lines changed

4 files changed

+46
-29
lines changed

builtin/rev-list.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,8 @@ static void finish_object(struct object *obj, const struct name_path *path, cons
176176

177177
static void show_object(struct object *obj, const struct name_path *path, const char *component)
178178
{
179-
char *name = path_name(path, component);
180-
/* An object with name "foo\n0000000..." can be used to
181-
* confuse downstream "git pack-objects" very badly.
182-
*/
183-
const char *ep = strchr(name, '\n');
184-
185-
finish_object(obj, path, name);
186-
if (ep) {
187-
printf("%s %.*s\n", sha1_to_hex(obj->sha1),
188-
(int) (ep - name),
189-
name);
190-
}
191-
else
192-
printf("%s %s\n", sha1_to_hex(obj->sha1), name);
193-
free(name);
179+
finish_object(obj, path, component);
180+
show_object_with_name(stdout, obj, path, component);
194181
}
195182

196183
static void show_edge(struct commit *commit)

revision.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,47 @@ char *path_name(const struct name_path *path, const char *name)
4040
return n;
4141
}
4242

43+
static int show_path_component_truncated(FILE *out, const char *name, int len)
44+
{
45+
int cnt;
46+
for (cnt = 0; cnt < len; cnt++) {
47+
int ch = name[cnt];
48+
if (!ch || ch == '\n')
49+
return -1;
50+
fputc(ch, out);
51+
}
52+
return len;
53+
}
54+
55+
static int show_path_truncated(FILE *out, const struct name_path *path)
56+
{
57+
int emitted, ours;
58+
59+
if (!path)
60+
return 0;
61+
emitted = show_path_truncated(out, path->up);
62+
if (emitted < 0)
63+
return emitted;
64+
if (emitted)
65+
fputc('/', out);
66+
ours = show_path_component_truncated(out, path->elem, path->elem_len);
67+
if (ours < 0)
68+
return ours;
69+
return ours || emitted;
70+
}
71+
72+
void show_object_with_name(FILE *out, struct object *obj, const struct name_path *path, const char *component)
73+
{
74+
struct name_path leaf;
75+
leaf.up = (struct name_path *)path;
76+
leaf.elem = component;
77+
leaf.elem_len = strlen(component);
78+
79+
fprintf(out, "%s ", sha1_to_hex(obj->sha1));
80+
show_path_truncated(out, &leaf);
81+
fputc('\n', out);
82+
}
83+
4384
void add_object(struct object *obj,
4485
struct object_array *p,
4586
struct name_path *path,

revision.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ struct name_path {
205205

206206
char *path_name(const struct name_path *path, const char *name);
207207

208+
extern void show_object_with_name(FILE *, struct object *, const struct name_path *, const char *);
209+
208210
extern void add_object(struct object *obj,
209211
struct object_array *p,
210212
struct name_path *path,

upload-pack.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,7 @@ static void show_commit(struct commit *commit, void *data)
8686

8787
static void show_object(struct object *obj, const struct name_path *path, const char *component)
8888
{
89-
/* An object with name "foo\n0000000..." can be used to
90-
* confuse downstream git-pack-objects very badly.
91-
*/
92-
const char *name = path_name(path, component);
93-
const char *ep = strchr(name, '\n');
94-
if (ep) {
95-
fprintf(pack_pipe, "%s %.*s\n", sha1_to_hex(obj->sha1),
96-
(int) (ep - name),
97-
name);
98-
}
99-
else
100-
fprintf(pack_pipe, "%s %s\n",
101-
sha1_to_hex(obj->sha1), name);
102-
free((char *)name);
89+
show_object_with_name(pack_pipe, obj, path, component);
10390
}
10491

10592
static void show_edge(struct commit *commit)

0 commit comments

Comments
 (0)