Module: PromptObjects::Env::Git

Defined in:
lib/prompt_objects/environment/git.rb

Overview

Git operations for environments. Each environment is a git repository for built-in versioning.

Constant Summary collapse

DEFAULT_USER_NAME =
"Prompt Objects"
DEFAULT_USER_EMAIL =
"prompt-objects@localhost"

Class Method Summary collapse

Class Method Details

.add_all(path) ⇒ Object

Stage all changes.

Parameters:

  • path (String)


28
29
30
# File 'lib/prompt_objects/environment/git.rb', line 28

def self.add_all(path)
  Dir.chdir(path) { system("git add -A") }
end

.auto_commit(path, message = "Auto-save") ⇒ Boolean

Auto-commit if there are changes (used for saving PO modifications).

Parameters:

  • path (String)
  • message (String) (defaults to: "Auto-save")

Returns:

  • (Boolean)

    True if committed, false if no changes



116
117
118
119
120
121
# File 'lib/prompt_objects/environment/git.rb', line 116

def self.auto_commit(path, message = "Auto-save")
  return false unless dirty?(path)

  commit(path, message)
  true
end

.bundle(path, output) ⇒ Boolean

Create a git bundle for export.

Parameters:

  • path (String)
  • output (String)

    Output file path

Returns:

  • (Boolean)

    Success



98
99
100
101
102
# File 'lib/prompt_objects/environment/git.rb', line 98

def self.bundle(path, output)
  Dir.chdir(path) do
    system("git bundle create #{output} --all 2>/dev/null")
  end
end

.clone_bundle(bundle_path, dest_path) ⇒ Boolean

Clone from a git bundle.

Parameters:

  • bundle_path (String)
  • dest_path (String)

Returns:

  • (Boolean)

    Success



108
109
110
# File 'lib/prompt_objects/environment/git.rb', line 108

def self.clone_bundle(bundle_path, dest_path)
  system("git clone --quiet #{bundle_path} #{dest_path} 2>/dev/null")
end

.commit(path, message) ⇒ Boolean

Create a commit.

Parameters:

  • path (String)
  • message (String)

Returns:

  • (Boolean)

    Success



36
37
38
39
40
41
42
# File 'lib/prompt_objects/environment/git.rb', line 36

def self.commit(path, message)
  ensure_identity(path)
  Dir.chdir(path) do
    system("git add -A")
    system("git", "commit", "--quiet", "-m", message)
  end
end

.commit_count(path) ⇒ Integer

Get commit count.

Parameters:

  • path (String)

Returns:

  • (Integer)


74
75
76
77
78
# File 'lib/prompt_objects/environment/git.rb', line 74

def self.commit_count(path)
  Dir.chdir(path) do
    `git rev-list --count HEAD 2>/dev/null`.strip.to_i
  end
end

.current_commit(path) ⇒ String?

Get current commit hash (short).

Parameters:

  • path (String)

Returns:

  • (String, nil)


64
65
66
67
68
69
# File 'lib/prompt_objects/environment/git.rb', line 64

def self.current_commit(path)
  Dir.chdir(path) do
    result = `git rev-parse --short HEAD 2>/dev/null`.strip
    result.empty? ? nil : result
  end
end

.dirty?(path) ⇒ Boolean

Check if there are uncommitted changes.

Parameters:

  • path (String)

Returns:

  • (Boolean)


57
58
59
# File 'lib/prompt_objects/environment/git.rb', line 57

def self.dirty?(path)
  uncommitted_changes(path).any?
end

.ensure_identity(path) ⇒ Object

New users may not have a global Git identity yet. Environment history should still work without modifying their global configuration, so use a repository-local automation identity only when no identity resolves.



126
127
128
129
130
131
132
133
134
135
# File 'lib/prompt_objects/environment/git.rb', line 126

def self.ensure_identity(path)
  Dir.chdir(path) do
    unless system("git", "config", "user.name", out: File::NULL, err: File::NULL)
      system("git", "config", "user.name", DEFAULT_USER_NAME)
    end
    unless system("git", "config", "user.email", out: File::NULL, err: File::NULL)
      system("git", "config", "user.email", DEFAULT_USER_EMAIL)
    end
  end
end

.init(path) ⇒ Object

Initialize a git repository.

Parameters:

  • path (String)


20
21
22
23
24
# File 'lib/prompt_objects/environment/git.rb', line 20

def self.init(path)
  initialized = Dir.chdir(path) { system("git", "init", "--quiet") }
  ensure_identity(path) if initialized
  initialized
end

.recent_commits(path, limit: 10) ⇒ Array<Hash>

Get recent commits.

Parameters:

  • path (String)
  • limit (Integer) (defaults to: 10)

Returns:

  • (Array<Hash>)

    Array of message:, date:



84
85
86
87
88
89
90
91
92
# File 'lib/prompt_objects/environment/git.rb', line 84

def self.recent_commits(path, limit: 10)
  Dir.chdir(path) do
    log = `git log --oneline --format="%h|%s|%ci" -n #{limit} 2>/dev/null`
    log.lines.map do |line|
      hash, message, date = line.strip.split("|", 3)
      { hash: hash, message: message, date: date }
    end
  end
end

.repo?(path) ⇒ Boolean

Check if a directory is a git repository.

Parameters:

  • path (String)

Returns:

  • (Boolean)


14
15
16
# File 'lib/prompt_objects/environment/git.rb', line 14

def self.repo?(path)
  Dir.exist?(File.join(path, ".git"))
end

.uncommitted_changes(path) ⇒ Array<String>

Get list of uncommitted changes.

Parameters:

  • path (String)

Returns:

  • (Array<String>)

    Changed file paths



47
48
49
50
51
52
# File 'lib/prompt_objects/environment/git.rb', line 47

def self.uncommitted_changes(path)
  Dir.chdir(path) do
    status = `git status --porcelain 2>/dev/null`
    status.lines.map { |line| line[3..].strip }
  end
end