From 209ef6909e8aa0ab075f85fd3062c2ee34ff4d4d Mon Sep 17 00:00:00 2001 From: "R. Steve McKown" Date: Sun, 23 Sep 2012 19:26:45 -0600 Subject: [PATCH] Add version option, derived from repository tags --- .gitignore | 1 + Makefile | 24 ++++++++++++++++++++++- mkversion.mk | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ repo_shell.c | 12 +++++++++--- version.c.in | 1 + version.h | 6 ++++++ 6 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 mkversion.mk create mode 100644 version.c.in create mode 100644 version.h diff --git a/.gitignore b/.gitignore index baba52a..f4255f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ repo_shell +version.c *.swp .svn diff --git a/Makefile b/Makefile index b2e6998..dc3b5f8 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,24 @@ -repo_shell: repo_shell.c inih/ini.c +PROGRAM := repo_shell +# Git derives its version "prefix" from tags + +# Add TMI's mkversion to render files xxx.in -> xxx, updating __appVersion__ +# Massage the version coming back because setup.py build to exe only wants +# versions of the form a.b.c.d. We'll use this to our advantage to cause +# setup.py to error if the VERSION is either dirty, or if git, not a tag. +include mkversion.mk +#$(warning VERSION $(VERSION)) +INFILES := $(shell ls *.in 2>/dev/null) +INFILES := $(INFILES:%.in=%) +#$(warning INFILES $(INFILES)) + +repo_shell: repo_shell.c inih/ini.c version.c $(CC) -I inih $^ -o $@ + +$(INFILES):%: %.in + @sed -e 's|__appVersion__|$(VERSION)|g' < $^ > $@-tmp + @diff -q $@-tmp $@ >/dev/null 2>&1 && rm -f $@-tmp || mv $@-tmp $@ + +clean: + @rm -rf $(PROGRAM) $(INFILES) + +.PHONY: all clean $(INFILES) diff --git a/mkversion.mk b/mkversion.mk new file mode 100644 index 0000000..0a4f124 --- /dev/null +++ b/mkversion.mk @@ -0,0 +1,55 @@ +# mkversion.mk +# +# Determine the repository checkout id. Assume that there are no local +# changes in the workspace. Support svn, git svn, and git. +# +# Usage example: +# +# VERSION_FILE := "version.h" +# $(VERSION_FILE):%.h: %.in +# sed -e 's|__appVersion__|$(VERSION)|g' < $^ > $@ +# +# Contents of version.h: +# +# #ifndef VERSION_H +# #define VERSION_H +# +# #define VERSION "__appVersion__" +# +# #endif + +ifeq (.svn,$(shell ls -d .svn 2>/dev/null)) + VERSION := $(shell svnversion . 2>/dev/null | sed -e 's|M$$|-dirty|') + ifneq (,$(VERSION)) + VERSION := r$(VERSION) + endif + VERFROM := svn +endif +ifeq (.git,$(shell ls -d .git)) + VERSION := $(shell git svn info 2>/dev/null | grep "Revision: " | \ + sed -e s'/^[^0-9]*\([0-9]*\).*$$/r\1/') + ifneq (,$(VERSION)) + # git-svn + ifeq (,$(shell git log -n1 2>/dev/null | grep "git-svn-id: ")) + VERSION := $(VERSION)-dirty + else + ifneq (,$(shell git diff-index --name-only HEAD 2>/dev/null)) + VERSION := $(VERSION)-dirty + endif + endif + VERFROM := svn + else + # git + VERSION := $(shell git describe --tags --always 2>/dev/null) + ifneq (,$(VERSION)) + ifneq (,$(shell git diff-index --name-only HEAD 2>/dev/null)) + VERSION := $(VERSION)-dirty + endif + endif + VERFROM := git + endif +endif +ifeq (,$(VERSION)) + VERSION := unknown +endif +#$(warning VERSION $(VERSION)) diff --git a/repo_shell.c b/repo_shell.c index e11bf54..b51c0ad 100644 --- a/repo_shell.c +++ b/repo_shell.c @@ -8,6 +8,7 @@ #include #include #include "ini.h" +#include "version.h" #define CFG_FILE "/etc/repo_shell.cfg" #define GIT_ACL_FILE "git_acl.cfg" @@ -164,8 +165,8 @@ static int git_acl(const char *user, const char *repo) len = strlen(pw->pw_dir) + strlen(GIT_ACL_FILE) + 2; file = xmalloc(sizeof(char) * len); sprintf(file, "%s/%s", pw->pw_dir, GIT_ACL_FILE); - fprintf(stderr, "[someday check %s]\n", file); - free(file); + fprintf(stderr, "[someday check %s]\n", file); + free(file); return 2; /* assume read/write for now */ } @@ -303,7 +304,7 @@ static struct commands { }; static int handler(void* user, const char* section, const char* name, - const char* value) + const char* value) { cfg_t* pconfig = (cfg_t*)user; @@ -339,6 +340,11 @@ int main(int argc, char **argv) die("opening /dev/null failed"); close (devnull_fd); + if (argc == 2 && (!strcmp(argv[1], "-v") || + !strcmp(argv[1], "--version"))) { + fprintf(stderr, "%s\n", version); + return 0; + } if (argc < 3) die("invalid arguments"); diff --git a/version.c.in b/version.c.in new file mode 100644 index 0000000..c38856b --- /dev/null +++ b/version.c.in @@ -0,0 +1 @@ +const char version[] = "__appVersion__"; diff --git a/version.h b/version.h new file mode 100644 index 0000000..f22f7cf --- /dev/null +++ b/version.h @@ -0,0 +1,6 @@ +#ifndef VERSION_H +#define VERSION_H + +extern const char version[]; + +#endif /* end of include guard: VERSION_H */ -- 2.39.2