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

Built-in cherry

This replaces the shell script git-cherry with a version written in C. The behaviour of the new version differs from the original in two points: it has no long help any more, and it is handling the (optional) third parameter a bit differently. Basically, it does the equivalent of ours=`git-rev-list $ours ^$limit ^$upstream` instead of ours=`git-rev-list $ours ^$limit` Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>

Rene Scharfe committed Oct 24, 2006 at 01:01 UTC e827633a5d7d627eb1170b2d0c71e944d0d56faf
5 files changed +110 -93
Makefile
+2 -2
@@ -157,7 +157,7 @@ BASIC_LDFLAGS =
157
158 SCRIPT_SH = \
159 git-bisect.sh git-branch.sh git-checkout.sh \
160 - git-cherry.sh git-clean.sh git-clone.sh git-commit.sh \
160 + git-clean.sh git-clone.sh git-commit.sh \
161 git-fetch.sh \
162 git-ls-remote.sh \
163 git-merge-one-file.sh git-parse-remote.sh \
@@ -210,7 +210,7 @@ PROGRAMS = \
210 EXTRA_PROGRAMS =
211
212 BUILT_INS = \
213 - git-format-patch$X git-show$X git-whatchanged$X \
213 + git-format-patch$X git-show$X git-whatchanged$X git-cherry$X \
214 git-get-tar-commit-id$X \
215 $(patsubst builtin-%.o,git-%$X,$(BUILTIN_OBJS))
216
builtin-log.c
+106
@@ -437,3 +437,109 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
437 return 0;
438 }
439
440 +static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
441 +{
442 + unsigned char sha1[20];
443 + if (get_sha1(arg, sha1) == 0) {
444 + struct commit *commit = lookup_commit_reference(sha1);
445 + if (commit) {
446 + commit->object.flags |= flags;
447 + add_pending_object(revs, &commit->object, arg);
448 + return 0;
449 + }
450 + }
451 + return -1;
452 +}
453 +
454 +static const char cherry_usage[] =
455 +"git-cherry [-v] <upstream> [<head>] [<limit>]";
456 +int cmd_cherry(int argc, const char **argv, const char *prefix)
457 +{
458 + struct rev_info revs;
459 + struct diff_options patch_id_opts;
460 + struct commit *commit;
461 + struct commit_list *list = NULL;
462 + const char *upstream;
463 + const char *head = "HEAD";
464 + const char *limit = NULL;
465 + int verbose = 0;
466 +
467 + if (argc > 1 && !strcmp(argv[1], "-v")) {
468 + verbose = 1;
469 + argc--;
470 + argv++;
471 + }
472 +
473 + switch (argc) {
474 + case 4:
475 + limit = argv[3];
476 + /* FALLTHROUGH */
477 + case 3:
478 + head = argv[2];
479 + /* FALLTHROUGH */
480 + case 2:
481 + upstream = argv[1];
482 + break;
483 + default:
484 + usage(cherry_usage);
485 + }
486 +
487 + init_revisions(&revs, prefix);
488 + revs.diff = 1;
489 + revs.combine_merges = 0;
490 + revs.ignore_merges = 1;
491 + revs.diffopt.recursive = 1;
492 +
493 + if (add_pending_commit(head, &revs, 0))
494 + die("Unknown commit %s", head);
495 + if (add_pending_commit(upstream, &revs, UNINTERESTING))
496 + die("Unknown commit %s", upstream);
497 +
498 + /* Don't say anything if head and upstream are the same. */
499 + if (revs.pending.nr == 2) {
500 + struct object_array_entry *o = revs.pending.objects;
501 + if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
502 + return 0;
503 + }
504 +
505 + get_patch_ids(&revs, &patch_id_opts, prefix);
506 +
507 + if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
508 + die("Unknown commit %s", limit);
509 +
510 + /* reverse the list of commits */
511 + prepare_revision_walk(&revs);
512 + while ((commit = get_revision(&revs)) != NULL) {
513 + /* ignore merges */
514 + if (commit->parents && commit->parents->next)
515 + continue;
516 +
517 + commit_list_insert(commit, &list);
518 + }
519 +
520 + while (list) {
521 + unsigned char sha1[20];
522 + char sign = '+';
523 +
524 + commit = list->item;
525 + if (!get_patch_id(commit, &patch_id_opts, sha1) &&
526 + lookup_object(sha1))
527 + sign = '-';
528 +
529 + if (verbose) {
530 + static char buf[16384];
531 + pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
532 + buf, sizeof(buf), 0, NULL, NULL, 0);
533 + printf("%c %s %s\n", sign,
534 + sha1_to_hex(commit->object.sha1), buf);
535 + }
536 + else {
537 + printf("%c %s\n", sign,
538 + sha1_to_hex(commit->object.sha1));
539 + }
540 +
541 + list = list->next;
542 + }
543 +
544 + return 0;
545 +}
builtin.h
+1
@@ -19,6 +19,7 @@ extern int cmd_archive(int argc, const char **argv, const char *prefix);
19 extern int cmd_cat_file(int argc, const char **argv, const char *prefix);
20 extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);
21 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
22 +extern int cmd_cherry(int argc, const char **argv, const char *prefix);
23 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
24 extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
25 extern int cmd_diff_files(int argc, const char **argv, const char *prefix);
git-cherry.sh deleted
-91
@@ -1,91 +0,0 @@
1 -#!/bin/sh
2 -#
3 -# Copyright (c) 2005 Junio C Hamano.
4 -#
5 -
6 -USAGE='[-v] <upstream> [<head>] [<limit>]'
7 -LONG_USAGE=' __*__*__*__*__> <upstream>
8 - /
9 - fork-point
10 - \__+__+__+__+__+__+__+__> <head>
11 -
12 -Each commit between the fork-point (or <limit> if given) and <head> is
13 -examined, and compared against the change each commit between the
14 -fork-point and <upstream> introduces. If the change seems to be in
15 -the upstream, it is shown on the standard output with prefix "+".
16 -Otherwise it is shown with prefix "-".'
17 -. git-sh-setup
18 -
19 -case "$1" in -v) verbose=t; shift ;; esac
20 -
21 -case "$#,$1" in
22 -1,*..*)
23 - upstream=$(expr "z$1" : 'z\(.*\)\.\.') ours=$(expr "z$1" : '.*\.\.\(.*\)$')
24 - set x "$upstream" "$ours"
25 - shift ;;
26 -esac
27 -
28 -case "$#" in
29 -1) upstream=`git-rev-parse --verify "$1"` &&
30 - ours=`git-rev-parse --verify HEAD` || exit
31 - limit="$upstream"
32 - ;;
33 -2) upstream=`git-rev-parse --verify "$1"` &&
34 - ours=`git-rev-parse --verify "$2"` || exit
35 - limit="$upstream"
36 - ;;
37 -3) upstream=`git-rev-parse --verify "$1"` &&
38 - ours=`git-rev-parse --verify "$2"` &&
39 - limit=`git-rev-parse --verify "$3"` || exit
40 - ;;
41 -*) usage ;;
42 -esac
43 -
44 -# Note that these list commits in reverse order;
45 -# not that the order in inup matters...
46 -inup=`git-rev-list ^$ours $upstream` &&
47 -ours=`git-rev-list $ours ^$limit` || exit
48 -
49 -tmp=.cherry-tmp$$
50 -patch=$tmp-patch
51 -mkdir $patch
52 -trap "rm -rf $tmp-*" 0 1 2 3 15
53 -
54 -for c in $inup
55 -do
56 - git-diff-tree -p $c
57 -done | git-patch-id |
58 -while read id name
59 -do
60 - echo $name >>$patch/$id
61 -done
62 -
63 -LF='
64 -'
65 -
66 -O=
67 -for c in $ours
68 -do
69 - set x `git-diff-tree -p $c | git-patch-id`
70 - if test "$2" != ""
71 - then
72 - if test -f "$patch/$2"
73 - then
74 - sign=-
75 - else
76 - sign=+
77 - fi
78 - case "$verbose" in
79 - t)
80 - c=$(git-rev-list --pretty=oneline --max-count=1 $c)
81 - esac
82 - case "$O" in
83 - '') O="$sign $c" ;;
84 - *) O="$sign $c$LF$O" ;;
85 - esac
86 - fi
87 -done
88 -case "$O" in
89 -'') ;;
90 -*) echo "$O" ;;
91 -esac
git.c
+1
@@ -224,6 +224,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
224 { "cat-file", cmd_cat_file, RUN_SETUP },
225 { "checkout-index", cmd_checkout_index, RUN_SETUP },
226 { "check-ref-format", cmd_check_ref_format },
227 + { "cherry", cmd_cherry, RUN_SETUP },
228 { "commit-tree", cmd_commit_tree, RUN_SETUP },
229 { "count-objects", cmd_count_objects, RUN_SETUP },
230 { "diff", cmd_diff, RUN_SETUP | USE_PAGER },