-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocate-dominating-file
executable file
·50 lines (45 loc) · 1.38 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
39
40
41
42
43
44
45
46
47
48
49
50
#!/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] [directory] [filename]"
echo
echo " -h/--help - Print help and exit."
echo " -s/--silent - Don't print anything to stdout."
exit
end
if test (count $argv) -gt 1
if not test -d "$argv[1]"
cd (path dirname "$argv[1]")
else
cd "$argv[1]"
end
set filename "$argv[2]"
else if test (count $argv) = '1'
set filename "$argv[1]"
else
echo "$script: Error: You must provide a filename to search for." >&2
exit 1
end
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