Class: GitJump::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/git_jump/repository.rb

Overview

Wrapper for git repository operations

Defined Under Namespace

Classes: NotAGitRepositoryError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = Dir.pwd) ⇒ Repository

Returns a new instance of Repository.



14
15
16
17
# File 'lib/git_jump/repository.rb', line 14

def initialize(path = Dir.pwd)
  @path = File.expand_path(path)
  raise NotAGitRepositoryError, "Not a git repository: #{@path}" unless valid?
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/git_jump/repository.rb', line 12

def path
  @path
end

Instance Method Details

#branch_exists?(branch_name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/git_jump/repository.rb', line 58

def branch_exists?(branch_name)
  branches.include?(branch_name)
end

#branchesObject



39
40
41
42
43
44
# File 'lib/git_jump/repository.rb', line 39

def branches
  result = execute_git("branch", "--format=%(refname:short)")
  result.split("\n").map(&:strip)
rescue StandardError
  []
end

#checkout(branch_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/git_jump/repository.rb', line 46

def checkout(branch_name)
  # Set environment variable to skip git-jump hook during checkout
  # This prevents double-loading of gems when git-jump triggers checkout
  ENV["GIT_JUMP_SKIP_HOOK"] = "1"
  execute_git("checkout", branch_name)
  true
rescue StandardError => e
  raise "Failed to checkout branch '#{branch_name}': #{e.message}"
ensure
  ENV.delete("GIT_JUMP_SKIP_HOOK")
end

#current_branchObject



32
33
34
35
36
37
# File 'lib/git_jump/repository.rb', line 32

def current_branch
  result = execute_git("branch", "--show-current")
  result.strip
rescue StandardError
  nil
end

#hook_installed?(hook_name) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/git_jump/repository.rb', line 81

def hook_installed?(hook_name)
  path = hook_path(hook_name)
  File.exist?(path) && File.executable?(path)
end

#hook_path(hook_name) ⇒ Object



62
63
64
# File 'lib/git_jump/repository.rb', line 62

def hook_path(hook_name)
  File.join(project_path, ".git", "hooks", hook_name)
end

#install_hook(hook_name, content) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/git_jump/repository.rb', line 66

def install_hook(hook_name, content)
  path = hook_path(hook_name)
  hooks_dir = File.dirname(path)

  # Create hooks directory if it doesn't exist
  FileUtils.mkdir_p(hooks_dir) unless File.directory?(hooks_dir)

  File.write(path, content)
  FileUtils.chmod(0o755, path)

  true
rescue StandardError => e
  raise "Failed to install hook '#{hook_name}': #{e.message}"
end

#project_basenameObject



28
29
30
# File 'lib/git_jump/repository.rb', line 28

def project_basename
  File.basename(project_path)
end

#project_pathObject



24
25
26
# File 'lib/git_jump/repository.rb', line 24

def project_path
  @project_path ||= find_git_root || @path
end

#read_hook(hook_name) ⇒ Object



86
87
88
89
90
# File 'lib/git_jump/repository.rb', line 86

def read_hook(hook_name)
  File.read(hook_path(hook_name))
rescue StandardError
  nil
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/git_jump/repository.rb', line 19

def valid?
  git_dir = File.join(@path, ".git")
  File.directory?(git_dir) || find_git_root
end