From: Junio C Hamano <gitster@pobox.com>
To: Miklos Vajna <vmiklos@frugalware.org>
Cc: git@vger.kernel.org
Subject: [PATCH 2/2] Introduce reduce_heads()
Date: Sat, 21 Jun 2008 02:45:55 -0700 [thread overview]
Message-ID: <7vfxr7azos.fsf@gitster.siamese.dyndns.org> (raw)
In-Reply-To: 1214007784-4801-1-git-send-email-vmiklos@frugalware.org
The new function reduce_heads() is given a list of commits, and removes
ones that can be reached from other commits in the list. It is useful for
reducing the commits randomly thrown at the git-merge command and remove
redundant commits that the user shouldn't have given to it.
The implementation uses the get_merge_bases_many() introduced in the
previous commit. If the merge base between one commit taken from the list
and the remaining commits is the commit itself, that means the commit is
reachable from some of the other commits.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
* The important part of this patch is the addition to commit.c to show
how I would write your filter_independent(). The new command is not
essential, but is included as a demonstration.
Makefile | 1 +
builtin-reduce-heads.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
builtin.h | 1 +
commit.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
commit.h | 2 ++
git.c | 1 +
6 files changed, 94 insertions(+), 0 deletions(-)
create mode 100644 builtin-reduce-heads.c
diff --git a/Makefile b/Makefile
index b003e3e..d660756 100644
--- a/Makefile
+++ b/Makefile
@@ -522,6 +522,7 @@ BUILTIN_OBJS += builtin-pack-refs.o
BUILTIN_OBJS += builtin-prune-packed.o
BUILTIN_OBJS += builtin-prune.o
BUILTIN_OBJS += builtin-push.o
+BUILTIN_OBJS += builtin-reduce-heads.o
BUILTIN_OBJS += builtin-read-tree.o
BUILTIN_OBJS += builtin-reflog.o
BUILTIN_OBJS += builtin-remote.o
diff --git a/builtin-reduce-heads.c b/builtin-reduce-heads.c
new file mode 100644
index 0000000..ff6178d
--- /dev/null
+++ b/builtin-reduce-heads.c
@@ -0,0 +1,44 @@
+#include "cache.h"
+#include "commit.h"
+#include "strbuf.h"
+
+static void show_commit_list(struct commit_list *list)
+{
+ while (list) {
+ struct strbuf it;
+ struct commit *commit = list->item;
+
+ list = list->next;
+ strbuf_init(&it, 128);
+ parse_commit(commit);
+ pretty_print_commit(CMIT_FMT_ONELINE, commit, &it,
+ 0, NULL, NULL, 0, 0);
+ puts(it.buf);
+ strbuf_release(&it);
+ }
+}
+
+int cmd_reduce_heads(int ac, const char **av, const char *prefix)
+{
+ struct commit_list *list = NULL, **tail = &list;
+ int i;
+
+ for (i = 1; i < ac; i++) {
+ struct commit *commit;
+ unsigned char sha1[20];
+
+ if (get_sha1(av[i], sha1))
+ die("'%s' is not a valid ref.", av[i]);
+ commit = lookup_commit_reference(sha1);
+ if (!commit)
+ die("cannot find commit %s", av[i]);
+ tail = &commit_list_insert(commit, tail)->next;
+ }
+
+ show_commit_list(list);
+ putchar('\n');
+
+ list = reduce_heads(list);
+ show_commit_list(list);
+ return 0;
+}
diff --git a/builtin.h b/builtin.h
index b460b2d..b6fb163 100644
--- a/builtin.h
+++ b/builtin.h
@@ -72,6 +72,7 @@ extern int cmd_read_tree(int argc, const char **argv, const char *prefix);
extern int cmd_reflog(int argc, const char **argv, const char *prefix);
extern int cmd_remote(int argc, const char **argv, const char *prefix);
extern int cmd_config(int argc, const char **argv, const char *prefix);
+extern int cmd_reduce_heads(int argc, const char **argv, const char *prefix);
extern int cmd_rerere(int argc, const char **argv, const char *prefix);
extern int cmd_reset(int argc, const char **argv, const char *prefix);
extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
diff --git a/commit.c b/commit.c
index 4ee234d..0fc4acb 100644
--- a/commit.c
+++ b/commit.c
@@ -690,3 +690,48 @@ int in_merge_bases(struct commit *commit, struct commit **reference, int num)
free_commit_list(bases);
return ret;
}
+
+struct commit_list *reduce_heads(struct commit_list *heads)
+{
+ struct commit_list *p;
+ struct commit_list *result = NULL, **tail = &result;
+ struct commit **other;
+ size_t num_head, num_other;
+
+ if (!heads)
+ return NULL;
+
+ /* Avoid unnecessary reallocations */
+ for (p = heads, num_head = 0; p; p = p->next)
+ num_head++;
+ other = xcalloc(sizeof(*other), num_head);
+
+ /* For each commit, see if it can be reached by others */
+ for (p = heads; p; p = p->next) {
+ struct commit_list *q, *base;
+
+ num_other = 0;
+ for (q = heads; q; q = q->next) {
+ if (p == q)
+ continue;
+ other[num_other++] = q->item;
+ }
+ if (num_other) {
+ base = get_merge_bases_many(p->item, num_other, other, 1);
+ } else
+ base = NULL;
+ /*
+ * If p->item does not have anything common with other
+ * commits, there won't be any merge base. If it is
+ * reachable from some of the others, p->item will be
+ * the merge base. If its history is connected with
+ * others, but p->item is not reachable by others, we
+ * will get something other than p->item back.
+ */
+ if (!base || (base->item != p->item))
+ tail = &(commit_list_insert(p->item, tail)->next);
+ free_commit_list(base);
+ }
+ free(other);
+ return result;
+}
diff --git a/commit.h b/commit.h
index 2d94d41..ec95102 100644
--- a/commit.h
+++ b/commit.h
@@ -138,4 +138,6 @@ static inline int single_parent(struct commit *commit)
return commit->parents && !commit->parents->next;
}
+struct commit_list *reduce_heads(struct commit_list *heads);
+
#endif /* COMMIT_H */
diff --git a/git.c b/git.c
index 59f0fcc..23b690d 100644
--- a/git.c
+++ b/git.c
@@ -338,6 +338,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "prune-packed", cmd_prune_packed, RUN_SETUP },
{ "push", cmd_push, RUN_SETUP },
{ "read-tree", cmd_read_tree, RUN_SETUP },
+ { "reduce-heads", cmd_reduce_heads, RUN_SETUP },
{ "reflog", cmd_reflog, RUN_SETUP },
{ "remote", cmd_remote, RUN_SETUP },
{ "repo-config", cmd_config },
--
1.5.6.6.gd3e97
next prev parent reply other threads:[~2008-06-21 9:47 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-19 23:22 [PATCH 00/11] Build in merge Miklos Vajna
2008-06-19 23:22 ` [PATCH 01/11] Move split_cmdline() to alias.c Miklos Vajna
2008-06-19 23:22 ` [PATCH 02/11] Move commit_list_count() to commit.c Miklos Vajna
2008-06-19 23:22 ` [PATCH 03/11] Move parse-options's skip_prefix() to git-compat-util.h Miklos Vajna
2008-06-19 23:22 ` [PATCH 04/11] Add new test to ensure git-merge handles pull.twohead and pull.octopus Miklos Vajna
2008-06-19 23:22 ` [PATCH 05/11] parseopt: add a new PARSE_OPT_ARGV0_IS_AN_OPTION option Miklos Vajna
2008-06-19 23:22 ` [PATCH 06/11] Move read_cache_unmerged() to read-cache.c Miklos Vajna
2008-06-19 23:22 ` [PATCH 07/11] git-fmt-merge-msg: make it usable from other builtins Miklos Vajna
2008-06-19 23:22 ` [PATCH 08/11] Introduce get_octopus_merge_bases() in commit.c Miklos Vajna
2008-06-19 23:22 ` [PATCH 09/11] Introduce filter_independent() " Miklos Vajna
2008-06-19 23:22 ` [PATCH 10/11] Build in merge Miklos Vajna
2008-06-19 23:22 ` [PATCH 11/11] Add new test to ensure git-merge handles more than 25 refs Miklos Vajna
2008-06-20 3:03 ` [PATCH 09/11] Introduce filter_independent() in commit.c Junio C Hamano
2008-06-20 11:53 ` Johannes Schindelin
2008-06-20 12:06 ` Miklos Vajna
2008-06-20 12:37 ` Johannes Schindelin
2008-06-20 13:25 ` Miklos Vajna
2008-06-21 0:23 ` [PATCH] " Miklos Vajna
2008-06-21 9:45 ` Junio C Hamano
2008-06-21 17:00 ` [PATCH 00/13] Build in merge Miklos Vajna
2008-06-21 17:00 ` [PATCH 09/13] Add new test to ensure git-merge handles more than 25 refs Miklos Vajna
2008-06-21 17:00 ` [PATCH 10/13] Introduce get_merge_bases_many() Miklos Vajna
2008-06-21 17:00 ` [PATCH 11/13] Introduce reduce_heads() Miklos Vajna
2008-06-21 17:00 ` [PATCH 12/13] Build in merge Miklos Vajna
2008-06-21 17:00 ` [PATCH 13/13] Add new test case to ensure git-merge filters for independent parents Miklos Vajna
2008-06-21 17:15 ` [PATCH 13/13] Add new test case to ensure git-merge reduces octopus parents when possible Miklos Vajna
2008-06-25 16:22 ` [PATCH 12/13] Build in merge Olivier Marin
2008-06-27 1:06 ` Miklos Vajna
2008-06-27 11:03 ` Olivier Marin
2008-06-27 12:54 ` Miklos Vajna
2008-06-27 13:04 ` Olivier Marin
2008-06-27 13:17 ` Miklos Vajna
2008-06-27 11:56 ` Johannes Schindelin
2008-06-27 13:01 ` Miklos Vajna
2008-06-21 9:45 ` [PATCH 1/2] Introduce get_merge_bases_many() Junio C Hamano
2008-06-21 9:45 ` Junio C Hamano [this message]
2008-06-20 3:04 ` [PATCH 00/11] Build in merge Junio C Hamano
2008-06-21 0:32 ` Miklos Vajna
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7vfxr7azos.fsf@gitster.siamese.dyndns.org \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=vmiklos@frugalware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).