Skip to content

Commit 58dbe58

Browse files
pcloudsgitster
authored andcommitted
shallow.c: use commit-slab for commit depth instead of commit->util
It's done so that commit->util can be removed. See more explanation in the commit that removes commit->util. While at there, plug a leak for keeping track of depth in this code. Signed-off-by: Junio C Hamano <[email protected]>
1 parent c6b7206 commit 58dbe58

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

shallow.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "commit-slab.h"
1313
#include "revision.h"
1414
#include "list-objects.h"
15+
#include "commit-slab.h"
1516

1617
static int is_shallow = -1;
1718
static struct stat_validity shallow_stat;
@@ -74,6 +75,11 @@ int is_repository_shallow(void)
7475
return is_shallow;
7576
}
7677

78+
/*
79+
* TODO: use "int" elemtype instead of "int *" when/if commit-slab
80+
* supports a "valid" flag.
81+
*/
82+
define_commit_slab(commit_depth, int *);
7783
struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
7884
int shallow_flag, int not_shallow_flag)
7985
{
@@ -82,25 +88,29 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
8288
struct object_array stack = OBJECT_ARRAY_INIT;
8389
struct commit *commit = NULL;
8490
struct commit_graft *graft;
91+
struct commit_depth depths;
8592

93+
init_commit_depth(&depths);
8694
while (commit || i < heads->nr || stack.nr) {
8795
struct commit_list *p;
8896
if (!commit) {
8997
if (i < heads->nr) {
98+
int **depth_slot;
9099
commit = (struct commit *)
91100
deref_tag(heads->objects[i++].item, NULL, 0);
92101
if (!commit || commit->object.type != OBJ_COMMIT) {
93102
commit = NULL;
94103
continue;
95104
}
96-
if (!commit->util)
97-
commit->util = xmalloc(sizeof(int));
98-
*(int *)commit->util = 0;
105+
depth_slot = commit_depth_at(&depths, commit);
106+
if (!*depth_slot)
107+
*depth_slot = xmalloc(sizeof(int));
108+
**depth_slot = 0;
99109
cur_depth = 0;
100110
} else {
101111
commit = (struct commit *)
102112
object_array_pop(&stack);
103-
cur_depth = *(int *)commit->util;
113+
cur_depth = **commit_depth_at(&depths, commit);
104114
}
105115
}
106116
parse_commit_or_die(commit);
@@ -116,25 +126,31 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
116126
}
117127
commit->object.flags |= not_shallow_flag;
118128
for (p = commit->parents, commit = NULL; p; p = p->next) {
119-
if (!p->item->util) {
120-
int *pointer = xmalloc(sizeof(int));
121-
p->item->util = pointer;
122-
*pointer = cur_depth;
129+
int **depth_slot = commit_depth_at(&depths, p->item);
130+
if (!*depth_slot) {
131+
*depth_slot = xmalloc(sizeof(int));
132+
**depth_slot = cur_depth;
123133
} else {
124-
int *pointer = p->item->util;
125-
if (cur_depth >= *pointer)
134+
if (cur_depth >= **depth_slot)
126135
continue;
127-
*pointer = cur_depth;
136+
**depth_slot = cur_depth;
128137
}
129138
if (p->next)
130139
add_object_array(&p->item->object,
131140
NULL, &stack);
132141
else {
133142
commit = p->item;
134-
cur_depth = *(int *)commit->util;
143+
cur_depth = **commit_depth_at(&depths, commit);
135144
}
136145
}
137146
}
147+
for (i = 0; i < depths.slab_count; i++) {
148+
int j;
149+
150+
for (j = 0; j < depths.slab_size; j++)
151+
free(depths.slab[i][j]);
152+
}
153+
clear_commit_depth(&depths);
138154

139155
return result;
140156
}

0 commit comments

Comments
 (0)