From 81b50f3ce40bfdd66e5d967bf82be001039a9a98 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 22 Feb 2010 08:42:18 -0800 Subject: Move 'builtin-*' into a 'builtin/' subdirectory This shrinks the top-level directory a bit, and makes it much more pleasant to use auto-completion on the thing. Instead of [torvalds@nehalem git]$ em buil Display all 180 possibilities? (y or n) [torvalds@nehalem git]$ em builtin-sh builtin-shortlog.c builtin-show-branch.c builtin-show-ref.c builtin-shortlog.o builtin-show-branch.o builtin-show-ref.o [torvalds@nehalem git]$ em builtin-shor builtin-shortlog.c builtin-shortlog.o [torvalds@nehalem git]$ em builtin-shortlog.c you get [torvalds@nehalem git]$ em buil [type] builtin/ builtin.h [torvalds@nehalem git]$ em builtin [auto-completes to] [torvalds@nehalem git]$ em builtin/sh [type] shortlog.c shortlog.o show-branch.c show-branch.o show-ref.c show-ref.o [torvalds@nehalem git]$ em builtin/sho [auto-completes to] [torvalds@nehalem git]$ em builtin/shor [type] shortlog.c shortlog.o [torvalds@nehalem git]$ em builtin/shortlog.c which doesn't seem all that different, but not having that annoying break in "Display all 180 possibilities?" is quite a relief. NOTE! If you do this in a clean tree (no object files etc), or using an editor that has auto-completion rules that ignores '*.o' files, you won't see that annoying 'Display all 180 possibilities?' message - it will just show the choices instead. I think bash has some cut-off around 100 choices or something. So the reason I see this is that I'm using an odd editory, and thus don't have the rules to cut down on auto-completion. But you can simulate that by using 'ls' instead, or something similar. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- builtin/merge-base.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 builtin/merge-base.c (limited to 'builtin/merge-base.c') diff --git a/builtin/merge-base.c b/builtin/merge-base.c new file mode 100644 index 0000000000..54e7ec2237 --- /dev/null +++ b/builtin/merge-base.c @@ -0,0 +1,63 @@ +#include "builtin.h" +#include "cache.h" +#include "commit.h" +#include "parse-options.h" + +static int show_merge_base(struct commit **rev, int rev_nr, int show_all) +{ + struct commit_list *result; + + result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0); + + if (!result) + return 1; + + while (result) { + printf("%s\n", sha1_to_hex(result->item->object.sha1)); + if (!show_all) + return 0; + result = result->next; + } + + return 0; +} + +static const char * const merge_base_usage[] = { + "git merge-base [-a|--all] ...", + NULL +}; + +static struct commit *get_commit_reference(const char *arg) +{ + unsigned char revkey[20]; + struct commit *r; + + if (get_sha1(arg, revkey)) + die("Not a valid object name %s", arg); + r = lookup_commit_reference(revkey); + if (!r) + die("Not a valid commit name %s", arg); + + return r; +} + +int cmd_merge_base(int argc, const char **argv, const char *prefix) +{ + struct commit **rev; + int rev_nr = 0; + int show_all = 0; + + struct option options[] = { + OPT_BOOLEAN('a', "all", &show_all, "outputs all common ancestors"), + OPT_END() + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0); + if (argc < 2) + usage_with_options(merge_base_usage, options); + rev = xmalloc(argc * sizeof(*rev)); + while (argc-- > 0) + rev[rev_nr++] = get_commit_reference(*argv++); + return show_merge_base(rev, rev_nr, show_all); +} -- cgit v1.2.3 From aa8f98c1bfcf162e0bd23d20c34857940f2c2256 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Tue, 17 Aug 2010 02:01:15 -0500 Subject: merge-base --octopus to mimic show-branch --merge-base While show-branch --merge-base does not support more than MAX_REVS revs, git supports more with a different algorithm (v1.6.0-rc0~51^2~13, Introduce get_octopus_merge_bases() in commit.c, 2008-06-27). Expose that functionality. This should help scripts to catch up with builtin merge in supporting dodecapus. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- builtin/merge-base.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'builtin/merge-base.c') diff --git a/builtin/merge-base.c b/builtin/merge-base.c index 54e7ec2237..c30128659f 100644 --- a/builtin/merge-base.c +++ b/builtin/merge-base.c @@ -23,7 +23,7 @@ static int show_merge_base(struct commit **rev, int rev_nr, int show_all) } static const char * const merge_base_usage[] = { - "git merge-base [-a|--all] ...", + "git merge-base [-a|--all] [--octopus] ...", NULL }; @@ -41,21 +41,50 @@ static struct commit *get_commit_reference(const char *arg) return r; } +static int show_octopus_merge_bases(int count, const char **args, int show_all) +{ + struct commit_list *revs = NULL; + struct commit_list *result; + int i; + + for (i = count - 1; i >= 0; i++) + commit_list_insert(get_commit_reference(args[i]), &revs); + result = get_octopus_merge_bases(revs); + + if (!result) + return 1; + + while (result) { + printf("%s\n", sha1_to_hex(result->item->object.sha1)); + if (!show_all) + return 0; + result = result->next; + } + + return 0; +} + int cmd_merge_base(int argc, const char **argv, const char *prefix) { struct commit **rev; int rev_nr = 0; int show_all = 0; + int octopus = 0; struct option options[] = { - OPT_BOOLEAN('a', "all", &show_all, "outputs all common ancestors"), + OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"), + OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"), OPT_END() }; git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0); - if (argc < 2) + if (!octopus && argc < 2) usage_with_options(merge_base_usage, options); + + if (octopus) + return show_octopus_merge_bases(argc, argv, show_all); + rev = xmalloc(argc * sizeof(*rev)); while (argc-- > 0) rev[rev_nr++] = get_commit_reference(*argv++); -- cgit v1.2.3 From a1e0ad78b784fd7c47c7bc2847f4813aca4cebaf Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Tue, 17 Aug 2010 02:01:54 -0500 Subject: merge-base --independent to print reduced parent list in a merge While show-branch --independent does not support more than MAX_REVS revs, git internally supports more with a different algorithm. Expose that functionality as "git merge-base --independent". This should help scripts to catch up with builtin merge in supporting dodecapus. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- builtin/merge-base.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'builtin/merge-base.c') diff --git a/builtin/merge-base.c b/builtin/merge-base.c index c30128659f..96dd160731 100644 --- a/builtin/merge-base.c +++ b/builtin/merge-base.c @@ -24,6 +24,7 @@ static int show_merge_base(struct commit **rev, int rev_nr, int show_all) static const char * const merge_base_usage[] = { "git merge-base [-a|--all] [--octopus] ...", + "git merge-base --independent ...", NULL }; @@ -41,15 +42,19 @@ static struct commit *get_commit_reference(const char *arg) return r; } -static int show_octopus_merge_bases(int count, const char **args, int show_all) +static int handle_octopus(int count, const char **args, int reduce, int show_all) { struct commit_list *revs = NULL; struct commit_list *result; int i; - for (i = count - 1; i >= 0; i++) + if (reduce) + show_all = 1; + + for (i = count - 1; i >= 0; i--) commit_list_insert(get_commit_reference(args[i]), &revs); - result = get_octopus_merge_bases(revs); + + result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs); if (!result) return 1; @@ -70,20 +75,24 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix) int rev_nr = 0; int show_all = 0; int octopus = 0; + int reduce = 0; struct option options[] = { OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"), OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"), + OPT_BOOLEAN(0, "independent", &reduce, "list revs not reachable from others"), OPT_END() }; git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0); - if (!octopus && argc < 2) + if (!octopus && !reduce && argc < 2) usage_with_options(merge_base_usage, options); + if (reduce && (show_all || octopus)) + die("--independent cannot be used with other options"); - if (octopus) - return show_octopus_merge_bases(argc, argv, show_all); + if (octopus || reduce) + return handle_octopus(argc, argv, reduce, show_all); rev = xmalloc(argc * sizeof(*rev)); while (argc-- > 0) -- cgit v1.2.3 From 5729482429725c4688c678701ca5197eaf367a07 Mon Sep 17 00:00:00 2001 From: Vincent van Ravesteijn Date: Fri, 15 Apr 2011 10:34:03 +0200 Subject: Documentation: update to git-merge-base --octopus Unlike plain merge-base, merge-base --octopus only requires at least one commit argument; update the synopsis to reflect that. Add a sentence to the discussion that when --octopus is used, we do expect '2' (the common ansestor across all) as the result. Signed-off-by: Vincent van Ravesteijn Reviewed-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- builtin/merge-base.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'builtin/merge-base.c') diff --git a/builtin/merge-base.c b/builtin/merge-base.c index 96dd160731..4f30f1b0c8 100644 --- a/builtin/merge-base.c +++ b/builtin/merge-base.c @@ -23,7 +23,8 @@ static int show_merge_base(struct commit **rev, int rev_nr, int show_all) } static const char * const merge_base_usage[] = { - "git merge-base [-a|--all] [--octopus] ...", + "git merge-base [-a|--all] ...", + "git merge-base [-a|--all] --octopus ...", "git merge-base --independent ...", NULL }; -- cgit v1.2.3