-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.sh
executable file
·42 lines (37 loc) · 876 Bytes
/
parse.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
#!/usr/bin/env sh
# Get buffer address
f=$(realpath $1)
# Go to buffer directory
[[ -d $f ]] && cd $f || cd "${f%/*}"
# Check if buffer address is a directory and return it if find .mxc isn't specified
[[ -d $f ]] && [[ $2 -ne 1 ]] && {
echo $f
exit 0
}
# Look for .mxc if specified
m=".mxc"
[[ $2 = 1 ]] && {
# Get containing directory if buffer address is a file (or use buffer address if it's a directory)
[[ -d $f ]] && d=$f || d="${f%/*}"
# Check for .mxc in immediate directory
[[ -f "$d/$m" ]] && {
echo "$d/$m"
exit 0
}
# Parse git repo back to root for .mxc if in a git repo
dGit=$(git rev-parse --show-toplevel 2> /dev/null)
[[ -d $dGit ]] && {
while [[ $d != $dGit ]]
do
d="${d%/*}"
[[ -f "$d/$m" ]] && {
echo "$d/$m"
exit 0
}
done
}
# Exit with error if .mxc not found
exit 1
}
# Return original buffer address
echo $f