-
Notifications
You must be signed in to change notification settings - Fork 6
/
pushd-root.sh
59 lines (51 loc) · 1.89 KB
/
pushd-root.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
# This is a helper script for make-pretty-timed.sh and
# make-pretty-timed-diff.sh.
# in case we're run from out of git repo
PUSHD_ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
pushd "$PUSHD_ROOT_DIR" 1>/dev/null
# now change to the git root; use `cd` so we only need one `popd`
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "$ROOT_DIR"
# We assume that we're a git submodule, so we need to do this once more
cd ..
# now change to the git root; use `cd` so we only need one `popd`
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "$ROOT_DIR"
# Now find a makefile. We assume that the top-level makefile is last
# in git ls-files; we want to pick up everything, whether it is cached
# or just laying around
MAKEFILE="$(git ls-files --cached --others *GNUmakefile *makefile *Makefile | tail -1)"
if [ ! -z "$MAKEFILE" ]; then
cd "$(dirname "$MAKEFILE")"
else
echo "WARNING: No GNUmakefile,makefile,Makefile, found in git ls-files"
fi
MAKEFILE="$((git ls-files --cached --others *Makefile; git ls-files --cached --others *makefile; git ls-files --cached --others *GNUmakefile) | tail -1)"
function relpath() {
SRC="$(readlink -f "$(pwd)")/"
TGT="$(readlink -f "$1")"
RET=""
while [ ! -z "$SRC$TGT" ]; do
# strip the first component off both paths
NEXT_SRC="${SRC#*/}"
NEXT_TGT="${TGT#*/}"
CUR_SRC="${SRC%$NEXT_SRC}"
CUR_TGT="${TGT%$NEXT_TGT}"
# if they match, and there's something there, then keep going
if [ "$CUR_SRC" == "$CUR_TGT" ] && [ ! -z "$CUR_SRC" ]; then
SRC="$NEXT_SRC"
TGT="$NEXT_TGT"
else
# if they don't match, then the entirety of the target goes into the return (only relevant the first time)
RET="$TGT"
TGT=""
# and then if we've actually stripped something off of SRC, we add a ../ to the beginning of ret
if [ ! -z "$CUR_SRC" ]; then
RET="../$RET"
SRC="$NEXT_SRC"
fi
fi
done
echo "$RET"
}