class GitSwitch::GitHelper

Public Class Methods

git_repo?() click to toggle source
# File lib/git_switch/git_helper.rb, line 3
def self.git_repo?
  !find_git_repo.nil?
end

Private Class Methods

find_git_repo(start_path = '.') click to toggle source

Returns the git root directory given a path inside the repo. Returns nil if the path is not in a git repo.

# 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.expand_path(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
root_directory?(file_path) click to toggle source

Returns true if the given path represents a root directory (/ or C:/)

# 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.expand_path(file_path) == File.expand_path(File.join(file_path, '..'))
end