Class: Agent::Lock::Scope
- Inherits:
-
Object
- Object
- Agent::Lock::Scope
- Defined in:
- lib/agent/lock/scope.rb
Overview
What a lock covers: one path, or a glob over many.
** is the whole tree, workflow/** a corner of it, lib/a.rb a single
file. A directory is taken to mean everything under it, since an agent
that says it is working in docs is not promising to leave docs/api
alone.
Two scopes conflict when either one's fixed part contains the other's.
workflow/** and workflow/lib/cli.rb conflict; docs/** and
workflow/** do not. Comparing the fixed parts rather than trying to
intersect two globs is deliberate: glob intersection has answers nobody
can predict, and the failure it would buy is two agents editing one file.
This errs the other way, toward refusing work that might have been safe.
Names are compared with their case folded, on every filesystem. APFS and
NTFS find README.md under readme.md too, so compared byte for byte
those were two scopes that never met, and two agents each held "the"
file. Asking the checkout's filesystem instead was rejected: a Mac and
a Linux box sharing one Redis would then disagree about whether a lock
is in the way, and the one that cannot see it writes anyway. On a
filesystem that does keep two such files apart, a lock on one now
stands in front of both, which is the same error in the safe direction.
Only the comparison folds. The pattern stays as it was typed, and so does the lock id made from it, which is what existing locks are filed under.
Defined Under Namespace
Classes: Invalid
Constant Summary collapse
- ALL =
The glob that claims the whole tree.
"**"- WHOLE_TREE =
The spellings of the whole tree, taken literally wherever the agent stands.
[".", "*", ALL].freeze
- EMPTY =
The refusal for an empty scope, which is usually an unset variable.
"empty scope: pass ** to claim the whole tree"
Instance Attribute Summary collapse
-
#pattern ⇒ String
readonly
The pattern, relative to the tree root.
Class Method Summary collapse
-
.directory?(text, tree) ⇒ Boolean
A plain path naming a directory that exists.
-
.glob?(text) ⇒ Boolean
Whether it has a wildcard anywhere in it.
-
.parse(path, tree:) ⇒ Scope
What the user typed, as a pattern relative to the tree root.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
The same pattern, letter for letter.
- #conflicts_with?(other) ⇒ Boolean
-
#covers?(other) ⇒ Boolean
Whether holding this scope already means holding
other, which is a stricter question than whether the two overlap. -
#fixed_part ⇒ String
The leading segments with no wildcard in them, which is the deepest directory a pattern is certainly confined to.
-
#folded_fixed_part ⇒ String
protected
#fixed_part as it is compared.
-
#folded_pattern ⇒ String
protected
:foldrather than a plain downcase, since it is what comparing without case means past ASCII: it is the foldingcasecmp?does. -
#initialize(pattern) ⇒ Scope
constructor
A new instance of Scope.
-
#recursive? ⇒ Boolean
The whole tree, or everything under one directory.
-
#same_as?(other) ⇒ Boolean
Whether
otheris this claim, whatever case either was typed in. -
#slug ⇒ String
Safe to use in a filename.
-
#to_s ⇒ String
The normalized pattern, relative to the tree root.
Constructor Details
#initialize(pattern) ⇒ Scope
Returns a new instance of Scope.
127 128 129 |
# File 'lib/agent/lock/scope.rb', line 127 def initialize(pattern) @pattern = pattern.to_s.squeeze("/").delete_prefix("./") end |
Instance Attribute Details
#pattern ⇒ String (readonly)
Returns the pattern, relative to the tree root.
51 52 53 |
# File 'lib/agent/lock/scope.rb', line 51 def pattern @pattern end |
Class Method Details
.directory?(text, tree) ⇒ Boolean
Returns a plain path naming a directory that exists.
85 86 87 88 89 |
# File 'lib/agent/lock/scope.rb', line 85 def directory?(text, tree) return false if glob?(text) File.directory?(File.join(tree.root, text)) end |
.glob?(text) ⇒ Boolean
Returns whether it has a wildcard anywhere in it.
80 |
# File 'lib/agent/lock/scope.rb', line 80 def glob?(text) = text.match?(/[*?\[{]/) |
.parse(path, tree:) ⇒ Scope
What the user typed, as a pattern relative to the tree root.
An empty scope used to mean the whole tree, so alock acquire "$SCOPE"
with the variable unset claimed everything, and nobody asked for
that. Globs used to skip the tree entirely, so ../x/** was stored
verbatim and /abs/tree/lib/** never met lib/**. Both are read
the way a plain path is now.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/agent/lock/scope.rb', line 66 def parse(path, tree:) text = path.to_s.strip raise Invalid, EMPTY if text.empty? return new(ALL) if WHOLE_TREE.include?(text) text = glob?(text) ? relative_glob(text, tree) : tree.relative(text) return new(ALL) if text == "." text = "#{text}/#{ALL}" if directory?(text, tree) new(text) end |
Instance Method Details
#==(other) ⇒ Boolean
Returns the same pattern, letter for letter. The same claim in another case is #same_as?.
193 |
# File 'lib/agent/lock/scope.rb', line 193 def ==(other) = other.is_a?(Scope) && pattern == other.pattern |
#conflicts_with?(other) ⇒ Boolean
141 142 143 144 145 |
# File 'lib/agent/lock/scope.rb', line 141 def conflicts_with?(other) return true if same_as?(other) contains?(folded_fixed_part, other.folded_fixed_part) || contains?(other.folded_fixed_part, folded_fixed_part) end |
#covers?(other) ⇒ Boolean
Whether holding this scope already means holding other, which is a
stricter question than whether the two overlap. Only a scope that
takes everything under a directory can promise that; a partial glob
such as lib/*.rb covers nothing but itself, since working out what
else it matches is the guessing #conflicts_with? refuses to do.
159 160 161 162 163 |
# File 'lib/agent/lock/scope.rb', line 159 def covers?(other) return true if same_as?(other) recursive? && contains?(folded_fixed_part, other.folded_fixed_part) end |
#fixed_part ⇒ String
The leading segments with no wildcard in them, which is the deepest directory a pattern is certainly confined to.
135 136 137 |
# File 'lib/agent/lock/scope.rb', line 135 def fixed_part @fixed_part ||= pattern.split("/").take_while { |part| !self.class.glob?(part) }.join("/") end |
#folded_fixed_part ⇒ String (protected)
Returns #fixed_part as it is compared.
204 |
# File 'lib/agent/lock/scope.rb', line 204 def folded_fixed_part = @folded_fixed_part ||= fixed_part.downcase(:fold) |
#folded_pattern ⇒ String (protected)
:fold rather than a plain downcase, since it is what comparing
without case means past ASCII: it is the folding casecmp? does.
201 |
# File 'lib/agent/lock/scope.rb', line 201 def folded_pattern = @folded_pattern ||= pattern.downcase(:fold) |
#recursive? ⇒ Boolean
Returns the whole tree, or everything under one directory.
178 |
# File 'lib/agent/lock/scope.rb', line 178 def recursive? = [ALL, "#{fixed_part}/#{ALL}"].include?(pattern) |
#same_as?(other) ⇒ Boolean
Whether other is this claim, whatever case either was typed in. A
looser question than #==, which is asked of the text: a lock is
stored, and filed, under the spelling its holder used.
175 |
# File 'lib/agent/lock/scope.rb', line 175 def same_as?(other) = folded_pattern == other.folded_pattern |
#slug ⇒ String
Returns safe to use in a filename.
181 182 183 184 185 |
# File 'lib/agent/lock/scope.rb', line 181 def slug text = pattern.gsub("**", "all").gsub(%r{[^A-Za-z0-9._/-]}, "").tr("/", "-").squeeze("-") text = text.delete_prefix("-").delete_suffix("-") text.empty? ? "tree" : text[0, 60] end |
#to_s ⇒ String
Returns the normalized pattern, relative to the tree root.
188 |
# File 'lib/agent/lock/scope.rb', line 188 def to_s = pattern |