[PATCH] Fixed a bashism with the use of $RANDOM in hgeditor

Martin Geisler mg at aragost.com
Tue May 25 03:03:59 CDT 2010


Javi Merino <cibervicho at gmail.com> writes:

> Hi, similar to a previous patch, there is another bashism in hgeditor.
> The $RANDOM variable is bash specific, so a generic /bin/sh may not
> expand it. This patch fixes it.
>
> Regards,
> Javi (Vicho)
>
> # HG changeset patch
> # User Javi Merino <cibervicho at gmail.com>
> # Date 1274285195 -7200
> # Node ID f4938bf98c51c9319c4a82e0eb7faa000c03fc97
> # Parent  d26f662bfbf599a10e6cb354fdb0c46f372c1c5d
> Fixed a bashism with the use of $RANDOM in hgeditor.
>
> The variable $RANDOM is not POSIX so a portable /bin/sh may not define
> it.  When creating a directory with a random name it's better to use
> mktemp, which, even though is not POSIX, exists in common Unixes
> including Linux, OpenBSD, FreeBSD and MacOS X.
>
> diff --git a/hgeditor b/hgeditor
> --- a/hgeditor
> +++ b/hgeditor
> @@ -27,11 +27,8 @@ cleanup_exit() {
>  trap "cleanup_exit" 0 # normal exit
>  trap "exit 255" HUP INT QUIT ABRT TERM
>  
> -HGTMP="${TMPDIR-/tmp}/hgeditor.$RANDOM.$RANDOM.$RANDOM.$$"
> -(umask 077 && mkdir "$HGTMP") || {
> -    echo "Could not create temporary directory! Exiting." 1>&2
> -    exit 1
> -}
> +HGTMP=$(mktemp -d ${TMPDIR-/tmp}/hgeditor.XXXXXX)

My mktemp has a --tmpdir flag which will make it use $TMPDIR
automatically or fall back to /tmp. This is on Linux -- can we use that
in general?

> +[ x$HGTMP != x -a -d $HGTMP ] || echo "Could not create temporary directory! Exiting." 1>&2

It seems we lost the "exit" line here. I can fix that, but I was just
curious about the --tmpdir flag.

-- 
Martin Geisler

aragost Trifork
Professional Mercurial support
http://aragost.com/mercurial/


More information about the Mercurial-devel mailing list