Class: Carson::Warehouse

Inherits:
Object
  • Object
show all
Defined in:
lib/carson/warehouse.rb

Overview

A governed repository — the warehouse where parcels are stored on shelves (worktrees) with labels (branches). Wraps git operations with story-language methods. An intelligent warehouse that manages itself: packing parcels, checking compliance, and sweeping up.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, main_label: "main", bureau_address: "github", compliance_checker: nil) ⇒ Warehouse

Returns a new instance of Warehouse.



15
16
17
18
19
20
# File 'lib/carson/warehouse.rb', line 15

def initialize( path:, main_label: "main", bureau_address: "github", compliance_checker: nil )
	@path = path
	@main_label = main_label
	@bureau_address = bureau_address
	@compliance_checker = compliance_checker
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/carson/warehouse.rb', line 13

def path
  @path
end

Instance Method Details

#based_on_latest_standard?(parcel, registry: "#{bureau_address}/#{main_label}") ⇒ Boolean

Is the parcel based on the client’s latest production standard? Checks whether the registry tip is an ancestor of the parcel’s head.

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/carson/warehouse.rb', line 70

def based_on_latest_standard?( parcel, registry: "#{bureau_address}/#{main_label}" )
	_, _, status = git( "merge-base", "--is-ancestor", registry, parcel.head )
	status.success?
end

#bureau_addressObject

The bureau’s address (remote name).



40
41
42
# File 'lib/carson/warehouse.rb', line 40

def bureau_address
	@bureau_address
end

#clean?Boolean

Is the warehouse floor clean? No uncommitted changes on the current shelf.

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/carson/warehouse.rb', line 45

def clean?
	output, _, status = git( "status", "--porcelain" )
	status.success? && output.strip.empty?
end

#current_headObject

The tip of the parcel on the current shelf (commit SHA).



30
31
32
# File 'lib/carson/warehouse.rb', line 30

def current_head
	git( "rev-parse", "HEAD" ).first.strip
end

#current_labelObject

The label on the current shelf (branch name).



25
26
27
# File 'lib/carson/warehouse.rb', line 25

def current_label
	git( "rev-parse", "--abbrev-ref", "HEAD" ).first.strip
end

#fetch_latest(remote: bureau_address, registry: nil) ⇒ Object

Get latest registry state from the bureau (git fetch). Returns true on success, false on failure.



61
62
63
64
65
66
# File 'lib/carson/warehouse.rb', line 61

def fetch_latest( remote: bureau_address, registry: nil )
	arguments = [ "fetch", remote ]
	arguments << registry if registry
	_, _, status = git( *arguments )
	status.success?
end

#label_absorbed?(name) ⇒ Boolean

Has this label been merged into main?

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/carson/warehouse.rb', line 129

def label_absorbed?( name )
	merged_output, = git( "branch", "--merged", main_label, "--format", "%(refname:short)" )
	merged_output.lines.map { it.strip }.include?( name )
end

#labelsObject

All labels (branch names).



123
124
125
126
# File 'lib/carson/warehouse.rb', line 123

def labels
	output, = git( "branch", "--format", "%(refname:short)" )
	output.lines.map { it.strip }.reject { it.empty? }
end

#main_labelObject

The destination label (from config).



35
36
37
# File 'lib/carson/warehouse.rb', line 35

def main_label
	@main_label
end

#main_worktree_rootObject

The main warehouse location — resolves correctly even from a side shelf. Used by sync! and ledger recording to always reference the canonical path.



136
137
138
139
140
141
# File 'lib/carson/warehouse.rb', line 136

def main_worktree_root
	git_common_dir, = git( "rev-parse", "--path-format=absolute", "--git-common-dir" )
	common = git_common_dir.strip
	# If it ends with /.git, the parent is the main worktree root.
	common.end_with?( "/.git" ) ? File.dirname( common ) : common
end

#pack!(message:) ⇒ Object

Pack a parcel — stage all changes and commit. Returns true on success, false on failure.



95
96
97
98
99
# File 'lib/carson/warehouse.rb', line 95

def pack!( message: )
	git( "add", "-A" )
	_, _, status = git( "commit", "-m", message )
	status.success?
end

#rebase_on_latest_standard!(registry: "#{bureau_address}/#{main_label}") ⇒ Object

Update the warehouse’s production standard — rebase onto latest registry state. Called after the bureau refuses a parcel for being behind standard. Returns true on success, false on failure.



88
89
90
91
# File 'lib/carson/warehouse.rb', line 88

def rebase_on_latest_standard!( registry: "#{bureau_address}/#{main_label}" )
	_, _, status = git( "rebase", registry )
	status.success?
end

#receive_latest_standard!(remote: bureau_address) ⇒ Object

Receive the latest standard from the registry after a parcel is accepted. Fast-forwards local main without switching branches. Returns true on success, false on failure.



104
105
106
107
108
109
110
# File 'lib/carson/warehouse.rb', line 104

def receive_latest_standard!( remote: bureau_address )
	_, _, status = Open3.capture3(
		"git", "-C", main_worktree_root,
		"fetch", remote, "#{main_label}:#{main_label}"
	)
	status.success?
end

#shelvesObject

All shelves (worktree paths).



115
116
117
118
119
120
# File 'lib/carson/warehouse.rb', line 115

def shelves
	output, = git( "worktree", "list", "--porcelain" )
	output.lines
		.select { it.start_with?( "worktree " ) }
		.map { it.sub( "worktree ", "" ).strip }
end

#ship(parcel, remote: bureau_address) ⇒ Object

Ship a parcel to the bureau. The warehouse sends the parcel’s label to the remote.



54
55
56
57
# File 'lib/carson/warehouse.rb', line 54

def ship( parcel, remote: bureau_address )
	_, _, status = git( "push", "-u", remote, parcel.label )
	status.success?
end

#submit_compliance!Object

Ensure the warehouse complies with company standards (template sync). Delegates to the injected compliance checker. If no checker is set, the warehouse assumes compliance — no templates to enforce. Returns a hash: { compliant: true/false, committed: true/false, error: nil/string }



79
80
81
82
83
# File 'lib/carson/warehouse.rb', line 79

def submit_compliance!
	return { compliant: true, committed: false } unless @compliance_checker

	@compliance_checker.call( self )
end