Rework ChangeLog generation to fix make distcheck
diff --git a/Makefile.am b/Makefile.am
index b9b9188..55bdaa6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -106,7 +106,23 @@
 
 
 
-dist-hook: ChangeLog
+distclean-local:
+	if test "$(srcdir)" = "."; then :; else \
+		rm -f ChangeLog; \
+	fi
 
-ChangeLog : .git/HEAD
-	$(AM_V_GEN) git log --stat > ChangeLog
+ChangeLog:
+	$(AM_V_GEN) if test -d "$(srcdir)/.git"; then \
+	  (GIT_DIR=$(top_srcdir)/.git ./missing --run git log --stat) | fmt --split-only > $@.tmp \
+	  && mv -f $@.tmp $@ \
+	  || ($(RM) $@.tmp; \
+	      echo Failed to generate ChangeLog, your ChangeLog may be outdated >&2; \
+	      (test -f $@ || echo git log is required to generate this file >> $@)); \
+	else \
+	  test -f $@ || \
+	  (echo A git checkout and git log is required to generate ChangeLog >&2 && \
+	  echo A git checkout and git log is required to generate this file >> $@); \
+	fi
+
+.PHONY: ChangeLog
+