LinuxCommandLibrary

hg-root

Find Mercurial repository's root directory

TLDR

Display the root location of the current repository

$ hg root
copy

Display the root location of the specified repository
$ hg root --cwd [path/to/directory]
copy

SYNOPSIS

hg root [REPO]

PARAMETERS

REPO
    An optional path to a Mercurial repository. If not specified, the command uses the current working directory to find the repository root.

DESCRIPTION

The `hg root` command is a Mercurial utility that identifies and prints the absolute path to the root directory of the current or specified Mercurial repository. When executed within a repository's working copy or a subdirectory thereof, it outputs the path to the top-level directory containing the `.hg` metadata directory. If an optional REPO argument is provided, the command will attempt to find the root of that specific repository.

This command is particularly useful for scripting, navigation, or simply confirming your current repository's base location. It helps developers quickly ascertain where a repository begins, which is fundamental for understanding the scope of version control operations and ensuring commands are executed from the correct context.

CAVEATS

The command will fail and report an error if executed in a directory that is not part of a Mercurial repository, or if the specified REPO path does not point to a valid Mercurial repository.

USE CASES

`hg root` is frequently used in shell scripts to build paths relative to the repository root, or to `cd` into the root directory before executing other Mercurial commands. It's also helpful for debugging when you're unsure if you're in a Mercurial repository or where its base is located, providing a quick confirmation of your current version control context.

HISTORY

The `hg root` command has been a fundamental part of Mercurial (hg) since its early days. As a distributed version control system, Mercurial relies on clear identification of repository boundaries. `hg root` was developed to provide a simple, programmatic way to locate the `.hg` directory, which defines the repository's root. Its design emphasizes simplicity and consistency, reflecting Mercurial's overall philosophy. It has remained a stable and essential utility throughout Mercurial's development history, serving as a core component for navigating and scripting within repositories.

SEE ALSO

hg identify(1), hg status(1), hg init(1), pwd(1)

Copied to clipboard