Class: GitJump::Actions::Install

Inherits:
Base
  • Object
show all
Defined in:
lib/git_jump/actions/install.rb

Overview

Action to install post-checkout git hook

Constant Summary collapse

HOOK_TEMPLATE =
<<~BASH.freeze
  #!/bin/sh
  # Git Jump post-checkout hook
  # Auto-generated - do not edit manually

  PREV_HEAD=$1
  NEW_HEAD=$2
  BRANCH_CHECKOUT=$3

  # Skip if called from git-jump itself to avoid double-loading
  if [ -n "$GIT_JUMP_SKIP_HOOK" ]; then
      exit 0
  fi

  # Only run on branch checkouts (not file checkouts)
  if [ "$BRANCH_CHECKOUT" = "1" ]; then
      RUBY_PATH="$(which ruby)"
  #{"    "}
      if [ -z "$RUBY_PATH" ]; then
          exit 0
      fi
  #{"    "}
      "$RUBY_PATH" -e "
        begin
          require 'git_jump'
          GitJump::Hooks::PostCheckout.new('$(pwd)').run
        rescue LoadError
          # Gem not available, skip silently
        rescue => e
          # Silent error handling in hook
        end
      " 2>/dev/null
  fi
BASH

Instance Attribute Summary

Attributes inherited from Base

#config, #database, #output, #repository

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from GitJump::Actions::Base

Instance Method Details

#executeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/git_jump/actions/install.rb', line 44

def execute
  if repository.hook_installed?("post-checkout")
    existing_content = repository.read_hook("post-checkout")

    if existing_content&.include?("Git Jump post-checkout hook")
      output.info("Git Jump hook already installed")
      return true
    else
      output.warning("A post-checkout hook already exists")
      return false unless output.prompt("Overwrite existing hook?")
    end
  end

  repository.install_hook("post-checkout", HOOK_TEMPLATE)
  output.success("Installed post-checkout hook in #{repository.project_basename}")
  output.info("Branches will now be automatically tracked on checkout")

  true
rescue StandardError => e
  output.error("Failed to install hook: #{e.message}")
  false
end