git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Make sure an autogenerated version has at least four parts

From
Sam Vilain <sam@vilain.net>
Date
May 25, 2007, 01:33 UTC
Message-ID
<46563CE1.9020007@vilain.net>
In-Reply-To
<20070521073650.GV5412@admingilde.org>
Martin Waitz wrote:
Show 8 quoted lines
> hoi :)
> 
> On Mon, May 21, 2007 at 02:52:21PM +1200, Sam Vilain wrote:
>> Otherwise, a custom "v1.5.2.42.gd00b" is considered newer than a
>> "v1.5.2.1.69.gcafe".
> 
> or just use git describe output without replacing "-" with "."?
> 

dpkg uses "-" in version numbers for its own uses - to delimit the packager's packaging version from the software version. The change I posted keeps original behaviour - just fills out the .0's.

Perhaps the munging should go in git-describe instead?
Subject: [PATCH] describe: add --levels option

Some projects might want git describe to always give a result that has a given number of version levels. ie, if you say --levels=4 and describe finds a name like 'v1.5.2', the result will be 'v1.5.2.0'.

This does mean that on exact tag matches, the returned version is not a resolvable ref - but that is probably caveat emptor.

Signed-off-by: Sam Vilain <sam@vilain.net>
---
 builtin-describe.c  |   50 ++++++++++++++++++++++++++++++++++++++++++++++----
 t/t6120-describe.sh |    8 ++++++++
 2 files changed, 54 insertions(+), 4 deletions(-)
diff --git a/builtin-describe.c b/builtin-describe.c
index 165917e..05eabc5 100644
--- a/builtin-describe.c
+++ b/builtin-describe.c
@@ -15,6 +15,7 @@ static int all;	/* Default to annotated tags only */
 static int tags;	/* But allow any tags if --tags is specified */
 static int abbrev = DEFAULT_ABBREV;
 static int max_candidates = 10;
+static int num_levels = 0;
 
 struct commit_name {
 	int prio; /* annotated tag = 2, tag = 1, head = 0 */
@@ -134,6 +135,7 @@ static void describe(const char *arg, int last_one)
 	struct possible_tag all_matches[MAX_TAGS];
 	unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
 	unsigned long seen_commits = 0;
+	char* chosen;
 
 	if (get_sha1(arg, sha1))
 		die("Not a valid object name %s", arg);
@@ -148,8 +150,9 @@ static void describe(const char *arg, int last_one)
 
 	n = cmit->util;
 	if (n) {
-		printf("%s\n", n->path);
-		return;
+		chosen = n->path;
+		abbrev = 0;
+		goto show;
 	}
 
 	if (debug)
@@ -228,10 +231,44 @@ static void describe(const char *arg, int last_one)
 				sha1_to_hex(gave_up_on->object.sha1));
 		}
 	}
+
+	chosen = all_matches[0].name->path;
+
+	/* make the described version have the desired number of
+	 * levels in it */
+ show:
+	if (num_levels) {
+		int found = 1;
+		char* idx = chosen;
+		int i;
+		while ((idx = index(idx, '.'))) {
+			found++;
+			idx++;
+		}
+		if (found > num_levels) {
+			idx = chosen;
+			for (i = 0; i < num_levels; i++) {
+				if (i)
+					idx++;
+				idx = index(idx, '.');
+			}
+			*idx = '\0';
+		}
+		else if (found < num_levels) {
+			int extra = 2 * (num_levels - found);
+			char* new = xmalloc(strlen(chosen) + extra + 1);
+			chosen = strcpy(new, chosen);
+			while (found < num_levels) {
+				strcat(chosen, ".0");
+				found++;
+			}
+		}
+	}
+
 	if (abbrev == 0)
-		printf("%s\n", all_matches[0].name->path );
+		printf("%s\n", chosen);
 	else
-		printf("%s-%d-g%s\n", all_matches[0].name->path,
+		printf("%s-%d-g%s\n", chosen,
 		       all_matches[0].depth,
 		       find_unique_abbrev(cmit->object.sha1, abbrev));
 
@@ -266,6 +303,11 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 			else if (max_candidates > MAX_TAGS)
 				max_candidates = MAX_TAGS;
 		}
+		else if (!prefixcmp(arg, "--levels=")) {
+			num_levels = strtoul(arg + 9, NULL, 10);
+			if (num_levels < 0) 
+				num_levels = 0;
+		}
 		else
 			usage(describe_usage);
 	}
diff --git a/t/t6120-describe.sh b/t/t6120-describe.sh
index 3e9edda..0336ddd 100755
--- a/t/t6120-describe.sh
+++ b/t/t6120-describe.sh
@@ -33,6 +33,7 @@ test_expect_success setup '
 
 	test_tick &&
 	echo two >file && git-add file && git-commit -m second &&
+	git-tag -a -m v1.1 v1.1 &&
 	two=$(git-rev-parse HEAD) &&
 
 	test_tick &&
@@ -94,4 +95,11 @@ check_describe D-* --tags HEAD^^
 check_describe A-* --tags HEAD^^2
 check_describe B --tags HEAD^^2^
 
+check_describe A.0-* --tags --levels=2 HEAD
+check_describe A.0.0-* --tags --levels=3 HEAD
+check_describe v1 --tags --levels=1 v1.1
+check_describe v1.1 --tags --levels=2 v1.1
+check_describe v1.1.0 --tags --levels=3 v1.1
+check_describe v1-* --tags --levels=1 A^1
+
 test_done
-- 
1.5.2.0.45.gfea6d-dirty
Previous: Martin WaitzNext: Sam Vilain
Message 5 of 7 in “Make sure an autogenerated version has at least four parts”
  1. Make sure an autogenerated version has at least four partsSam Vilain, May 21, 2007
  2. Martin WaitzMay 21, 2007
  3. Jan HudecMay 21, 2007
  4. Martin WaitzMay 21, 2007
  5. Sam VilainMay 25, 2007
  6. Sam VilainMay 25, 2007
  7. Martin WaitzMay 25, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.