-
Notifications
You must be signed in to change notification settings - Fork 0
/
locate-dominating-file
executable file
·38 lines (33 loc) · 1.08 KB
/
locate-dominating-file
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
#!/usr/bin/env fish
# locate-dominating-file - Search upwards in the directory hierarchy for the specified file.
# This can be used, for example, to check if we're in a git repository, by locating ".git".
# Returns 1 if the target was not found, or 0 on success.
# With the -s or --silent argument, don't print anything to stdout.
# Inspired by the Emacs function of the same name.
set -- script "$(path basename (status filename))"
argparse -n $script 'h/help' 's/silent' -- $argv
if set -q _flag_h
echo "$script - Search upwards in the directory hierarchy for the specified file."
echo "Usage: $script [arguments] [filename]"
echo
echo " -h/--help - Print help and exit."
echo " -s/--silent - Don't print anything to stdout."
exit
end
set filename $argv[1]
while true
if test -e "$filename"
if not set -q _flag_s
echo -n (pwd)
if not string match -q '*/' (pwd)
echo -n '/'
end
echo "$filename"
end
exit 0
end
if test (pwd) = '/'
exit 1
end
cd ..
end