From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH v2 3/6] name-rev: --weight option
Date: Wed, 29 Aug 2012 20:50:26 -0700 [thread overview]
Message-ID: <1346298629-13730-4-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1346298629-13730-1-git-send-email-gitster@pobox.com>
Instead of naming a rev after a tip that is topologically closest,
use the tip that is the oldest one among those which contain the
rev.
The semantics "name-rev --weight" would give us is closer to what
people expect from "describe --contains".
Note that this is fairly expensive to compute; a later change in the
series will cache the weight value using notes-cache.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
builtin/name-rev.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 102 insertions(+), 2 deletions(-)
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index ebbf541..7cdb758 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -4,6 +4,8 @@
#include "tag.h"
#include "refs.h"
#include "parse-options.h"
+#include "diff.h"
+#include "revision.h"
#define CUTOFF_DATE_SLOP 86400 /* one day */
@@ -11,8 +13,85 @@ struct rev_name {
const char *tip_name;
int generation;
int distance;
+ int weight;
};
+/*
+ * Historically, "name-rev" named a rev based on the tip that is
+ * topologically closest to it.
+ *
+ * It does not give a good answer to "what is the earliest tag that
+ * contains the commit?", however, because you can build a new commit
+ * on top of an ancient commit X, merge it to the tip and tag the
+ * result, which would make X reachable from the new tag in two hops,
+ * even though it appears in the part of the history that is contained
+ * in other ancient tags.
+ *
+ * In order to answer that question, "name-rev" can be told to name a
+ * rev based on the tip that has smallest number of commits behind it.
+ */
+static int use_weight;
+
+/*
+ * NEEDSWORK: the result of this computation must be cached to
+ * a dedicated notes tree, keyed by the commit object name.
+ */
+static int compute_ref_weight(struct commit *commit)
+{
+ struct rev_info revs;
+ int weight = 1; /* give root the weight of 1 */
+
+ reset_revision_walk();
+ init_revisions(&revs, NULL);
+ add_pending_object(&revs, (struct object *)commit, NULL);
+ prepare_revision_walk(&revs);
+ while (get_revision(&revs))
+ weight++;
+ return weight;
+}
+
+static int ref_weight(const char *refname, size_t reflen)
+{
+ struct strbuf buf = STRBUF_INIT;
+ unsigned char sha1[20];
+ struct commit *commit;
+ struct rev_name *name;
+
+ strbuf_add(&buf, refname, reflen);
+ if (get_sha1(buf.buf, sha1))
+ die("Internal error: cannot parse tip '%s'", buf.buf);
+ strbuf_release(&buf);
+
+ commit = lookup_commit_reference_gently(sha1, 0);
+ if (!commit)
+ die("Internal error: cannot look up commit '%s'", buf.buf);
+ name = commit->util;
+ if (!name)
+ die("Internal error: a tip without name '%s'", buf.buf);
+ if (!name->weight)
+ name->weight = compute_ref_weight(commit);
+ return name->weight;
+}
+
+static int tip_weight_cmp(const char *a, const char *b)
+{
+ size_t reflen_a, reflen_b;
+ static const char traversal[] = "^~";
+
+ /*
+ * A "tip" may look like <refname> followed by traversal
+ * instruction (e.g. ^2~74). We only are interested in
+ * the weight of the ref part.
+ */
+ reflen_a = strcspn(a, traversal);
+ reflen_b = strcspn(b, traversal);
+
+ if (reflen_a == reflen_b && !memcmp(a, b, reflen_a))
+ return 0;
+
+ return ref_weight(a, reflen_a) - ref_weight(b, reflen_b);
+}
+
static long cutoff = LONG_MAX;
/* How many generations are maximally preferred over _one_ merge traversal? */
@@ -49,8 +128,27 @@ static void name_rev(struct commit *commit,
use_this_tip = 1;
}
- if (distance < name->distance)
- use_this_tip = 1;
+ if (!use_weight) {
+ if (distance < name->distance)
+ use_this_tip = 1;
+ } else {
+ if (!name->tip_name)
+ use_this_tip = 1;
+ else {
+ /*
+ * Pick a name based on the ref that is older,
+ * i.e. having smaller number of commits
+ * behind it. Break the tie by picking the
+ * path with smaller numer of steps to reach
+ * that ref from the commit.
+ */
+ int cmp = tip_weight_cmp(name->tip_name, tip_name);
+ if (0 < cmp)
+ use_this_tip = 1;
+ else if (!cmp && distance < name->distance)
+ use_this_tip = 1;
+ }
+ }
if (!use_this_tip)
return;
@@ -241,6 +339,8 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
OPT_BOOLEAN(0, "undefined", &allow_undefined, "allow to print `undefined` names"),
OPT_BOOLEAN(0, "always", &always,
"show abbreviated commit object as fallback"),
+ OPT_BOOLEAN(0, "weight", &use_weight,
+ "name revs based on the oldest tip that contain them"),
OPT_END(),
};
--
1.7.12.286.g9df01f7
next prev parent reply other threads:[~2012-08-30 3:51 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-08-30 3:50 [PATCH v2 0/6] describe --contains / name-rev --weight Junio C Hamano
2012-08-30 3:50 ` [PATCH v2 1/6] name-rev: lose unnecessary typedef Junio C Hamano
2012-08-30 3:50 ` [PATCH v2 2/6] name_rev: clarify the logic to assign a new tip-name to a commit Junio C Hamano
2012-08-30 3:50 ` Junio C Hamano [this message]
2012-09-04 22:29 ` [PATCH 3.5/6] name-rev --weight: trivial optimization Junio C Hamano
2012-08-30 3:50 ` [PATCH v2 4/6] name-rev --weight: cache the computed weight in notes Junio C Hamano
2012-08-30 3:50 ` [PATCH v2 5/6] name-rev --weight: tests and documentation Junio C Hamano
2012-08-30 3:50 ` [PATCH v2 6/6] describe --contains: use "name-rev --weight" Junio C Hamano
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=1346298629-13730-4-git-send-email-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.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).