summaryrefslogtreecommitdiffstats
path: root/git-buildmsg
diff options
context:
space:
mode:
authorHans Ulrich Niedermann <hun@n-dimensional.de>2007-11-10 17:06:12 +0100
committerHans Ulrich Niedermann <hun@n-dimensional.de>2007-11-10 17:06:12 +0100
commit2319e65dbffeb1c6654239a58a4e17dde0380a5f (patch)
tree6dded019ca936b2ea3f25336abb35cf03bbb30b9 /git-buildmsg
parentff93e51a4ae4f8fefeb7a49eaafc8ba4ff2c52e2 (diff)
downloadndim-git-utils-2319e65dbffeb1c6654239a58a4e17dde0380a5f.tar.gz
ndim-git-utils-2319e65dbffeb1c6654239a58a4e17dde0380a5f.tar.xz
ndim-git-utils-2319e65dbffeb1c6654239a58a4e17dde0380a5f.zip
Demonstrate "dist-of-git-commit" message
It is certainly possible to do that in a cleaner way with less code, but at least this works.
Diffstat (limited to 'git-buildmsg')
-rw-r--r--git-buildmsg/Makefile-files41
-rw-r--r--git-buildmsg/README.git-buildmsg8
-rw-r--r--git-buildmsg/git-buildmsg.in157
-rw-r--r--git-buildmsg/print-buildmsg.c11
4 files changed, 155 insertions, 62 deletions
diff --git a/git-buildmsg/Makefile-files b/git-buildmsg/Makefile-files
index ad5bcc5..e639d56 100644
--- a/git-buildmsg/Makefile-files
+++ b/git-buildmsg/Makefile-files
@@ -15,3 +15,44 @@ git-buildmsg/git-buildmsg-test.c: git-buildmsg/git-buildmsg
mkdir -p git-buildmsg
sh git-buildmsg/git-buildmsg --example --srcdir "$(top_srcdir)" \
--output git-buildmsg/git-buildmsg-test.c
+
+BUILT_SOURCES += git-buildmsg.stamp
+GIT_BUILDMSG_CMD = $(SHELL) "git-buildmsg/git-buildmsg" --error-if-no-git
+GIT_BUILDMSG_H = git-buildmsg.h
+CLEANFILES += $(GIT_BUILDMSG_H)
+git-buildmsg.stamp: git-buildmsg/git-buildmsg
+ if env GIT_DIR="$(top_srcdir)/.git" $(GIT_BUILDMSG_CMD) -s "$(top_srcdir)" -o "$(GIT_BUILDMSG_H)"; then \
+ if test -s "$(srcdir)/$(GIT_BUILDMSG_H)"; then \
+ if diff "$(srcdir)/$(GIT_BUILDMSG_H)" "$(GIT_BUILDMSG_H)"; then \
+ echo "New $(GIT_BUILDMSG_H) does not differ from $(srcdir)/$(GIT_BUILDMSG_H)"; \
+ exit 0; \
+ else \
+ echo "Error: $(srcdir)/$(GIT_BUILDMSG_H) and $(GIT_BUILDMSG_H) differ."; \
+ echo " You probably want to remove the former."; \
+ exit 1; \
+ fi; \
+ fi; \
+ else \
+ if test -s "$(srcdir)/$(GIT_BUILDMSG_H)"; then \
+ echo "Not a git worktree."; \
+ echo "Keep using the $(GIT_BUILDMSG_H) from $(top_srcdir)"; \
+ rm -f "$(GIT_BUILDMSG_H)"; \
+ exit 0; \
+ else \
+ echo "Warning: $(GIT_BUILDMSG_H) without git workdir."; \
+ exit 0; \
+ fi; \
+ fi
+
+TESTS += print-buildmsg
+bin_PROGRAMS += print-buildmsg
+check_PROGRAMS += print-buildmsg
+print_buildmsg_SOURCES = git-buildmsg/print-buildmsg.c
+print_buildmsg_SOURCES += $(GIT_BUILDMSG_H)
+
+dist-hook:
+ if test -e "$(GIT_BUILDMSG_H)"; then \
+ $(SED) -e 's|^#undef GIT_IS_DIST.*|#define GIT_IS_DIST 1|' \
+ "$(GIT_BUILDMSG_H)" > "$(GIT_BUILDMSG_H).new"; \
+ mv -f "$(GIT_BUILDMSG_H).new" "$(GIT_BUILDMSG_H)"; \
+ fi
diff --git a/git-buildmsg/README.git-buildmsg b/git-buildmsg/README.git-buildmsg
index c95f5e7..dc9a7cd 100644
--- a/git-buildmsg/README.git-buildmsg
+++ b/git-buildmsg/README.git-buildmsg
@@ -7,12 +7,12 @@ EXTRA_DIST += git-buildmsg
---[ END top-level Makefile.am ]---
---[ BEGIN Makefile.am where information is needed ]---
-BUILT_SOURCES += git-buildmsgstamp
+BUILT_SOURCES += git-buildmsg.stamp
GIT_BUILDMSG_CMD = $(SHELL) "$(top_srcdir)/git-buildmsg"
GIT_BUILDMSG_H = git-buildmsg.h
CLEANFILES += $(GIT_BUILDMSG_H)
-git_version.stamp:
- env GIT_DIR="$(top_srcdir)/.git" $(GIT_VERSION_CMD) -s "$(top_srcdir)" -o "$(GIT_BUILDMSG_H)"
+git-buildmsg.stamp:
+ env GIT_DIR="$(top_srcdir)/.git" $(GIT_BUILDMSG_CMD) -s "$(top_srcdir)" -o "$(GIT_BUILDMSG_H)"
@if test -s "$(srcdir)/$(GIT_BUILDMSG_H)"; then \
if cmp "$(srcdir)/$(GIT_BUILDMSG_H)" "$(GIT_BUILDMSG_H)"; then :; \
else \
@@ -24,7 +24,7 @@ git_version.stamp:
bin_PROGRAMS += foo
foo_SOURCES = foo.c
-ndist_foo_SOURCES = $(GIT_BUILDMSG_H)
+nodist_foo_SOURCES = $(GIT_BUILDMSG_H)
---[ END Makefile.am where information is needed ]---
diff --git a/git-buildmsg/git-buildmsg.in b/git-buildmsg/git-buildmsg.in
index 7cb53dd..1f7d5ea 100644
--- a/git-buildmsg/git-buildmsg.in
+++ b/git-buildmsg/git-buildmsg.in
@@ -48,11 +48,12 @@ working_dir="$(pwd)"
# Who am I?
self="$(basename "$0")"
-# Defaults
+# Default settings
ifndef_symbol="GIT_BUILDMSG_H"
outfile="-"
print_example=false
srcdir="$(pwd)"
+error_if_no_git=false
# Help messages
USAGE="[<option>...]"
@@ -62,7 +63,9 @@ LONG_USAGE="\
-x --example Print complete example program.
-o --output FILENAME Set output file name.
- -s --srcdir DIRNAME Set source tree dir name."
+ -s --srcdir DIRNAME Set source tree dir name.
+
+ -e --error-if-no-git Abort with exitcode!=0 if git worktree not found."
# Parse command line parameter, affecting defaults
while [ "x$1" != "x" ]
@@ -74,11 +77,8 @@ do
-o|--output)
if shift; then
outfile="$1"
- if [ "x$outfile" = "x-" ]; then
- : # keep default ifndef_symbol
- else
+ [ "x$outfile" = "x-" ] || \
ifndef_symbol=`basename "$outfile" | $SED 's|\.|_|g; s|[^A-Za-z0-9_]||g' | tr a-z A-Z`
- fi
else
echo "$self: Fatal: \"$1\" option requires parameter." >&2
exit 1
@@ -90,25 +90,24 @@ do
;;
-h|--help)
echo "Usage: ${self} $USAGE"
- if test "x$LONG_USAGE" = "x"; then :;
- else
- echo "$LONG_USAGE"
- fi
+ [ -n "$LONG_USAGE" ] && echo "$LONG_USAGE"
exit
;;
-s|--srcdir)
if shift; then
- if test -d "$1"; then
- srcdir="$1"
- else
- echo "$self: Fatal: \"$1\" not a directory."
+ srcdir="$1"
+ [ -d "$srcdir" ] || {
+ echo "$self: Fatal: \"$1\" not a directory." >&2
exit 1
- fi
+ }
else
echo "$self: Fatal: \"$1\" option requires directory parameter."
exit 1
fi
;;
+ -e|--error-if-no-git)
+ error_if_no_git=:
+ ;;
*)
echo "$self: Fatal: Invalid command line paramenter: \"$1\"" >&2
exit 1
@@ -118,29 +117,21 @@ do
done
# If not printing to stdout, redirect stdout to output file
+outfile_exists=false
rename_new_output=false
if [ "x$outfile" = "x-" ]
then
: # keep using stdout
else
+ if [ -e "$outfile" ]; then
+ outfile_exists=:
+ fi
exec 1> "${outfile}.new"
fi
# Done with creating output files, so we can change to source dir
cd "$srcdir"
-# Write program header
-cat<<EOF
-/*
- * Basic versioning gathered from the git repository.
- * Automatically generated by ${self}.
- */
-
-#ifndef ${ifndef_symbol}
-#define ${ifndef_symbol} 1
-
-EOF
-
# Detect git tools (should work with old and new git versions)
git_found=yes
for git_tool in git-symbolic-ref git-rev-parse git-diff-files git-diff-index
@@ -148,41 +139,78 @@ do
[ x`which $git_tool 2>/dev/null` = "x" ] && { git_found=no; break; }
done
-# Determine and write git specific defines
+# Determine data about git tree
+is_git_worktree=no
if [ "x$git_found" = "xyes" ]; then
if [ -e "$GIT_DIR/index" ]; then
- echo "/* This is a git repository */"
- echo "#define GIT_USED 1"
- echo ""
+ is_git_worktree="yes"
- # Commit SHA-ID
- git_shaid=`git-rev-parse HEAD | $SED -n 's/^\(.\{8\}\).*/\1/p'`
- echo "/* Git SHA ID of last commit */"
- echo "#define GIT_SHAID \"${git_shaid}..\""
- echo ""
+ # Commit ID
+ git_commitid=`git-rev-parse HEAD | $SED -n 's/^\(.\{8\}\).*/\1/p'` || \
+ { echo "Error running git-rev-parse HEAD" >&2; exit 1; }
# Branch
- git_branch=`git-symbolic-ref HEAD | $SED -n 's|^refs/heads/||p'`
- echo "/* Branch this tree is on */"
- echo "#define GIT_BRANCH \"$git_branch\""
- echo ""
+ git_branch=`git-symbolic-ref HEAD | $SED -n 's|^refs/heads/||p'` || \
+ { echo "Error running git-symbolic-ref HEAD" >&2; exit 1; }
# Any uncommitted changes we should know about?
# Or technically: Are the working tree or index dirty?
+ git_dirty_tree=yes
if git-diff-files --quiet && git-diff-index --cached --quiet HEAD; then
- echo "/* SHA-ID uniquely defines the state of this code */"
- echo "#undef GIT_UNCOMMITTED"
- else
- echo "/* Local changes might be breaking things */"
- echo "#define GIT_UNCOMMITTED 1"
+ git_dirty_tree=no
fi
- else
- echo "/* This is not a git repository */"
- echo "#undef GIT_USED"
fi
-else
+fi
+
+# Write program header
+cat<<EOF
+/*
+ * Basic versioning gathered from the git repository.
+ * Automatically generated by ${self}.
+ */
+
+#ifndef ${ifndef_symbol}
+#define ${ifndef_symbol} 1
+
+/* whether this header file is from a dist tarball */
+#undef GIT_IS_DIST
+
+EOF
+
+# Write git specific defines
+if [ "x$git_found" = "xno" ]; then
echo "/* git is not installed */"
- echo "#undef GIT_USED"
+ echo "#undef GIT_IS_GIT_WORKTREE"
+elif [ "x$is_git_worktree" = "xno" ]; then
+ echo "/* This is not a git repository */"
+ echo "#undef GIT_IS_GIT_WORKTREE"
+else
+ echo "/* This is a git repository */"
+ echo "#define GIT_IS_GIT_WORKTREE 1"
+ echo ""
+ if [ "x$is_git_worktree" = "xyes" ]; then
+ echo "/* Git ID of last commit */"
+ echo "#define GIT_COMMIT_ID \"${git_commitid}..\""
+ echo ""
+
+ echo "/* Branch this tree is on */"
+ echo "#define GIT_BRANCH \"$git_branch\""
+ echo ""
+
+ if [ "x$git_dirty_tree" = "xyes" ]; then
+ echo "/* Commit ID uniquely defines the state of this code */"
+ echo "#undef GIT_DIRTY_WORKTREE"
+ else
+ echo "/* Local changes might be breaking things */"
+ echo "#define GIT_DIRTY_WORKTREE 1"
+ fi
+ else
+ echo "/* Git ID of last commit */"
+ echo "#undef GIT_COMMIT_ID"
+ echo ""
+ echo "/* Branch this tree is on */"
+ echo "#undef GIT_BRANCH"
+ fi
fi
# Define a few immediately useful message strings
@@ -193,21 +221,28 @@ cat<<EOF
* forms a proper sentence.
*/
-#ifdef GIT_USED
+#ifdef GIT_IS_DIST
+# define GIT_DIST_MSG "dist of "
+#else
+# define GIT_DIST_MSG ""
+#endif
+
+#ifdef GIT_IS_GIT_WORKTREE
-# ifdef GIT_UNCOMMITTED
-# define GIT_WITH_WITHOUT_UNCOMMITTED " + changes"
+# ifdef GIT_DIRTY_WORKTREE
+# define GIT_CHANGE_MSG " + changes"
# else
-# define GIT_WITH_WITHOUT_UNCOMMITTED ""
-# endif /* GIT_UNCOMMITTED */
+# define GIT_CHANGE_MSG ""
+# endif /* GIT_DIRTY_WORKTREE */
# define GIT_MESSAGE \\
+ GIT_DIST_MSG \\
"git branch " GIT_BRANCH ", " \\
- "commit " GIT_SHAID GIT_WITH_WITHOUT_UNCOMMITTED
+ "commit " GIT_COMMIT_ID GIT_CHANGE_MSG
#else
-# define GIT_MESSAGE "non-git sources"
-#endif /* GIT_USED */
+# define GIT_MESSAGE GIT_DIST_MSG "non-git sources"
+#endif /* GIT_IS_GIT_WORKTREE */
#endif /* ${ifndef_symbol} */
EOF
@@ -250,8 +285,14 @@ then
mv -f "$outfile.new" "$outfile"
fi
else
+ echo "$self: Output is new file, creating $outfile" >&2
mv -f "$outfile.new" "$outfile"
fi
fi
+if "$error_if_no_git" && [ "x$is_git_worktree" = "xno" ]; then
+ echo "$self: No git worktree found. Aborting with error, as requested." >&2
+ exit 2
+fi
+
# THE END.
diff --git a/git-buildmsg/print-buildmsg.c b/git-buildmsg/print-buildmsg.c
new file mode 100644
index 0000000..c5f4e84
--- /dev/null
+++ b/git-buildmsg/print-buildmsg.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <string.h>
+#include "git-buildmsg.h"
+
+int main(int argc, char *argv[])
+{
+ const char *const lastslash = strrchr(argv[0], '/');
+ const char *const self = (lastslash)?(lastslash+1):(argv[0]);
+ printf("%s: %s\n", self, GIT_MESSAGE);
+ return 0;
+}