pack-objects: further work on internal rev-list logic.
This teaches the internal rev-list logic to understand options that are needed for pack handling: --all, --unpacked, and --thin. It also moves two functions from builtin-rev-list to list-objects so that the two programs can share more code. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 6, 2006 at 01:42 UTC
8d1d8f83b5d918c6071b606e321de9c31fed9e68
4 files changed
+85
-93
builtin-pack-objects.c
+39
-60
@@ -69,6 +69,7 @@ static int progress = 1;
69
static volatile sig_atomic_t progress_update;
70
static int window = 10;
71
static int pack_to_stdout;
72
+static int num_preferred_base;
73
74
/*
75
* The object names in objects array are hashed with this hashtable,
@@ -841,7 +842,7 @@ static int check_pbase_path(unsigned hash)
842
return 0;
843
}
844
844
-static void add_preferred_base_object(char *name, unsigned hash)
845
+static void add_preferred_base_object(const char *name, unsigned hash)
846
{
847
struct pbase_tree *it;
848
int cmplen = name_cmp_len(name);
@@ -870,6 +871,9 @@ static void add_preferred_base(unsigned char *sha1)
871
unsigned long size;
872
unsigned char tree_sha1[20];
873
874
+ if (window <= num_preferred_base++)
875
+ return;
876
+
877
data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
878
if (!data)
879
return;
@@ -1331,7 +1335,6 @@ static int git_pack_config(const char *k, const char *v)
1335
1336
static void read_object_list_from_stdin(void)
1337
{
1334
- int num_preferred_base = 0;
1338
char line[40 + 1 + PATH_MAX + 2];
1339
unsigned char sha1[20];
1340
unsigned hash;
@@ -1351,8 +1354,7 @@ static void read_object_list_from_stdin(void)
1354
if (get_sha1_hex(line+1, sha1))
1355
die("expected edge sha1, got garbage:\n %s",
1356
line);
1354
- if (num_preferred_base++ < window)
1355
- add_preferred_base(sha1);
1357
+ add_preferred_base(sha1);
1358
continue;
1359
}
1360
if (get_sha1_hex(line, sha1))
@@ -1364,71 +1366,36 @@ static void read_object_list_from_stdin(void)
1366
}
1367
}
1368
1367
-/* copied from rev-list but needs to do things slightly differently */
1368
-static void mark_edge_parents_uninteresting(struct commit *commit)
1369
-{
1370
- struct commit_list *parents;
1371
-
1372
- for (parents = commit->parents; parents; parents = parents->next) {
1373
- struct commit *parent = parents->item;
1374
- if (!(parent->object.flags & UNINTERESTING))
1375
- continue;
1376
- mark_tree_uninteresting(parent->tree);
1377
- }
1378
-}
1379
-
1380
-static void mark_edges_uninteresting(struct commit_list *list)
1381
-{
1382
- for ( ; list; list = list->next) {
1383
- struct commit *commit = list->item;
1384
-
1385
- if (commit->object.flags & UNINTERESTING) {
1386
- mark_tree_uninteresting(commit->tree);
1387
- continue;
1388
- }
1389
- mark_edge_parents_uninteresting(commit);
1390
- }
1391
-}
1392
-
1369
static void show_commit(struct commit *commit)
1370
{
1371
unsigned hash = name_hash("");
1372
+ add_preferred_base_object("", hash);
1373
add_object_entry(commit->object.sha1, hash, 0);
1374
}
1375
1376
static void show_object(struct object_array_entry *p)
1377
{
1378
unsigned hash = name_hash(p->name);
1379
+ add_preferred_base_object(p->name, hash);
1380
add_object_entry(p->item->sha1, hash, 0);
1381
}
1382
1405
-static void get_object_list(int unpacked, int all)
1383
+static void show_edge(struct commit *commit)
1384
+{
1385
+ add_preferred_base(commit->object.sha1);
1386
+}
1387
+
1388
+static void get_object_list(int ac, const char **av)
1389
{
1390
struct rev_info revs;
1391
char line[1000];
1409
- const char *av[6];
1410
- int ac;
1392
int flags = 0;
1393
1413
- av[0] = "pack-objects";
1414
- av[1] = "--objects";
1415
- ac = 2;
1416
- if (unpacked)
1417
- av[ac++] = "--unpacked";
1418
- if (all)
1419
- av[ac++] = "--all";
1420
- av[ac++] = "--stdin";
1421
- av[ac] = NULL;
1422
-
1394
init_revisions(&revs, NULL);
1395
save_commit_buffer = 0;
1396
track_object_refs = 0;
1397
setup_revisions(ac, av, &revs, NULL);
1398
1428
- /* make sure we did not get pathspecs */
1429
- if (revs.prune_data)
1430
- die("pathspec given");
1431
-
1399
while (fgets(line, sizeof(line), stdin) != NULL) {
1400
int len = strlen(line);
1401
if (line[len - 1] == '\n')
@@ -1447,8 +1414,7 @@ static void get_object_list(int unpacked, int all)
1414
}
1415
1416
prepare_revision_walk(&revs);
1450
- mark_edges_uninteresting(revs.commits);
1451
-
1417
+ mark_edges_uninteresting(revs.commits, &revs, show_edge);
1418
traverse_commit_list(&revs, show_commit, show_object);
1419
}
1420
@@ -1458,9 +1424,14 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1424
int depth = 10;
1425
struct object_entry **list;
1426
int use_internal_rev_list = 0;
1461
- int unpacked = 0;
1462
- int all = 0;
1427
+ int thin = 0;
1428
int i;
1429
+ const char *rp_av[64];
1430
+ int rp_ac;
1431
+
1432
+ rp_av[0] = "pack-objects";
1433
+ rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1434
+ rp_ac = 2;
1435
1436
git_config(git_pack_config);
1437
@@ -1521,12 +1492,19 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1492
use_internal_rev_list = 1;
1493
continue;
1494
}
1524
- if (!strcmp("--unpacked", arg)) {
1525
- unpacked = 1;
1495
+ if (!strcmp("--unpacked", arg) ||
1496
+ !strncmp("--unpacked=", arg, 11) ||
1497
+ !strcmp("--all", arg)) {
1498
+ use_internal_rev_list = 1;
1499
+ if (ARRAY_SIZE(rp_av) - 1 <= rp_ac)
1500
+ die("too many internal rev-list options");
1501
+ rp_av[rp_ac++] = arg;
1502
continue;
1503
}
1528
- if (!strcmp("--all", arg)) {
1529
- all = 1;
1504
+ if (!strcmp("--thin", arg)) {
1505
+ use_internal_rev_list = 1;
1506
+ thin = 1;
1507
+ rp_av[1] = "--objects-edge";
1508
continue;
1509
}
1510
usage(pack_usage);
@@ -1551,9 +1529,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1529
if (pack_to_stdout != !base_name)
1530
usage(pack_usage);
1531
1554
- /* --unpacked and --all makes sense only with --revs */
1555
- if (!use_internal_rev_list && (unpacked || all))
1556
- usage(pack_usage);
1532
+ if (!pack_to_stdout && thin)
1533
+ die("--thin cannot be used to build an indexable pack.");
1534
1535
prepare_packed_git();
1536
@@ -1564,8 +1541,10 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1541
1542
if (!use_internal_rev_list)
1543
read_object_list_from_stdin();
1567
- else
1568
- get_object_list(unpacked, all);
1544
+ else {
1545
+ rp_av[rp_ac] = NULL;
1546
+ get_object_list(rp_ac, rp_av);
1547
+ }
1548
1549
if (progress)
1550
fprintf(stderr, "Done counting %d objects.\n", nr_objects);
builtin-rev-list.c
+6
-30
@@ -114,6 +114,11 @@ static void show_object(struct object_array_entry *p)
114
printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
115
}
116
117
+static void show_edge(struct commit *commit)
118
+{
119
+ printf("-%s\n", sha1_to_hex(commit->object.sha1));
120
+}
121
+
122
/*
123
* This is a truly stupid algorithm, but it's only
124
* used for bisection, and we just don't care enough.
@@ -192,35 +197,6 @@ static struct commit_list *find_bisection(struct commit_list *list)
197
return best;
198
}
199
195
-static void mark_edge_parents_uninteresting(struct commit *commit)
196
-{
197
- struct commit_list *parents;
198
-
199
- for (parents = commit->parents; parents; parents = parents->next) {
200
- struct commit *parent = parents->item;
201
- if (!(parent->object.flags & UNINTERESTING))
202
- continue;
203
- mark_tree_uninteresting(parent->tree);
204
- if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
205
- parent->object.flags |= SHOWN;
206
- printf("-%s\n", sha1_to_hex(parent->object.sha1));
207
- }
208
- }
209
-}
210
-
211
-static void mark_edges_uninteresting(struct commit_list *list)
212
-{
213
- for ( ; list; list = list->next) {
214
- struct commit *commit = list->item;
215
-
216
- if (commit->object.flags & UNINTERESTING) {
217
- mark_tree_uninteresting(commit->tree);
218
- continue;
219
- }
220
- mark_edge_parents_uninteresting(commit);
221
- }
222
-}
223
-
200
static void read_revisions_from_stdin(struct rev_info *revs)
201
{
202
char line[1000];
@@ -300,7 +276,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
276
277
prepare_revision_walk(&revs);
278
if (revs.tree_objects)
303
- mark_edges_uninteresting(revs.commits);
279
+ mark_edges_uninteresting(revs.commits, &revs, show_edge);
280
281
if (bisect_list)
282
revs.commits = find_bisection(revs.commits);
list-objects.c
+33
@@ -66,6 +66,39 @@ static void process_tree(struct rev_info *revs,
66
tree->buffer = NULL;
67
}
68
69
+static void mark_edge_parents_uninteresting(struct commit *commit,
70
+ struct rev_info *revs,
71
+ show_edge_fn show_edge)
72
+{
73
+ struct commit_list *parents;
74
+
75
+ for (parents = commit->parents; parents; parents = parents->next) {
76
+ struct commit *parent = parents->item;
77
+ if (!(parent->object.flags & UNINTERESTING))
78
+ continue;
79
+ mark_tree_uninteresting(parent->tree);
80
+ if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
81
+ parent->object.flags |= SHOWN;
82
+ show_edge(parent);
83
+ }
84
+ }
85
+}
86
+
87
+void mark_edges_uninteresting(struct commit_list *list,
88
+ struct rev_info *revs,
89
+ show_edge_fn show_edge)
90
+{
91
+ for ( ; list; list = list->next) {
92
+ struct commit *commit = list->item;
93
+
94
+ if (commit->object.flags & UNINTERESTING) {
95
+ mark_tree_uninteresting(commit->tree);
96
+ continue;
97
+ }
98
+ mark_edge_parents_uninteresting(commit, revs, show_edge);
99
+ }
100
+}
101
+
102
void traverse_commit_list(struct rev_info *revs,
103
void (*show_commit)(struct commit *),
104
void (*show_object)(struct object_array_entry *))
list-objects.h
+7
-3
@@ -1,8 +1,12 @@
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 *));
4
+typedef void (*show_commit_fn)(struct commit *);
5
+typedef void (*show_object_fn)(struct object_array_entry *);
6
+typedef void (*show_edge_fn)(struct commit *);
7
+
8
+void traverse_commit_list(struct rev_info *revs, show_commit_fn, show_object_fn);
9
+
10
+void mark_edges_uninteresting(struct commit_list *, struct rev_info *, show_edge_fn);
11
12
#endif