-
Notifications
You must be signed in to change notification settings - Fork 4
/
up
executable file
·71 lines (59 loc) · 1.25 KB
/
up
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
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
#
# Usage: up
#
# Move back up to the project root.
#
# Up has a heuristic to detect the project root.
#
# If no project root is found it stays in the current directory
# If the current directory is already a project root it will try to find
# a parent project
require 'find'
require 'pathname'
require 'uri'
def abort(*)
puts Dir.pwd
super
end
case ARGV.shift
when "-h", "--help"
puts "up is not installed"
puts
abort "Usage: eval \"$(up --setup)\""
when "--setup"
puts <<-SH
up() {
_up_dir=$(command up "$@")
if [ $? = 0 ]; then
[ "$_up_dir" != "$PWD" ] && cd "$_up_dir"
fi
}
SH
exit
end
HOME = Pathname.new(ENV['HOME']) if ENV['HOME']
PWD = ENV['PWD'] ? Pathname.new(ENV['PWD']) : Pathname.pwd
def project_root?(dir)
(dir + '.git').directory? ||
(dir + '.hg').directory? ||
(dir + '.envrc').file? ||
(dir + 'Gemfile').file? ||
(dir.to_s == ENV['DIRENV_DIR'].to_s[1..-1]) ||
false
end
def should_stop?(dir)
dir.root? || dir == HOME
end
def find_project_root
dir = PWD
# Search parent projects if already in a
# sub-project
dir = dir.parent if project_root?(dir)
while !should_stop?(dir)
return dir if project_root?(dir)
dir = dir.parent
end
PWD
end
puts find_project_root