hg-root
Find Mercurial repository's root directory
TLDR
Display the root location of the current repository
Display the root location of the specified repository
SYNOPSIS
hg-root
DESCRIPTION
The `hg-root` command, typically provided as part of the Mercurial (hg) version control system, is a utility designed to quickly determine the root directory of the current Mercurial repository. It traverses the file system upwards from the current working directory until it finds the `.hg` subdirectory, which signifies the repository's root. If found, the absolute path to that directory is printed to standard output. If no `.hg` directory is found in the current directory or any of its parent directories, the command exits without printing anything. This tool is invaluable for scripts and automated tasks that need to reliably locate the repository root, regardless of the script's current working directory. It avoids the need for complex directory traversal logic within those scripts, simplifying repository interactions. The command is primarily used for scripting and automation purposes. It provides a reliable and standardized way to identify the top level directory of an hg repo.
CAVEATS
The command only returns the repository root if executed from within a Mercurial repository or one of its subdirectories. It doesn't create a repository if one doesn't exist. If no `.hg` directory is found, it will exit silently without an error message.
USAGE EXAMPLES
Example 1: From within a Mercurial repository:
hg-root
Output: /path/to/repository
Example 2: From outside a Mercurial repository:
hg-root
Output: (No output).
Example 3: Using in a script:
REPO_ROOT=$(hg-root)
if [ -n "$REPO_ROOT" ]; then
cd "$REPO_ROOT"
echo "Repository root: $REPO_ROOT"
else
echo "Not in a Mercurial repository."
fi
HISTORY
The `hg-root` command was developed as part of the Mercurial version control system. It's been present since the early versions of Mercurial to assist in script automation and developer tools that require knowing the repository's root location.