]> git.refcnt.org Git - colorize.git/commitdiff
Include commit hash in version output
authorSteven Schubiger <stsc@refcnt.org>
Thu, 26 Jun 2014 14:08:37 +0000 (16:08 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Thu, 26 Jun 2014 14:08:37 +0000 (16:08 +0200)
Extracting the abbreviated commit hash will break when git will be
replaced by a different version control system.  But until it does,
get the hash from the git repository.

Makefile
colorize.c
version.pl [new file with mode: 0755]

index ca0ee68c0a5619faf4fa973677cb975b19b37fa0..d061794949a381d71485b18fca6146515a879201 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,10 +8,11 @@ CC=gcc
 CFLAGS=-ansi -pedantic
 
 colorize:      colorize.c
-                       $(CC) $(CFLAGS) -o colorize colorize.c -DCFLAGS="$(CFLAGS)"
+                       perl ./version.pl > version.h
+                       $(CC) $(CFLAGS) -o colorize colorize.c -DCFLAGS="$(CFLAGS)" -DHAVE_VERSION
 
 check:
                        perl ./test.pl
 
 clean:
-                       [ -e colorize ] && rm colorize; exit 0
+                       rm -f colorize version.h
index 54f6889fe3c308a74550bc38b335b3d0853a408a..ea18425296267f085d84d605fcb0d8080c3c39d1 100644 (file)
@@ -403,6 +403,11 @@ print_help (void)
 static void
 print_version (void)
 {
+#ifdef HAVE_VERSION
+# include "version.h"
+#else
+    const char *version = NULL;
+#endif
     const char *c_flags;
     struct bytes_size bytes_size;
     bool debug;
@@ -416,7 +421,10 @@ print_version (void)
 #else
     debug = false;
 #endif
-    printf ("%s v%s (compiled at %s, %s)\n", "colorize", VERSION, __DATE__, __TIME__);
+    if (version)
+      printf ("colorize %s (compiled at %s, %s)\n", version, __DATE__, __TIME__);
+    else
+      printf ("colorize v%s (compiled at %s, %s)\n", VERSION, __DATE__, __TIME__);
     printf ("Compiler flags: %s\n", c_flags);
     if (get_bytes_size (BUF_SIZE, &bytes_size))
       {
diff --git a/version.pl b/version.pl
new file mode 100755 (executable)
index 0000000..850902e
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my $version = '';
+
+# git repository
+if (system('which git >/dev/null 2>&1') == 0
+and system('git ls-files colorize.c --error-unmatch >/dev/null 2>&1') == 0) {
+    $version = `git describe --tags --dirty`;
+    $version =~ s/\n$//g;
+}
+
+if (length $version) {
+    print <<"EOT";
+const char *version = "$version";
+EOT
+}
+else {
+    print <<'EOT';
+const char *version = NULL;
+EOT
+}