[PATCH] Git: add pre-commit and post-rewrite hooks for versioning

Pete Batard pete at akeo.ie
Mon Apr 9 15:52:05 EDT 2012


The attached patch adds automated nano increment to libusb/version.h on 
git commits, using the current distance to origin + offset, with:
- [10000-19999] being reserved for libusbx master
- [0-9999] for old libusb (provided Peter ever uses the nano there)
- [20000-59999] for official (future) libusbx branches
- anything above 60000 for non-official private branches --should their 
user decide that they want to reuse our git hooks for their own versioning.

This nano versioning relies on the pre-commit and post-rewrite git hooks 
[1] to perform the automated increment. Ideally, we'd only use the 
pre-commit hook, but we need a special case for amend commits, as git 
does not provide any information as to whether a commit is regular or 
amend during it's pre-commit phase, whereas we need to correct the 
distance computation, which is off by 1, in case a commit is amended.

Amended commits therefore currently require the detection of a temporary 
.amend file in the repository, which, if one forgets to create it 
manually, will be automatically created for you post commit along with 
an explicit git notice prompting for a re-commit, to fix the version number.

Considering that, and I have to stress this out once again, these hooks 
are not meant to be used by anybody but official libusbx git 
maintainers, I don't really see the .amend issue as that much of a 
problem, especially as you would have to keep your eyes closed while 
committing to miss the notification to re-commit in case you forgot the 
.amend. And all you have to do if that is the case is just that: 
re-commit as you did with your last git invocation.
Of course, regular (non amend commits) do not need any special 
consideration.

I did investigate if it was possible to invoke git to recommit within a 
post commit hook but even if you avoid a loop, it doesn't seem 
achievable - if I have some time, I may try to ask the git guys for a 
pre-commit hook enhancement, that provides a parameter to the script as 
is the case for other hooks.

Finally, because these hooks require manual setup to actually be 
processed by git, anybody cloning the git repo will see their libusbx 
nano frozen to the last commit they pulled from master, which, if they 
didn't reuse our git hook versioning, should give us some good idea of 
what their branch origin was. And if they reused our hooks and acted on 
the *very* explicit warnings we have there, their nano will be above 
60000 which will let us know that they are using a private branch.

Regards,

/Pete

PS: if you wonder about that nano from -pbatard and the one from 
libusbx, that currently use the same branch offset, I don't really 
anticipate a conflict as the first nano for libusbx should be around 
465, whereas -pbatard (which doesn't use the git distance) is currently 
349, and -pbatard should come to an end before it reaches 400.

[1] http://schacon.github.com/git/githooks.html
-------------- next part --------------
>From cb8df8ca535a2ea9cd8e4ddfecf35f483a401769 Mon Sep 17 00:00:00 2001
From: Pete Batard <pete at akeo.ie>
Date: Mon, 9 Apr 2012 19:56:30 +0100
Subject: [PATCH] Git: add pre-commit and post-rewrite hooks for versioning

---
 _post-rewrite.sh |   28 ++++++++++++++++++++++++++++
 _pre-commit.sh   |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 0 deletions(-)
 create mode 100755 _post-rewrite.sh
 create mode 100755 _pre-commit.sh

diff --git a/_post-rewrite.sh b/_post-rewrite.sh
new file mode 100755
index 0000000..f5c7031
--- /dev/null
+++ b/_post-rewrite.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+#
+# Detect amended commits and warn user if .amend is missing
+#
+# To have git run this script on commit, create a "post-rewrite" text file in
+# .git/hooks/ with the following content:
+# #!/bin/sh
+# if [ -x ./_post-rewrite.sh ]; then
+#   source ./_post-rewrite.sh
+# fi
+#
+# NOTE: These versioning hooks are intended to be used *INTERNALLY* by the
+# libusbx development team and are NOT intended to solve versioning for any
+# derivative branch, such as one you would create for private development.
+#
+
+case "$1" in
+  amend)
+    # Check if a .amend exists. If none, create one and warn user to re-commit.
+    if [ -f ./.amend ]; then
+      rm ./.amend
+    else
+      echo "Amend commit detected, but no .amend file - One has now been created."
+      echo "Please re-commit as is (amend), so that the version number is correct."
+      touch ./.amend
+    fi ;;
+  *) ;;
+esac
diff --git a/_pre-commit.sh b/_pre-commit.sh
new file mode 100755
index 0000000..a3a813a
--- /dev/null
+++ b/_pre-commit.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+#
+# Sets the nano version according to the number of commits on this branch, as
+# well as the branch offset.
+#
+# To have git run this script on commit, first make sure you change
+# BRANCH_OFFSET to 60000 or higher, then create a "pre-commit" text file in
+# .git/hooks/ with the following content:
+# #!/bin/sh
+# if [ -x ./_pre-commit.sh ]; then
+#   source ./_pre-commit.sh
+# fi
+#
+# NOTE: These versioning hooks are intended to be used *INTERNALLY* by the
+# libusbx development team and are NOT intended to solve versioning for any
+# derivative branch, such as one you would create for private development.
+#
+# Should you wish to reuse these scripts for your own versioning, in your own
+# private branch, we kindly ask you to first set BRANCH_OFFSET to 60000, or
+# higher, as any offset below below 60000 is *RESERVED* for libusbx official
+# usage.
+
+################################################################################
+##  YOU *MUST* SET THE FOLLOWING TO 60000 OR HIGHER IF YOU REUSE THIS SCRIPT  ##
+################################################################################
+BRANCH_OFFSET=10000
+################################################################################
+
+type -P sed &>/dev/null || { echo "sed command not found. Aborting." >&2; exit 1; }
+type -P git &>/dev/null || { echo "git command not found. Aborting." >&2; exit 1; }
+
+NANO=`git log --oneline | wc -l`
+NANO=`expr $NANO + $BRANCH_OFFSET`
+# Amended commits need to have the nano corrected. Current versions of git hooks
+# only allow detection of amending post commit, so we require a .amend file,
+# which will be created post commit with a user warning if none exists when an
+# amend is detected.
+if [ -f ./.amend ]; then
+  NANO=`expr $NANO - 1`
+fi
+echo "setting nano to $NANO"
+# -i option of sed is useless on Windows. -b to prevent CRLF conversion.
+sed -b -e "s/^#define LIBUSB_NANO.*/#define LIBUSB_NANO $NANO/" libusb/version.h > libusb/version.h~
+mv libusb/version.h~ libusb/version.h
+git add libusb/version.h
-- 
1.7.9.msysgit.0



More information about the libusbx mailing list