Download:
child 18:45abe3e21572
parent 16:2a4c890a667a
17:facad31ec0dc
Anton Shestakov <engored@ya.ru>, Sat, 29 Nov 2014 12:56:58 +0800
find-closest: searching for a thing in parent dirs, a la jQuery.closest

5 files changed, 80 insertions(+), 0 deletions(-) [+]
find-closest/hgroot_0.sh file | annotate | diff | comparison | revisions
find-closest/hgroot_1.sh file | annotate | diff | comparison | revisions
find-closest/hgroot_2.sh file | annotate | diff | comparison | revisions
find-closest/hgroot_3.sh file | annotate | diff | comparison | revisions
find-closest/test.sh file | annotate | diff | comparison | revisions
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/find-closest/hgroot_0.sh Sat Nov 29 12:56:58 2014 +0800
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+hg root 2> /dev/null
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/find-closest/hgroot_1.sh Sat Nov 29 12:56:58 2014 +0800
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+root="$(pwd -P)"
+while [[ $root && ! -d $root/.hg ]]
+do
+ root="${root%/*}"
+done
+echo "$root"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/find-closest/hgroot_2.sh Sat Nov 29 12:56:58 2014 +0800
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+root="$PWD"
+while true ; do
+ [[ -d "$root/.hg" ]] && echo "$root" && break
+ [[ $root == '/' ]] && break
+ root="$(dirname "$root")"
+done
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/find-closest/hgroot_3.sh Sat Nov 29 12:56:58 2014 +0800
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+function upsearch () {
+ test / == "$PWD" && return || test -e "$1" && echo "$PWD" && return || cd .. && upsearch "$1"
+}
+
+upsearch '.hg'
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/find-closest/test.sh Sat Nov 29 12:56:58 2014 +0800
@@ -0,0 +1,54 @@
+#!/bin/bash
+
+function do_test {
+ RESULT=$($1) # get the result and also warm cache
+ TIME=$( {
+ time for r in `seq 1 100` ; do
+ $1 > /dev/null 2>&1
+ done;
+ } 2>&1 | gawk 'match($0, /^real\t([0-9]+)m([0-9.]+)s$/, m) {printf "%.3f", m[1] * 60 + m[2]}' )
+ if [[ $RESULT == $CHECK ]] ; then
+ echo -n 'ok '
+ else
+ echo -n 'fail '
+ fi
+ echo -en "$TIME\t "
+}
+
+HERE=$PWD
+
+pushd "$PWD" > /dev/null
+TESTROOT=$(mktemp -d)
+cd "$TESTROOT"
+CHECK=''
+
+echo 'not tracked'
+echo -e 'builtins \t dirname \t upsearch'
+
+for depth in `seq 1 10` ; do
+ cd "$(mktemp -dp.)"
+ #do_test "$HERE/hgroot_0.sh"
+ do_test "$HERE/hgroot_1.sh"
+ do_test "$HERE/hgroot_2.sh"
+ do_test "$HERE/hgroot_3.sh"
+ echo
+done
+
+cd "$TESTROOT"
+mkdir '.hg'
+CHECK=$TESTROOT
+
+echo 'tracked in mercurial'
+echo -e 'builtins \t dirname \t upsearch'
+
+for depth in `seq 1 10` ; do
+ cd "$(mktemp -dp.)"
+ #do_test "$HERE/hgroot_0.sh"
+ do_test "$HERE/hgroot_1.sh"
+ do_test "$HERE/hgroot_2.sh"
+ do_test "$HERE/hgroot_3.sh"
+ echo
+done
+
+popd > /dev/null
+rm -r "$TESTROOT"