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
-
.add_all(path) ⇒ Object
Stage all changes.
-
.auto_commit(path, message = "Auto-save") ⇒ Boolean
Auto-commit if there are changes (used for saving PO modifications).
-
.bundle(path, output) ⇒ Boolean
Create a git bundle for export.
-
.clone_bundle(bundle_path, dest_path) ⇒ Boolean
Clone from a git bundle.
-
.commit(path, message) ⇒ Boolean
Create a commit.
-
.commit_count(path) ⇒ Integer
Get commit count.
-
.current_commit(path) ⇒ String?
Get current commit hash (short).
-
.dirty?(path) ⇒ Boolean
Check if there are uncommitted changes.
-
.ensure_identity(path) ⇒ Object
New users may not have a global Git identity yet.
-
.init(path) ⇒ Object
Initialize a git repository.
-
.recent_commits(path, limit: 10) ⇒ Array<Hash>
Get recent commits.
-
.repo?(path) ⇒ Boolean
Check if a directory is a git repository.
-
.uncommitted_changes(path) ⇒ Array<String>
Get list of uncommitted changes.
Class Method Details
.add_all(path) ⇒ Object
Stage all changes.
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).
116 117 118 119 120 121 |
# File 'lib/prompt_objects/environment/git.rb', line 116 def self.auto_commit(path, = "Auto-save") return false unless dirty?(path) commit(path, ) true end |
.bundle(path, output) ⇒ Boolean
Create a git bundle for export.
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.
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.
36 37 38 39 40 41 42 |
# File 'lib/prompt_objects/environment/git.rb', line 36 def self.commit(path, ) ensure_identity(path) Dir.chdir(path) do system("git add -A") system("git", "commit", "--quiet", "-m", ) end end |
.commit_count(path) ⇒ Integer
Get commit count.
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).
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.
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.
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.
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, , date = line.strip.split("|", 3) { hash: hash, message: , date: date } end end end |
.repo?(path) ⇒ Boolean
Check if a directory is a git repository.
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.
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 |