Re: [PATCH 2/4] name-rev: allow converting the exact object name at the tip of a ref

2013-07-08 Thread Junio C Hamano
Ramkumar Ramachandra  writes:

>   Finds symbolic names suitable for human digestion for revisions
>   given in any format parsable by git rev-parse.
>
> It is meant to name _revisions_ (aka. commits):

That is a mistaken documentation, written based on a half-baked
implementation that conflated "the current code only can handle
commits" with "we need to only handle commit and nothing, ever".

We had to fix a similar misguided/short-sighted implementation in
the "git fetch" codepath when adding "git pull/merge $tag" (the code
peeled a tag object too early when writing FETCH_HEAD out, losing
information if what we were given was a tag or a commit).

I do not want to see us making the mistake worse unnecessarily.
When we see a commit object that is pointed by tag v1.8.3, it is the
right thing to do to return v1.8.3^0 as its string representation so
that "git rev-parse v1.8.3^0" give us the same thing back.  name-rev
that is fed a tag object v1.8.3 should give v1.8.3 (not v1.8.3^0)
back, otherwise feeding its output to "git rev-parse" will not give
us the same thing we fed as the input to name-rev.



--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 2/4] name-rev: allow converting the exact object name at the tip of a ref

2013-07-08 Thread Ramkumar Ramachandra
Junio C Hamano wrote:
> "git name-rev" is supposed to convert 40-hex object names into
> strings that name the same objects based on refs, that can be fed to
> "git rev-parse" to get the same object names back, so
>
> $ git rev-parse v1.8.3 v1.8.3^0 | git name-rev --stdin
> 8af06057d0c31a24e8737ae846ac2e116e8bafb9
> edca4152560522a431a51fc0a06147fc680b5b18 (tags/v1.8.3^0)

Wait, what?

  $ git name-rev 8af060
  8af060 tags/v1.8.3^0

Isn't this a failure specific to --stdin?

> Teach it to show this instead:
>
> $ git rev-parse v1.8.3 v1.8.3^0 | git name-rev --stdin
> 8af06057d0c31a24e8737ae846ac2e116e8bafb9 (tags/v1.8.3)
> edca4152560522a431a51fc0a06147fc680b5b18 (tags/v1.8.3^0)

Wait, what is name-rev?

  Finds symbolic names suitable for human digestion for revisions
  given in any format parsable by git rev-parse.

It is meant to name _revisions_ (aka. commits): in that context, what
sense does it make to distinguish between tags/v1.8.3 and
tags/v1.8.3^0?
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 2/4] name-rev: allow converting the exact object name at the tip of a ref

2013-07-07 Thread Junio C Hamano
"git name-rev" is supposed to convert 40-hex object names into
strings that name the same objects based on refs, that can be fed to
"git rev-parse" to get the same object names back, so

$ git rev-parse v1.8.3 v1.8.3^0 | git name-rev --stdin
8af06057d0c31a24e8737ae846ac2e116e8bafb9
edca4152560522a431a51fc0a06147fc680b5b18 (tags/v1.8.3^0)

has to have "^0" at the end, as "edca41" is a commit, not the tag
that references it.

The command however did not bother to see if the object is at the
tip of some ref, and failed to convert a tag object.

Teach it to show this instead:

$ git rev-parse v1.8.3 v1.8.3^0 | git name-rev --stdin
8af06057d0c31a24e8737ae846ac2e116e8bafb9 (tags/v1.8.3)
edca4152560522a431a51fc0a06147fc680b5b18 (tags/v1.8.3^0)

Signed-off-by: Junio C Hamano 
---
 builtin/name-rev.c | 59 +-
 1 file changed, 58 insertions(+), 1 deletion(-)

diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index 1234ebb..29a6f56 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -4,6 +4,7 @@
 #include "tag.h"
 #include "refs.h"
 #include "parse-options.h"
+#include "sha1-lookup.h"
 
 #define CUTOFF_DATE_SLOP 86400 /* one day */
 
@@ -113,6 +114,34 @@ struct name_ref_data {
const char *ref_filter;
 };
 
+static struct tip_table {
+   struct tip_table_entry {
+   unsigned char sha1[20];
+   const char *refname;
+   } *table;
+   int nr;
+   int alloc;
+   int sorted;
+} tip_table;
+
+static void add_to_tip_table(const unsigned char *sha1, const char *refname,
+int shorten_unambiguous)
+{
+   refname = name_ref_abbrev(refname, shorten_unambiguous);
+
+   ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
+   hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
+   tip_table.table[tip_table.nr].refname = xstrdup(refname);
+   tip_table.nr++;
+   tip_table.sorted = 0;
+}
+
+static int tipcmp(const void *a_, const void *b_)
+{
+   const struct tip_table_entry *a = a_, *b = b_;
+   return hashcmp(a->sha1, b->sha1);
+}
+
 static int name_ref(const char *path, const unsigned char *sha1, int flags, 
void *cb_data)
 {
struct object *o = parse_object(sha1);
@@ -135,6 +164,8 @@ static int name_ref(const char *path, const unsigned char 
*sha1, int flags, void
}
}
 
+   add_to_tip_table(sha1, path, can_abbreviate_output);
+
while (o && o->type == OBJ_TAG) {
struct tag *t = (struct tag *) o;
if (!t->tagged)
@@ -151,6 +182,32 @@ static int name_ref(const char *path, const unsigned char 
*sha1, int flags, void
return 0;
 }
 
+static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
+{
+   struct tip_table_entry *table = table_;
+   return table[ix].sha1;
+}
+
+static const char *get_exact_ref_match(const struct object *o)
+{
+   int found;
+
+   if (!tip_table.table || !tip_table.nr)
+   return NULL;
+
+   if (!tip_table.sorted) {
+   qsort(tip_table.table, tip_table.nr, sizeof(*tip_table.table),
+ tipcmp);
+   tip_table.sorted = 1;
+   }
+
+   found = sha1_pos(o->sha1, tip_table.table, tip_table.nr,
+nth_tip_table_ent);
+   if (0 <= found)
+   return tip_table.table[found].refname;
+   return NULL;
+}
+
 /* returns a static buffer */
 static const char *get_rev_name(const struct object *o)
 {
@@ -159,7 +216,7 @@ static const char *get_rev_name(const struct object *o)
struct commit *c;
 
if (o->type != OBJ_COMMIT)
-   return NULL;
+   return get_exact_ref_match(o);
c = (struct commit *) o;
n = c->util;
if (!n)
-- 
1.8.3.2-853-ga8cbcc9

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html