Subject: Re: [GUILT] add FreeBSD support

From: Zheng Liu <[email-masked]>
Date: Tue, 20 Aug 2013 15:44:16 +0800

Hi Josef,

On Fri, Aug 09, 2013 at 11:20:46AM -0400, Josef 'Jeff' Sipek wrote:
> On Fri, Aug 09, 2013 at 11:04:45PM +0800, gnehzuil.liu wrote:
> > ?? 2013-8-9??????10:46??Josef 'Jeff' Sipek <[email-masked]> д????
> > 
> > > On Fri, Aug 09, 2013 at 08:32:28PM +0800, Zheng Liu wrote:
> > >> From: Zheng Liu <[email-masked]>
> > >> 
> > >> Currently guilt doesn't support FreeBSD platform.  This commit tries to
> > >> add this support.  The file called 'os.FreeBSD' is copied from os.Darwin
> > >> due to these two platforms have almost the same command tools.
> > > 
> > > Out of curiosity, is it identical?  I eyeballed it, and they do look
> > > identical.  There's probably a better way to do this whole os-specific
> > > thing, but this will work well enough for now.
> > 
> > Yes, it is identical.  Sorry, I am a newbie for guilt, but I am happy to
> > improve this os-specific thing.    Any idea?
> 
> So, I'm a bit torn between some "build-time" checking that generates
> something like an "os" file based on what it detects and something that
> happens at runtime.  I like that currently there's nothing to do - you just
> clone the repo and you're set.  At the same time, the more code can be
> avoided executing the faster (in theory) guilt gets.

Sorry for the late reply.  I did a simple experiment that tries to fold
all os.* files into one file and uses a if statement to export functions
according to different platforms.  But frankly I don't like this because
it is not very clearly.  So IMHO we'd better add a 'os.FreeBSD' file to
support FreeBSD platform.

I attach the patch in this mail.  It is not very mature.  If you think
it is worthwhile improving this patch.  Please review it.  All feedbacks
are always welcome.

Regards,
                                                - Zheng

---
 Makefile            |   2 +-
 guilt               |   8 ++--
 os                  | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 os.Darwin           |  70 ---------------------------
 os.Linux            |  57 ----------------------
 os.SunOS            |  57 ----------------------
 regression/scaffold |   4 +-
 7 files changed, 141 insertions(+), 191 deletions(-)
 create mode 100644 os
 delete mode 100644 os.Darwin
 delete mode 100644 os.Linux
 delete mode 100644 os.SunOS

diff --git a/Makefile b/Makefile
index b38c1e4..395abc1 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ PREFIX?=/usr/local
 DESTDIR?=
 INSTALL?=install
 
-OSFILES = $(filter-out $(wildcard *~),$(wildcard os.*))
+OSFILES = os
 SCRIPTS = $(filter-out $(wildcard *~),$(wildcard guilt-*))
 
 .PHONY: all 
diff --git a/guilt b/guilt
index e9b2aab..718bed0 100755
--- a/guilt
+++ b/guilt
@@ -906,10 +906,10 @@ pager="more"
 
 UNAME_S=`uname -s`
 
-if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
-	. "$GUILT_PATH/os.$UNAME_S"
-elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
-	. "$GUILT_PATH/../lib/guilt/os.$UNAME_S"
+if [ -r "$GUILT_PATH/os" ]; then
+	. "$GUILT_PATH/os"
+elif [ -r "$GUILT_PATH/../lib/guilt/os" ]; then
+	. "$GUILT_PATH/../lib/guilt/os"
 else
 	die "Unsupported operating system: $UNAME_S"
 fi
