Class: Agent::Lock::Tree
- Inherits:
-
Object
- Object
- Agent::Lock::Tree
- Defined in:
- lib/agent/lock/tree.rb
Overview
The working tree a lock is about, and where its locks are kept.
Locks live in <git-common-dir>/agent-locks, which is chosen rather than
~/.agent-locks or a dotfile at the root for three reasons. Git cannot
track anything inside .git, so no repository needs a .gitignore line
and git clean -xdf cannot wipe the locks. Every worktree of a
repository resolves --git-common-dir to the same directory, so one
store serves all of them. And it dies with the checkout, instead of
outliving it in a home directory nobody thinks to sweep.
Outside a repository the store falls back to ~/.agent-locks, keyed by a
digest of the tree, since there is no .git to hide in.
Constant Summary collapse
- LOCAL_GIT_ENV =
What
git rev-parse --local-env-varslists: the variables that point git at a repository other than the one around the directory it is given. A git hook exports GIT_DIR and GIT_INDEX_FILE, soalockcalled from a pre-commit hook, with--dirnaming another checkout, resolved that checkout's root but the hook's own git dir, and kept its locks in the wrong store. Every git call here unsets them: the directory decides. %w[ GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG GIT_CONFIG_PARAMETERS GIT_CONFIG_COUNT GIT_OBJECT_DIRECTORY GIT_DIR GIT_WORK_TREE GIT_IMPLICIT_WORK_TREE GIT_GRAFT_FILE GIT_INDEX_FILE GIT_NO_REPLACE_OBJECTS GIT_REPLACE_REF_BASE GIT_PREFIX GIT_SHALLOW_FILE GIT_COMMON_DIR ].freeze
- GIT_ENV =
The environment every git call runs with: LOCAL_GIT_ENV unset.
LOCAL_GIT_ENV.to_h { |name| [name, nil] }.freeze
Instance Attribute Summary collapse
-
#root ⇒ String
readonly
Absolute, symlinks resolved, so /tmp and /private/tmp cannot become two names for one tree.
Class Method Summary collapse
- .for(dir = Dir.pwd) ⇒ Tree
-
.lock_key(repo:, branch:, root:, host:) ⇒ String
One definition, shared with Record, so that a lock read back from the store is compared on exactly the terms it was written on.
-
.normalize_remote(url) ⇒ String?
git@github.com:a/b.git,ssh://git@github.com:22/a/b, andhttps://user@github.com/a/b.gitall come out asgithub.com/a/b, and so doeshttps://me:p@ss@github.com/a/b: no piece of a password survives, even one holding an unencoded@.
Instance Method Summary collapse
-
#branch ⇒ String?
The branch checked out, nil on a detached HEAD or outside git.
-
#initialize(dir = Dir.pwd) ⇒ Tree
constructor
A new instance of Tree.
-
#lock_key ⇒ String
What two trees must share for their locks to collide: the repository and the branch.
-
#origin ⇒ String?
The origin remote as
host/owner/name, nil when the checkout has no origin or is not in git. -
#origin_slug ⇒ String?
#redis_slug, but only when it names a repository somebody could type.
-
#redis_slug ⇒ String
The repository as it should appear in a Redis key a person can read.
-
#relative(path) ⇒ String
A path the user typed, read from where they stand, as a name inside this tree.
-
#repo ⇒ String
The repository this tree is a checkout of, named the same way on every machine that has one.
-
#store_dir ⇒ String
Where the file store keeps this tree's locks:
$AGENT_LOCK_DIRwhen set to anything but blank, elseagent-locksinside the git common dir (shared by every worktree), else~/.agent-locks/<digest of the root>outside git. -
#worktree? ⇒ Boolean
A linked worktree rather than the original checkout.
-
#worktree_roots ⇒ Array<String>
Every checkout that shares this one's git common dir, and with it the lock store and the backend marker.
Constructor Details
#initialize(dir = Dir.pwd) ⇒ Tree
Returns a new instance of Tree.
49 50 51 52 53 |
# File 'lib/agent/lock/tree.rb', line 49 def initialize(dir = Dir.pwd) @dir = File.realpath(dir) @root = git("rev-parse", "--show-toplevel") || @dir @root = File.realpath(@root) end |
Instance Attribute Details
#root ⇒ String (readonly)
Returns absolute, symlinks resolved, so /tmp and /private/tmp cannot become two names for one tree.
46 47 48 |
# File 'lib/agent/lock/tree.rb', line 46 def root @root end |
Class Method Details
.for(dir = Dir.pwd) ⇒ Tree
41 |
# File 'lib/agent/lock/tree.rb', line 41 def for(dir = Dir.pwd) = new(dir) |
.lock_key(repo:, branch:, root:, host:) ⇒ String
One definition, shared with Record, so that a lock read back from the store is compared on exactly the terms it was written on.
154 155 156 157 158 |
# File 'lib/agent/lock/tree.rb', line 154 def lock_key(repo:, branch:, root:, host:) return "#{repo}@#{branch}" if repo && branch host ? "#{host}:#{root}" : root end |
.normalize_remote(url) ⇒ String?
git@github.com:a/b.git, ssh://git@github.com:22/a/b, and
https://user@github.com/a/b.git all come out as github.com/a/b,
and so does https://me:p@ss@github.com/a/b: no piece of a
password survives, even one holding an unencoded @.
Only a URL that starts with a scheme can carry a port. In the scp
form, [user@]host:path, whatever follows the colon is the path:
the port used to be stripped from both, so git@github.com:123/app
lost its all-digit owner, stopped agreeing with
https://github.com/123/app, and shared one name with every other
numeric owner's app on that host.
The whole name is lower-cased, owner and repository included. GitHub
and GitLab serve Acme/App and acme/app as one repository, and a
remote keeps the case it was cloned with: with only the host folded,
two clones of one repository had two lock keys, and each was told
it held the scope. A self-hosted server that does tell the two
apart now files both under one name, which errs toward refusing.
A remote that names a directory keeps its case: see local?.
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/agent/lock/tree.rb', line 183 def normalize_remote(url) text = url.to_s.strip return nil if text.empty? url_form = text.match?(SCHEME) local = local?(text) text = text.sub(SCHEME, "") text = text.sub(url_form ? USERINFO : SCP_USER, "") # user, and any password text = text.sub(%r{\A([^/:]+):\d*/}, '\1/') if url_form # port, which only a URL has text = text.sub(%r{\A([^/:]+):/?}, '\1/') # scp-style host:path text = text.delete_suffix("/").delete_suffix(".git").delete_suffix("/") local ? text : text.downcase end |
Instance Method Details
#branch ⇒ String?
Returns the branch checked out, nil on a detached HEAD or outside git.
92 |
# File 'lib/agent/lock/tree.rb', line 92 def branch = defined?(@branch) ? @branch : (@branch = git("symbolic-ref", "--short", "-q", "HEAD")) |
#lock_key ⇒ String
What two trees must share for their locks to collide: the repository
and the branch. Two machines on main are writing the same files, and
must see each other. Two worktrees on their own branches are not, and
are left alone, which is the point of making a worktree. A detached
HEAD has no branch to share, so it keeps to its own checkout on its
own host: the path alone is not enough, since two machines can clone
to the same path and now read one namespace.
103 |
# File 'lib/agent/lock/tree.rb', line 103 def lock_key = self.class.lock_key(repo: repo, branch: branch, root: root, host: Socket.gethostname) |
#origin ⇒ String?
Returns the origin remote as host/owner/name, nil when
the checkout has no origin or is not in git.
84 85 86 87 88 |
# File 'lib/agent/lock/tree.rb', line 84 def origin return @origin if defined?(@origin) @origin = self.class.normalize_remote(git("config", "--get", "remote.origin.url")) end |
#origin_slug ⇒ String?
#redis_slug, but only when it names a repository somebody could type. Without an origin the slug is a root commit or a path, which no other clone is filed under by name.
126 |
# File 'lib/agent/lock/tree.rb', line 126 def origin_slug = origin && redis_slug |
#redis_slug ⇒ String
The repository as it should appear in a Redis key a person can read.
A GitHub origin drops the host, so github.com/kigster/laser-cutter
is kigster/laser-cutter. Any other host stays in the label: stripping
it would let gitlab.com/kigster/laser-cutter wear the GitHub
repository's name. *?[] are flattened because this string is also a
Redis SCAN pattern, and those characters are glob syntax.
114 115 116 117 118 |
# File 'lib/agent/lock/tree.rb', line 114 def redis_slug name = repo.to_s.sub(%r{\Agithub\.com/}, "") name = "unknown" if name.empty? name.gsub(%r{[^A-Za-z0-9._/-]}, "-") end |
#relative(path) ⇒ String
A path the user typed, read from where they stand, as a name inside this tree.
There used to be a fallback to the basename for anything outside the
root, so /etc/passwd quietly locked the tree's own passwd: an agent
was told it held something it had never asked for, and the thing it had
asked for stayed unguarded. Outside is now an error.
The path is compared as typed first, then with symlinks resolved, so an
alias of the tree (/tmp for /private/tmp) is still inside it, while
an in-tree symlink that points elsewhere keeps its in-tree name, which is
what the lock is about. The path need not exist: an agent claims a file
before writing it.
240 241 242 243 244 |
# File 'lib/agent/lock/tree.rb', line 240 def relative(path) absolute = File.absolute_path(path, @dir) inside(absolute) || inside(resolve(absolute)) || raise(Scope::Invalid, "#{path} is outside the tree #{root}") end |
#repo ⇒ String
The repository this tree is a checkout of, named the same way on every machine that has one.
The absolute path used to be the name, so /Users/a/repo and
/Users/b/repo were two repositories even on one shared Redis, and two
machines never saw each other's locks. A lock is about the repository
and the files in it; where the checkout sits on somebody's disk is not
part of that. The origin URL is preferred, normalized so that ssh and
https spellings agree; then the root commit, which every clone shares
even without a remote; and only outside git, the path.
80 |
# File 'lib/agent/lock/tree.rb', line 80 def repo = @repo ||= origin || root_commit || root |
#store_dir ⇒ String
Where the file store keeps this tree's locks: $AGENT_LOCK_DIR when
set to anything but blank, else agent-locks inside the git common dir
(shared by every worktree), else ~/.agent-locks/<digest of the root>
outside git.
215 216 217 218 219 220 221 |
# File 'lib/agent/lock/tree.rb', line 215 def store_dir configured = configured_store_dir return configured if configured return File.join(common_dir, "agent-locks") if common_dir File.join(Dir.home, ".agent-locks", ::Digest::SHA256.hexdigest(root)[0, 12]) end |
#worktree? ⇒ Boolean
Returns a linked worktree rather than the original checkout.
56 |
# File 'lib/agent/lock/tree.rb', line 56 def worktree? = !git_dir.nil? && git_dir != common_dir |
#worktree_roots ⇒ Array<String>
Every checkout that shares this one's git common dir, and with it the lock store and the backend marker. Just this root outside git.
62 63 64 65 66 |
# File 'lib/agent/lock/tree.rb', line 62 def worktree_roots listed = git("worktree", "list", "--porcelain").to_s.lines .filter_map { |line| line[/\Aworktree (.+)$/, 1] } [root, *listed.map { |path| absolute(path) || path }].uniq end |