Skip to contentSkip to conversation

Make sure an autogenerated version has at least four parts

PATCH In review Sam Vilain 4 participants 7 messages Jun 15, 2016

lore
Share & tools
lore

Sam Vilain

author
#0 lore raw

PATCH 1/1 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".

Warning: contains awk.

---
 GIT-VERSION-GEN |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
GIT-VERSION-GEN +1 -1
index 06c360b..5eb58c3 100755
@@ -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
Signature · 1 line
1.5.1.1.175.g31e4
#1 lore raw

hoi :)

Quoted 2 lines · Sam Vilain

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

Signature · 1 line
Martin Waitz
#2 lore raw
Quoted 7 lines · Martin Waitz

hoi :)

Quoted 2 lines · Sam Vilain

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).

Signature · 1 line
						 Jan 'Bulb' Hudec <[email protected]>
#3 lore raw

hoi :)

Quoted 2 lines · Jan Hudec

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.

Signature · 1 line
Martin Waitz
#4 lore raw
In reply to Martin Waitz
Quoted 8 lines · Martin Waitz

hoi :)

Quoted 2 lines · Sam Vilain

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.

---
 builtin-describe.c  |   50 ++++++++++++++++++++++++++++++++++++++++++++++----
 t/t6120-describe.sh |    8 ++++++++
 2 files changed, 54 insertions(+), 4 deletions(-)
builtin-describe.c +46 -4
index 165917e..05eabc5 100644
@@ -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);
 	}
t/t6120-describe.sh +8 -0
index 3e9edda..0336ddd 100755
@@ -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
Signature · 1 line
1.5.2.0.45.gfea6d-dirty
#5 lore raw

hoi :)

Quoted 9 lines · Sam Vilain
Quoted 5 lines · Martin Waitz
Quoted 2 lines · Sam Vilain

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.

Quoted 3 lines

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.

Signature · 1 line
Martin Waitz
#6 lore raw
In reply to Sam Vilain
Quoted 7 lines · 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.

---
 GIT-VERSION-GEN |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
GIT-VERSION-GEN +2 -1
index 06c360b..91e8966 100755
@@ -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 ;;
Signature · 1 line
1.5.2.0.45.gfea6d-dirty