+@functools.lru_cache(maxsize=None)
+def cherry_pick_inprogress():
+ """Determine whether a cherry-pick is in progress."""
+ output = git(['rev-parse', '--git-path', 'CHERRY_PICK_HEAD']).stdout.strip()
+ return Path(output).exists()
+
+
+@functools.lru_cache(maxsize=None)
+def top_dir() -> Path:
+ """Find the top dir of the git checkout."""
+ output = git(['rev-parse', '--show-toplevel'], stderr=subprocess.PIPE).stdout.strip()
+ return Path(output).resolve()
+
+
+@functools.lru_cache(maxsize=None)
+def git_dir() -> Path:
+ """Find the internal git dir for this project."""
+ output = git(['rev-parse', '--git-dir']).stdout.strip()
+ return Path(output).resolve()
+
+
+@functools.lru_cache(maxsize=None)
+def worktree_is_local(worktree: str) -> bool:
+ """See whether |worktree| is the cwd git repo."""
+ if not worktree:
+ return True
+
+ # If .git is a symlink, worktree result might be the target.
+ if worktree == str(git_dir().resolve()):
+ return True
+
+ # NB: worktree path is supposed to be absolute from for-each-ref, but it's
+ # not always, so we have to resolve it. https://crbug.com/git/88
+ worktree = (git_dir() / worktree).resolve()
+ return worktree == top_dir()
+
+