{"thread":{"id":"11385","subject":"[PATCH] git-describe: Add a --match option to limit considered tags.","startedAt":"2007-12-21T16:18:20Z","lastAt":"2007-12-24T15:57:23Z","messageCount":7,"participants":["Pierre Habouzit","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"63930","messageId":"1198253900-31502-1-git-send-email-madcoder@debian.org","threadId":"11385","inReplyTo":null,"subject":"[PATCH] git-describe: Add a --match option to limit considered tags.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-12-21T16:18:20Z","receivedAt":"2007-12-21T16:18:20Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"Signed-off-by: Pierre Habouzit <madcoder@debian.org>\n---\n Documentation/git-describe.txt |    4 ++++\n builtin-describe.c             |   11 ++++++++---\n 2 files changed, 12 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-describe.txt b/Documentation/git-describe.txt\nindex ac23e28..cb869e9 100644\n--- a/Documentation/git-describe.txt\n+++ b/Documentation/git-describe.txt\n@@ -51,6 +51,10 @@ OPTIONS\n \tbeing employed to standard error.  The tag name will still\n \tbe printed to standard out.\n \n+--match <pattern>::\n+        Only consider tags matching the given pattern (can be used to avoid\n+        leaking private tags made from the repository).\n+\n EXAMPLES\n --------\n \ndiff --git a/builtin-describe.c b/builtin-describe.c\nindex 7a148a2..982a355 100644\n--- a/builtin-describe.c\n+++ b/builtin-describe.c\n@@ -19,6 +19,7 @@ static int all;\t/* Default to annotated tags only */\n static int tags;\t/* But allow any tags if --tags is specified */\n static int abbrev = DEFAULT_ABBREV;\n static int max_candidates = 10;\n+const char *pattern = NULL;\n \n struct commit_name {\n \tint prio; /* annotated tag = 2, tag = 1, head = 0 */\n@@ -57,9 +58,11 @@ static int get_name(const char *path, const unsigned char *sha1, int flag, void\n \t * Otherwise only annotated tags are used.\n \t */\n \tif (!prefixcmp(path, \"refs/tags/\")) {\n-\t\tif (object->type == OBJ_TAG)\n+\t\tif (object->type == OBJ_TAG) {\n \t\t\tprio = 2;\n-\t\telse\n+\t\t\tif (pattern && fnmatch(pattern, path + 10, 0))\n+\t\t\t\tprio = 0;\n+\t\t} else\n \t\t\tprio = 1;\n \t}\n \telse\n@@ -253,7 +256,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)\n \t\tOPT_BOOLEAN(0, \"tags\",       &tags, \"use any tag in .git/refs/tags\"),\n \t\tOPT__ABBREV(&abbrev),\n \t\tOPT_INTEGER(0, \"candidates\", &max_candidates,\n-\t\t\t\t\t\"consider <n> most recent tags (default: 10)\"),\n+\t\t            \"consider <n> most recent tags (default: 10)\"),\n+\t\tOPT_STRING(0, \"match\",       &pattern, \"pattern\",\n+\t\t           \"only consider tags matching <pattern>\"),\n \t\tOPT_END(),\n \t};\n \n-- \n1.5.4.rc1.1096.gde4c4-dirty\n"},{"id":"63941","messageId":"7v7ij87x5u.fsf@gitster.siamese.dyndns.org","threadId":"11385","inReplyTo":"1198253900-31502-1-git-send-email-madcoder@debian.org","subject":"Re: [PATCH] git-describe: Add a --match option to limit considered tags.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-21T17:52:29Z","receivedAt":"2007-12-21T17:52:29Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Does it work with \"describe --contains\" as well?\n"},{"id":"63955","messageId":"20071221212212.GB10318@artemis.madism.org","threadId":"11385","inReplyTo":"7v7ij87x5u.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] git-describe: Add a --match option to limit considered tags.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-12-21T21:22:13Z","receivedAt":"2007-12-21T21:22:13Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"On Fri, Dec 21, 2007 at 05:52:29PM +0000, Junio C Hamano wrote:\n> Does it work with \"describe --contains\" as well?\n\n  I think so, the idea here is that I give prio \"0\" to tags that don't\nmatch, which unless you pass --all (which basically conflicts with\n--match <foo> anyways) means that those are not added to the list of\ntags that are considered. So I don't see why it would fail with\n--contains.\n\n  That's a patch we \"need\" at work because we have a repository with\ndifferent products that share a _lot_ of code (and we're not confident\nwith submodules yet to switch) and we use git-describe to embed the\nexact version of the code that was shipped to a client. Though it sucks\nwhen the last tag shows another product name :)\n\n  I'd also like to use it in some scripts of mine for debian packaging\nwhere I have quite a lot of private tags and only want to describe tags\nthat match 'debian-sid*'\n\n-- \n·O·  Pierre Habouzit\n··O                                                madcoder@debian.org\nOOO                                                http://www.madism.org\n"},{"id":"63958","messageId":"20071221214954.GA16481@artemis.madism.org","threadId":"11385","inReplyTo":"20071221212212.GB10318@artemis.madism.org","subject":"[FIXED PATCH] git-describe: Add a --match option to limit considered tags.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-12-21T21:49:54Z","receivedAt":"2007-12-21T21:49:54Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"Signed-off-by: Pierre Habouzit <madcoder@debian.org>\n---\n\n    On Fri, Dec 21, 2007 at 09:22:12PM +0000, Pierre Habouzit wrote:\n    > On Fri, Dec 21, 2007 at 05:52:29PM +0000, Junio C Hamano wrote:\n    > > Does it work with \"describe --contains\" as well?\n\n    Okay, you're right … Here is an updated patch.\n\n Documentation/git-describe.txt |    4 ++++\n builtin-describe.c             |   21 ++++++++++++++++-----\n 2 files changed, 20 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/git-describe.txt b/Documentation/git-describe.txt\nindex ac23e28..cb869e9 100644\n--- a/Documentation/git-describe.txt\n+++ b/Documentation/git-describe.txt\n@@ -51,6 +51,10 @@ OPTIONS\n \tbeing employed to standard error.  The tag name will still\n \tbe printed to standard out.\n \n+--match <pattern>::\n+        Only consider tags matching the given pattern (can be used to avoid\n+        leaking private tags made from the repository).\n+\n EXAMPLES\n --------\n \ndiff --git a/builtin-describe.c b/builtin-describe.c\nindex 7a148a2..dd44df9 100644\n--- a/builtin-describe.c\n+++ b/builtin-describe.c\n@@ -19,6 +19,7 @@ static int all;\t/* Default to annotated tags only */\n static int tags;\t/* But allow any tags if --tags is specified */\n static int abbrev = DEFAULT_ABBREV;\n static int max_candidates = 10;\n+const char *pattern = NULL;\n \n struct commit_name {\n \tint prio; /* annotated tag = 2, tag = 1, head = 0 */\n@@ -57,9 +58,11 @@ static int get_name(const char *path, const unsigned char *sha1, int flag, void\n \t * Otherwise only annotated tags are used.\n \t */\n \tif (!prefixcmp(path, \"refs/tags/\")) {\n-\t\tif (object->type == OBJ_TAG)\n+\t\tif (object->type == OBJ_TAG) {\n \t\t\tprio = 2;\n-\t\telse\n+\t\t\tif (pattern && fnmatch(pattern, path + 10, 0))\n+\t\t\t\tprio = 0;\n+\t\t} else\n \t\t\tprio = 1;\n \t}\n \telse\n@@ -253,7 +256,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)\n \t\tOPT_BOOLEAN(0, \"tags\",       &tags, \"use any tag in .git/refs/tags\"),\n \t\tOPT__ABBREV(&abbrev),\n \t\tOPT_INTEGER(0, \"candidates\", &max_candidates,\n-\t\t\t\t\t\"consider <n> most recent tags (default: 10)\"),\n+\t\t            \"consider <n> most recent tags (default: 10)\"),\n+\t\tOPT_STRING(0, \"match\",       &pattern, \"pattern\",\n+\t\t           \"only consider tags matching <pattern>\"),\n \t\tOPT_END(),\n \t};\n \n@@ -266,12 +271,18 @@ int cmd_describe(int argc, const char **argv, const char *prefix)\n \tsave_commit_buffer = 0;\n \n \tif (contains) {\n-\t\tconst char **args = xmalloc((4 + argc) * sizeof(char*));\n+\t\tconst char **args = xmalloc((5 + argc) * sizeof(char*));\n \t\tint i = 0;\n \t\targs[i++] = \"name-rev\";\n \t\targs[i++] = \"--name-only\";\n-\t\tif (!all)\n+\t\tif (!all) {\n \t\t\targs[i++] = \"--tags\";\n+\t\t\tif (pattern) {\n+\t\t\t\tchar *s = xmalloc(strlen(\"--refs=refs/tags/\") + strlen(pattern) + 1);\n+\t\t\t\tsprintf(s, \"--refs=refs/tags/%s\", pattern);\n+\t\t\t\targs[i++] = s;\n+\t\t\t}\n+\t\t}\n \t\tmemcpy(args + i, argv, argc * sizeof(char*));\n \t\targs[i + argc] = NULL;\n \t\treturn cmd_name_rev(i + argc, args, prefix);\n-- \n1.5.4.rc1.1097.gd122b-dirty\n\n"},{"id":"64011","messageId":"20071222180244.GD23262@artemis.madism.org","threadId":"11385","inReplyTo":"20071221214954.GA16481@artemis.madism.org","subject":"Re: [FIXED PATCH] git-describe: Add a --match option to limit considered tags.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-12-22T18:02:44Z","receivedAt":"2007-12-22T18:02:44Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"  Like I said on IRC, I saw that git describe --contains has a bad\nbehaviour:\n\n    $ git describe --match='asd*' HEAD; echo $?\n    fatal: cannot describe 'e272415ab7da3bde51af2ce95c88d7be3abfba28'\n    128\n    $ git describe --contains HEAD; echo $?\n    undefined\n    0\n\nTHe \"undefined\" output is on stdout (not stderr), and returns 0.\nThe issue here is that it internally uses git-name-rev by exec-ing it, which\nmakes it hard to fix. Though I suppose that we could instead of fork-ing\nshare some logic with builtin-name-rev.c, but I'm not at home yet, so\nwon't likely have a patch for this issue.\n\nNote that the use of the \"new\" --match here was just to be unable to\ndescribe the HEAD to show the difference, the inconsistency has nothing\nto do with the patch I propose, I just happen to noticed that.\n\nAFAICT it's not a regression, it's just a misfeature :)\n-- \n·O·  Pierre Habouzit\n··O                                                madcoder@debian.org\nOOO                                                http://www.madism.org\n"},{"id":"64072","messageId":"20071224111822.GA10356@artemis.madism.org","threadId":"11385","inReplyTo":"20071222180244.GD23262@artemis.madism.org","subject":"Re: [FIXED PATCH] git-describe: Add a --match option to limit considered tags.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-12-24T11:18:22Z","receivedAt":"2007-12-24T11:18:22Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"Rework get_rev_name to return NULL rather than \"undefined\" when a reference\nis undefined. If --undefined is passed (default) git-name-rev prints\n\"undefined\" for the name, else it die()s.\n\nMake git-describe use --no-undefined when calling git-name-rev so that\n--contains behavior matches the standard git-describe one.\n\nSigned-off-by: Pierre Habouzit <madcoder@debian.org>\n---\n\n    On sam, déc 22, 2007 at 06:02:44 +0000, Pierre Habouzit wrote:\n    >   Like I said on IRC, I saw that git describe --contains has a bad\n    > behaviour:\n    > \n    >     $ git describe --match='asd*' HEAD; echo $?\n    >     fatal: cannot describe 'e272415ab7da3bde51af2ce95c88d7be3abfba28'\n    >     128\n    >     $ git describe --contains HEAD; echo $?\n    >     undefined\n    >     0\n\n    Okay here is a patch to have git-name-rev have a strict mode where\n    it rejects undefined's.  With this patch:\n\n    $ git describe --contains HEAD\n    fatal: cannot describe 'e26a806b93f2f2f2354831ce0f943347a8ba7c3e'\n\n    This patch is minimal and looks safe for master to me. A better patch\n    would probably to have shared some logic between name-rev and\n    describe, but this would have been a quite larger patch, and I was\n    uncomfortable with it.\n\n\n builtin-describe.c |    3 ++-\n builtin-name-rev.c |   34 ++++++++++++++++++++++++----------\n 2 files changed, 26 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin-describe.c b/builtin-describe.c\nindex 18eab47..3428483 100644\n--- a/builtin-describe.c\n+++ b/builtin-describe.c\n@@ -271,10 +271,11 @@ int cmd_describe(int argc, const char **argv, const char *prefix)\n \tsave_commit_buffer = 0;\n \n \tif (contains) {\n-\t\tconst char **args = xmalloc((5 + argc) * sizeof(char*));\n+\t\tconst char **args = xmalloc((6 + argc) * sizeof(char*));\n \t\tint i = 0;\n \t\targs[i++] = \"name-rev\";\n \t\targs[i++] = \"--name-only\";\n+\t\targs[i++] = \"--no-undefined\";\n \t\tif (!all) {\n \t\t\targs[i++] = \"--tags\";\n \t\t\tif (pattern) {\ndiff --git a/builtin-name-rev.c b/builtin-name-rev.c\nindex a0c89a8..f22c8b5 100644\n--- a/builtin-name-rev.c\n+++ b/builtin-name-rev.c\n@@ -125,18 +125,18 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void\n }\n \n /* returns a static buffer */\n-static const char* get_rev_name(struct object *o)\n+static const char *get_rev_name(struct object *o)\n {\n \tstatic char buffer[1024];\n \tstruct rev_name *n;\n \tstruct commit *c;\n \n \tif (o->type != OBJ_COMMIT)\n-\t\treturn \"undefined\";\n+\t\treturn NULL;\n \tc = (struct commit *) o;\n \tn = c->util;\n \tif (!n)\n-\t\treturn \"undefined\";\n+\t\treturn NULL;\n \n \tif (!n->generation)\n \t\treturn n->tip_name;\n@@ -159,7 +159,7 @@ static char const * const name_rev_usage[] = {\n int cmd_name_rev(int argc, const char **argv, const char *prefix)\n {\n \tstruct object_array revs = { 0, 0, NULL };\n-\tint all = 0, transform_stdin = 0;\n+\tint all = 0, transform_stdin = 0, allow_undefined = 1;\n \tstruct name_ref_data data = { 0, 0, NULL };\n \tstruct option opts[] = {\n \t\tOPT_BOOLEAN(0, \"name-only\", &data.name_only, \"print only names (no SHA-1)\"),\n@@ -169,6 +169,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)\n \t\tOPT_GROUP(\"\"),\n \t\tOPT_BOOLEAN(0, \"all\", &all, \"list all commits reachable from all refs\"),\n \t\tOPT_BOOLEAN(0, \"stdin\", &transform_stdin, \"read from stdin\"),\n+\t\tOPT_BOOLEAN(0, \"undefined\", &allow_undefined, \"allow to print `undefined` names\"),\n \t\tOPT_END(),\n \t};\n \n@@ -226,7 +227,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)\n \t\t\t\telse if (++forty == 40 &&\n \t\t\t\t\t\t!ishex(*(p+1))) {\n \t\t\t\t\tunsigned char sha1[40];\n-\t\t\t\t\tconst char *name = \"undefined\";\n+\t\t\t\t\tconst char *name = NULL;\n \t\t\t\t\tchar c = *(p+1);\n \n \t\t\t\t\tforty = 0;\n@@ -240,11 +241,10 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)\n \t\t\t\t\t}\n \t\t\t\t\t*(p+1) = c;\n \n-\t\t\t\t\tif (!strcmp(name, \"undefined\"))\n+\t\t\t\t\tif (!name)\n \t\t\t\t\t\tcontinue;\n \n-\t\t\t\t\tfwrite(p_start, p - p_start + 1, 1,\n-\t\t\t\t\t       stdout);\n+\t\t\t\t\tfwrite(p_start, p - p_start + 1, 1, stdout);\n \t\t\t\t\tprintf(\" (%s)\", name);\n \t\t\t\t\tp_start = p + 1;\n \t\t\t\t}\n@@ -260,18 +260,32 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)\n \t\tmax = get_max_object_index();\n \t\tfor (i = 0; i < max; i++) {\n \t\t\tstruct object * obj = get_indexed_object(i);\n+\t\t\tconst char *name;\n \t\t\tif (!obj)\n \t\t\t\tcontinue;\n \t\t\tif (!data.name_only)\n \t\t\t\tprintf(\"%s \", sha1_to_hex(obj->sha1));\n-\t\t\tprintf(\"%s\\n\", get_rev_name(obj));\n+\t\t\tname = get_rev_name(obj);\n+\t\t\tif (name)\n+\t\t\t\tprintf(\"%s\\n\", name);\n+\t\t\telse if (allow_undefined)\n+\t\t\t\tprintf(\"undefined\\n\");\n+\t\t\telse\n+\t\t\t\tdie(\"cannot describe '%s'\", sha1_to_hex(obj->sha1));\n \t\t}\n \t} else {\n \t\tint i;\n \t\tfor (i = 0; i < revs.nr; i++) {\n+\t\t\tconst char *name;\n \t\t\tif (!data.name_only)\n \t\t\t\tprintf(\"%s \", revs.objects[i].name);\n-\t\t\tprintf(\"%s\\n\", get_rev_name(revs.objects[i].item));\n+\t\t\tname = get_rev_name(revs.objects[i].item);\n+\t\t\tif (name)\n+\t\t\t\tprintf(\"%s\\n\", name);\n+\t\t\telse if (allow_undefined)\n+\t\t\t\tprintf(\"undefined\\n\");\n+\t\t\telse\n+\t\t\t\tdie(\"cannot describe '%s'\", sha1_to_hex(revs.objects[i].item->sha1));\n \t\t}\n \t}\n \n-- \n1.5.4.rc1.1123.ge26a8\n"},{"id":"64077","messageId":"20071224155723.GA11269@artemis.madism.org","threadId":"11385","inReplyTo":"20071224111822.GA10356@artemis.madism.org","subject":"Re: [FIXED PATCH] git-describe: Add a --match option to limit considered tags.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-12-24T15:57:23Z","receivedAt":"2007-12-24T15:57:23Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"  Woops I borked the mail, the subject was supposed to be:\n\nSubject: [PATCH] Add a --(no-)undefined option to git-name-rev.\n\n-- \n·O·  Pierre Habouzit\n··O                                                madcoder@debian.org\nOOO                                                http://www.madism.org\n"}]}