diff --git a/os b/os
new file mode 100644
index 0000000..6d1bc01
--- /dev/null
+++ b/os
@@ -0,0 +1,134 @@
+UNAME_S=`uname -s`
+
+if [ $UNAME_S == 'FreeBSD' ] || [ $UNAME_S == 'Darwin' ]; then
+	# usage: touch_date <unix ts> <file>
+	touch_date()
+	{
+		touch -t `date -r $1 +%Y%m%d%H%M.%S` "$2"
+	}
+
+	# usage: last_modified <file>
+	last_modified()
+	{
+		stat -f "%m" "$1"
+	}
+
+	# usage: format_last_modified <file>
+	format_last_modified()
+	{
+		stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S %z" "$1"
+	}
+
+	# usage: head_n [count]
+	head_n()
+	{
+		if [ "$1" -gt 0 ]; then
+			head -n "$1"
+		fi
+	}
+
+	# usage: sha1 [file]
+	sha1()
+	{
+		if [ $# = 1 ]
+		then
+			openssl dgst -sha1 "$1" | sed "s,SHA1.\(.*\).= \(.*\),\2  \1,"
+		else
+			openssl dgst -sha1 | sed 's,\(.*= \)*\(.*\),\2  -,'
+		fi
+	}
+
+	# usage: cp_a <src> <dst>
+	cp_a()
+	{
+		cp -pR "$1" "$2"
+	}
+
+	# usage: _tac
+	_tac()
+	{
+		sed -e '1!G;h;$!d'
+	}
+
+	_seq()
+	{
+		(
+			if [ $# -eq 1 ]
+			then
+				/usr/bin/jot $1
+			elif [ $# -eq 2 ]
+			then
+				n1=$((${2} - ${1} + 1))
+				n2=$1
+				/usr/bin/jot $n1 $n2
+			elif [ $# -eq 3 ]
+			then
+				num1=$1
+				incr=$2
+				num2=$3
+				/usr/bin/awk -v n1=$num1 -v n2=$num2 -v add=$incr 'BEGIN{ for(i=n1; i<=n2; i+=add) print i;}' | /usr/bin/sed -E '/e/s/^.+e.+$/0/'
+			fi
+		)
+		return 0
+	}
+elif [ $UNAME_S == 'Linux' ] || [ $UNAME_S == 'SunOS' ]; then
+	# usage: touch_date <unix ts> <file>
+	touch_date()
+	{
+		touch -d @$1 "$2"
+	}
+
+	# usage: last_modified <file>
+	last_modified()
+	{
+		stat -c "%Y" "$1"
+	}
+
+	# usage: format_last_modified <file>
+	format_last_modified()
+	{
+		# must strip nano-second part otherwise git gets very
+		# confused, and makes up strange timestamps from the past
+		# (chances are it decides to interpret it as a unix
+		# timestamp).
+		stat -c "%y" "$1" | sed -e '
+	s/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) \([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)\.[0-9]* \(.*\)$/\1-\2-\3 \4:\5:\6 \7/'
+	}
+
+	# usage: head_n [count]
+	head_n()
+	{
+		head -n "$1"
+	}
+
+	# usage: sha1 [file]
+	sha1()
+	{
+		if [ $# = 1 ]
+		then
+			sha1sum "$1"
+		else
+			sha1sum
+		fi
+	}
+
+	# usage: cp_a <src> <dst>
+	cp_a()
+	{
+		cp -a "$1" "$2"
+	}
+
+	# usage: _tac
+	_tac()
+	{
+		tac
+	}
+
+	_seq()
+	{
+		seq "$@"
+		return $?
+	}
+else
+	die "Unsupported operating system: $UNAME_S"
+fi
diff --git a/os.Darwin b/os.Darwin
deleted file mode 100644
index 3f23121..0000000
--- a/os.Darwin
+++ /dev/null
@@ -1,70 +0,0 @@
-# usage: touch_date <unix ts> <file>
-touch_date()
-{
-	touch -t `date -r $1 +%Y%m%d%H%M.%S` "$2"
-}
-
-# usage: last_modified <file>
-last_modified()
-{
-	stat -f "%m" "$1"
-}
-
-# usage: format_last_modified <file>
-format_last_modified()
-{
-	stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S %z" "$1"
-}
-
-# usage: head_n [count]
-head_n()
-{
-	if [ "$1" -gt 0 ]; then
-		head -n "$1"
-	fi
-}
-
-# usage: sha1 [file]
-sha1()
-{
-	if [ $# = 1 ]
-	then
-		openssl dgst -sha1 "$1" | sed "s,SHA1.\(.*\).= \(.*\),\2  \1,"
-	else
-		openssl dgst -sha1 | sed 's,\(.*= \)*\(.*\),\2  -,'
-	fi
-}
-
-# usage: cp_a <src> <dst>
-cp_a()
-{
-	cp -pR "$1" "$2"
-}
-
-# usage: _tac
-_tac()
-{
-	sed -e '1!G;h;$!d'
-}
-
-_seq()
-{
-	(
-		if [ $# -eq 1 ]
-		then
-			/usr/bin/jot $1
-		elif [ $# -eq 2 ]
-		then
-			n1=$((${2} - ${1} + 1))
-			n2=$1
-			/usr/bin/jot $n1 $n2
-		elif [ $# -eq 3 ]
-		then
-			num1=$1
-			incr=$2
-			num2=$3
-			/usr/bin/awk -v n1=$num1 -v n2=$num2 -v add=$incr 'BEGIN{ for(i=n1; i<=n2; i+=add) print i;}' | /usr/bin/sed -E '/e/s/^.+e.+$/0/'
-		fi
-	)
-	return 0
-}
diff --git a/os.Linux b/os.Linux
deleted file mode 100644
index aaebf88..0000000
--- a/os.Linux
+++ /dev/null
@@ -1,57 +0,0 @@
-# usage: touch_date <unix ts> <file>
-touch_date()
-{
-	touch -d @$1 "$2"
-}
-
-# usage: last_modified <file>
-last_modified()
-{
-	stat -c "%Y" "$1"
-}
-
-# usage: format_last_modified <file>
-format_last_modified()
-{
-	# must strip nano-second part otherwise git gets very
-	# confused, and makes up strange timestamps from the past
-	# (chances are it decides to interpret it as a unix
-	# timestamp).
-	stat -c "%y" "$1" | sed -e '
-s/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) \([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)\.[0-9]* \(.*\)$/\1-\2-\3 \4:\5:\6 \7/'
-}
-
-# usage: head_n [count]
-head_n()
-{
-	head -n "$1"
-}
-
-# usage: sha1 [file]
-sha1()
-{
-	if [ $# = 1 ]
-	then
-		sha1sum "$1"
-	else
-		sha1sum
-	fi
-}
-
-# usage: cp_a <src> <dst>
-cp_a()
-{
-	cp -a "$1" "$2"
-}
-
-# usage: _tac
-_tac()
-{
-	tac
-}
-
-_seq()
-{
-	seq "$@"
-	return $?
-}
diff --git a/os.SunOS b/os.SunOS
deleted file mode 100644
index aaebf88..0000000
--- a/os.SunOS
+++ /dev/null
@@ -1,57 +0,0 @@
-# usage: touch_date <unix ts> <file>
-touch_date()
-{
-	touch -d @$1 "$2"
-}
-
-# usage: last_modified <file>
-last_modified()
-{
-	stat -c "%Y" "$1"
-}
-
-# usage: format_last_modified <file>
-format_last_modified()
-{
-	# must strip nano-second part otherwise git gets very
-	# confused, and makes up strange timestamps from the past
-	# (chances are it decides to interpret it as a unix
-	# timestamp).
-	stat -c "%y" "$1" | sed -e '
-s/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) \([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)\.[0-9]* \(.*\)$/\1-\2-\3 \4:\5:\6 \7/'
-}
-
-# usage: head_n [count]
-head_n()
-{
-	head -n "$1"
-}
-
-# usage: sha1 [file]
-sha1()
-{
-	if [ $# = 1 ]
-	then
-		sha1sum "$1"
-	else
-		sha1sum
-	fi
-}
-
-# usage: cp_a <src> <dst>
-cp_a()
-{
-	cp -a "$1" "$2"
-}
-
-# usage: _tac
-_tac()
-{
-	tac
-}
-
-_seq()
-{
-	seq "$@"
-	return $?
-}
diff --git a/regression/scaffold b/regression/scaffold
index 546d8c6..4d4613b 100644
--- a/regression/scaffold
+++ b/regression/scaffold
@@ -25,8 +25,8 @@ function die
 
 UNAME_S=`uname -s`
 
-if [ -r "$REG_DIR/../os.$UNAME_S" ]; then
-	. "$REG_DIR/../os.$UNAME_S"
+if [ -r "$REG_DIR/../os" ]; then
+	. "$REG_DIR/../os"
 else
 	die "Unsupported operating system: $UNAME_S"
 fi
-- 
1.8.3.4