Class: GitSwitch::GitHelper
- Inherits:
-
Object
- Object
- GitSwitch::GitHelper
- Defined in:
- lib/git_switch/git_helper.rb
Class Method Summary collapse
-
.find_git_repo(start_path = '.') ⇒ Object
Returns the git root directory given a path inside the repo.
- .git_repo? ⇒ Boolean
-
.root_directory?(file_path) ⇒ Boolean
Returns true if the given path represents a root directory (/ or C:/).
Class Method Details
.find_git_repo(start_path = '.') ⇒ Object
Returns the git root directory given a path inside the repo. Returns nil if the path is not in a git repo.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/git_switch/git_helper.rb', line 22 def self.find_git_repo(start_path = '.') raise NoSuchPathError unless File.exists?(start_path) current_path = File.(start_path) # for clarity: set to an explicit nil and then just return whatever # the current value of this variable is (nil or otherwise) return_path = nil until root_directory?(current_path) if File.exists?(File.join(current_path, '.git')) # done return_path = current_path break else # go up a directory and try again current_path = File.dirname(current_path) end end return_path end |
.git_repo? ⇒ Boolean
3 4 5 |
# File 'lib/git_switch/git_helper.rb', line 3 def self.git_repo? !find_git_repo.nil? end |
.root_directory?(file_path) ⇒ Boolean
Returns true if the given path represents a root directory (/ or C:/)
13 14 15 16 17 18 |
# File 'lib/git_switch/git_helper.rb', line 13 def self.root_directory?(file_path) # Implementation inspired by http://stackoverflow.com/a/4969416: # Does file + ".." resolve to the same directory as file_path? File.directory?(file_path) && File.(file_path) == File.(File.join(file_path, '..')) end |