@matheusbsbs / matheus-bagliano / commits / cf4845441c
Fork 0

Teach git-rev-list to follow just a specified set of files

This is the first cut at a git-rev-list that knows to ignore commits that don't change a certain file (or set of files). NOTE! For now it only prunes _merge_ commits, and follows the parent where there are no differences in the set of files specified. In the long run, I'd like to make it re-write the straight-line history too, but for now the merge simplification is much more fundamentally important (the rewriting of straight-line history is largely a separate simplification phase, but the merge simplification needs to happen early if we want to optimize away unnecessary commit parsing). If all parents of a merge change some of the files, the merge is left as is, so the end result is in no way guaranteed to be a linear history, but it will often be a lot /more/ linear than the full tree, since it prunes out parents that didn't matter for that set of files. As an example from the current kernel: [torvalds@g5 linux]$ git-rev-list HEAD | wc -l 9885 [torvalds@g5 linux]$ git-rev-list HEAD -- Makefile | wc -l 4084 [torvalds@g5 linux]$ git-rev-list HEAD -- drivers/usb | wc -l 5206 and you can also use 'gitk' to more visually see the pruning of the history tree, with something like gitk -- drivers/usb showing a simplified history that tries to follow the first parent in a merge that is the parent that fully defines drivers/usb/. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Oct 20, 2005 at 21:25 UTC cf4845441c3931731643859dc80a79e52bcd3e0f
1 file changed +125 -4
rev-list.c
+125 -4
@@ -5,6 +5,7 @@
5 #include "tree.h"
6 #include "blob.h"
7 #include "epoch.h"
8 +#include "diff.h"
9
10 #define SEEN (1u << 0)
11 #define INTERESTING (1u << 1)
@@ -44,6 +45,7 @@ static int show_breaks = 0;
45 static int stop_traversal = 0;
46 static int topo_order = 0;
47 static int no_merges = 0;
48 +static const char **paths = NULL;
49
50 static void show_commit(struct commit *commit)
51 {
@@ -377,18 +379,129 @@ static void mark_edges_uninteresting(struct commit_list *list)
379 }
380 }
381
382 +static int is_different = 0;
383 +
384 +static void file_add_remove(struct diff_options *options,
385 + int addremove, unsigned mode,
386 + const unsigned char *sha1,
387 + const char *base, const char *path)
388 +{
389 + is_different = 1;
390 +}
391 +
392 +static void file_change(struct diff_options *options,
393 + unsigned old_mode, unsigned new_mode,
394 + const unsigned char *old_sha1,
395 + const unsigned char *new_sha1,
396 + const char *base, const char *path)
397 +{
398 + is_different = 1;
399 +}
400 +
401 +static struct diff_options diff_opt = {
402 + .recursive = 1,
403 + .add_remove = file_add_remove,
404 + .change = file_change,
405 +};
406 +
407 +static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
408 +{
409 + if (!commit->tree)
410 + return NULL;
411 +
412 + while (parent) {
413 + struct commit *p = parent->item;
414 + parent = parent->next;
415 + parse_commit(p);
416 + if (!p->tree)
417 + continue;
418 + is_different = 0;
419 + if (diff_tree_sha1(commit->tree->object.sha1,
420 + p->tree->object.sha1, "", &diff_opt) < 0)
421 + continue;
422 + if (!is_different)
423 + return p;
424 + }
425 + return NULL;
426 +}
427 +
428 +static void add_parents_to_list(struct commit *commit, struct commit_list **list)
429 +{
430 + struct commit_list *parent = commit->parents;
431 +
432 + /*
433 + * If the commit is uninteresting, don't try to
434 + * prune parents - we want the maximal uninteresting
435 + * set.
436 + *
437 + * Normally we haven't parsed the parent
438 + * yet, so we won't have a parent of a parent
439 + * here. However, it may turn out that we've
440 + * reached this commit some other way (where it
441 + * wasn't uninteresting), in which case we need
442 + * to mark its parents recursively too..
443 + */
444 + if (commit->object.flags & UNINTERESTING) {
445 + while (parent) {
446 + struct commit *p = parent->item;
447 + parent = parent->next;
448 + parse_commit(p);
449 + p->object.flags |= UNINTERESTING;
450 + if (p->parents)
451 + mark_parents_uninteresting(p);
452 + if (p->object.flags & SEEN)
453 + continue;
454 + p->object.flags |= SEEN;
455 + insert_by_date(p, list);
456 + }
457 + return;
458 + }
459 +
460 + /*
461 + * Ok, the commit wasn't uninteresting. If it
462 + * is a merge, try to find the parent that has
463 + * no differences in the path set if one exists.
464 + */
465 + if (paths && parent && parent->next) {
466 + struct commit *preferred;
467 +
468 + preferred = try_to_simplify_merge(commit, parent);
469 + if (preferred) {
470 + parent->item = preferred;
471 + parent->next = NULL;
472 + }
473 + }
474 +
475 + while (parent) {
476 + struct commit *p = parent->item;
477 +
478 + parent = parent->next;
479 +
480 + parse_commit(p);
481 + if (p->object.flags & SEEN)
482 + continue;
483 + p->object.flags |= SEEN;
484 + insert_by_date(p, list);
485 + }
486 +}
487 +
488 static struct commit_list *limit_list(struct commit_list *list)
489 {
490 struct commit_list *newlist = NULL;
491 struct commit_list **p = &newlist;
492 while (list) {
385 - struct commit *commit = pop_most_recent_commit(&list, SEEN);
493 + struct commit_list *entry = list;
494 + struct commit *commit = list->item;
495 struct object *obj = &commit->object;
496
497 + list = list->next;
498 + free(entry);
499 +
500 if (max_age != -1 && (commit->date < max_age))
501 obj->flags |= UNINTERESTING;
502 if (unpacked && has_sha1_pack(obj->sha1))
503 obj->flags |= UNINTERESTING;
504 + add_parents_to_list(commit, &list);
505 if (obj->flags & UNINTERESTING) {
506 mark_parents_uninteresting(commit);
507 if (everybody_uninteresting(list))
@@ -507,15 +620,15 @@ static void handle_all(struct commit_list **lst)
620 global_lst = NULL;
621 }
622
510 -int main(int argc, char **argv)
623 +int main(int argc, const char **argv)
624 {
625 + const char *prefix = setup_git_directory();
626 struct commit_list *list = NULL;
627 int i, limited = 0;
628
515 - setup_git_directory();
629 for (i = 1 ; i < argc; i++) {
630 int flags;
518 - char *arg = argv[i];
631 + const char *arg = argv[i];
632 char *dotdot;
633 struct commit *commit;
634
@@ -587,6 +700,14 @@ int main(int argc, char **argv)
700 limited = 1;
701 continue;
702 }
703 + if (!strcmp(arg, "--")) {
704 + paths = get_pathspec(prefix, argv + i + 1);
705 + if (paths) {
706 + limited = 1;
707 + diff_tree_setup_paths(paths);
708 + }
709 + break;
710 + }
711
712 if (show_breaks && !merge_order)
713 usage(rev_list_usage);