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

7 messages from 2007-05-21 to 2007-05-25. Participants: Sam Vilain, Martin Waitz, Jan Hudec.
Thread: https://gitlist.dev/t/8260

## Sam Vilain, 2007-05-21 02:52

Subject: [PATCH] Make sure an autogenerated version has at least four parts
Message-ID: <11797159411969-git-send-email-sam.vilain@catalyst.net.nz>
URL: https://gitlist.dev/e/11797159411969-git-send-email-sam.vilain%40catalyst.net.nz

```
Otherwise, a custom "v1.5.2.42.gd00b" is considered newer than a
"v1.5.2.1.69.gcafe".

Warning: contains awk.

Signed-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>
---
 GIT-VERSION-GEN |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 06c360b..5eb58c3 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -18,7 +18,7 @@ elif test -d .git &&
 	v[0-9]*) : happy ;;
 	esac
 then
-	VN=$(echo "$VN" | sed -e 's/-/./g');
+	VN=$(echo "$VN" | awk -F- 'X=$1 { Y=$2; Z=$3; while (!(X ~ /\..*\..*\./)) { X = X ".0" } print X ( Y ? "." Y : "" ) ( Z ? "." Z : "" ) }');
 else
 	VN="$DEF_VER"
 fi
-- 
1.5.1.1.175.g31e4

```

## Martin Waitz, 2007-05-21 07:36

Subject: Re: [PATCH] Make sure an autogenerated version has at least four parts
Message-ID: <20070521073650.GV5412@admingilde.org>
URL: https://gitlist.dev/e/20070521073650.GV5412%40admingilde.org
In-Reply-To: <11797159411969-git-send-email-sam.vilain@catalyst.net.nz>

```
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 "."?

-- 
Martin Waitz

```

## Jan Hudec, 2007-05-21 19:57

Subject: Re: [PATCH] Make sure an autogenerated version has at least four parts
Message-ID: <20070521195745.GD5082@efreet.light.src>
URL: https://gitlist.dev/e/20070521195745.GD5082%40efreet.light.src
In-Reply-To: <20070521073650.GV5412@admingilde.org>

```
On Mon, May 21, 2007 at 09:36:50 +0200, Martin Waitz wrote:
> 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 "."?

That depends on what rules you use to compare versions. Under dpkg ones it
would. I don't know what eg. rpm uses. (Note: - is BEFORE . in asciibet).

-- 
						 Jan 'Bulb' Hudec <bulb@ucw.cz>

```

## Martin Waitz, 2007-05-21 20:06

Subject: Re: [PATCH] Make sure an autogenerated version has at least four parts
Message-ID: <20070521200635.GB5412@admingilde.org>
URL: https://gitlist.dev/e/20070521200635.GB5412%40admingilde.org
In-Reply-To: <20070521195745.GD5082@efreet.light.src>

```
hoi :)

On Mon, May 21, 2007 at 09:57:45PM +0200, Jan Hudec wrote:
> That depends on what rules you use to compare versions. Under dpkg ones it
> would. I don't know what eg. rpm uses. (Note: - is BEFORE . in asciibet).

if rpm does not grok that then perhaps the version transformation should
be moved to the git.spec Makefile target.
That way at least  the real git version is in line with git-describe.

-- 
Martin Waitz

```

## Sam Vilain, 2007-05-25 01:33

Subject: Re: [PATCH] Make sure an autogenerated version has at least four parts
Message-ID: <46563CE1.9020007@vilain.net>
URL: https://gitlist.dev/e/46563CE1.9020007%40vilain.net
In-Reply-To: <20070521073650.GV5412@admingilde.org>

```
Martin Waitz wrote:
> 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

```

## Sam Vilain, 2007-05-25 01:42

Subject: Re: [PATCH] Make sure an autogenerated version has at least four parts
Message-ID: <46563F21.2060107@vilain.net>
URL: https://gitlist.dev/e/46563F21.2060107%40vilain.net
In-Reply-To: <46563CE1.9020007@vilain.net>

```
I wrote:
> 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

...and here's the corresponding GIT-VERSION-GEN patch:

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

Otherwise, a custom "v1.5.2.42.gd00b" is considered newer than a
"v1.5.2.1.69.gcafe".

Contains a workaround for the chicken-and-egg problem it would
otherwise introduce.

Signed-off-by: Sam Vilain <sam@vilain.net>
---
 GIT-VERSION-GEN |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 06c360b..91e8966 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -12,7 +12,8 @@ if test -f version
 then
 	VN=$(cat version) || VN="$DEF_VER"
 elif test -d .git &&
-	VN=$(git describe --abbrev=4 HEAD 2>/dev/null) &&
+	VN=$(git describe --abbrev=4 --levels=4 HEAD 2>/dev/null ||
+		git describe --abbrev=4 HEAD 2>/dev/null) &&
 	case "$VN" in
 	*$LF*) (exit 1) ;;
 	v[0-9]*) : happy ;;
-- 
1.5.2.0.45.gfea6d-dirty

```

## Martin Waitz, 2007-05-25 09:47

Subject: Re: [PATCH] Make sure an autogenerated version has at least four parts
Message-ID: <20070525094743.GO5412@admingilde.org>
URL: https://gitlist.dev/e/20070525094743.GO5412%40admingilde.org
In-Reply-To: <46563CE1.9020007@vilain.net>

```
hoi :)

On Fri, May 25, 2007 at 01:33:21PM +1200, Sam Vilain wrote:
> Martin Waitz wrote:
> > 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.

But you can also have "-" in the upstream version without problems.
Only the last "-" is used as deliminator and therefore the Debian
version part must not contain a "-"; it's no problem for the upstream
part.


> The change I posted keeps original behaviour - just fills out the
> .0's.
> Perhaps the munging should go in git-describe instead?

If we want to be able to separate the software version as it is defined
by the project maintainer from the additional Git information we should
do so with a clear separator, not by changing the software version.

Maybe have some Makefile magic which creates a nice version,
but interpreting and changing the tagged version in git-describe sounds
wrong to me.

-- 
Martin Waitz

```
