frankc
frankc

Reputation: 33

mercurial: any command or python api to get repository name

Is there any Mercurial command or Python API that could yield the repo name? This will help developing cross-repo scripts.

The only related solution that I found is to parse the .hg/hgrc [paths] section 'default' config option.

[paths]
default = ssh://server//path/tools

There must be a more elegant solution, I think.

Upvotes: 3

Views: 2792

Answers (2)

Martin Geisler
Martin Geisler

Reputation: 73768

There is no real concept of a "repository name" in Mercurial (a repository doesn't "know" or care about its own name). I think you mean "last past component of the default pull path"?

If so, then parsing the output of hg path default would be the most direct way to get that information.

However, you should note that the default path can (and often is) changed: think of cloning a local clone time for testing:

$ hg clone http://server/lib-foo
$ hg clone lib-foo lib-foo-test
$ hg clone lib-foo-test lib-foo-more-testing

The lib-foo-more-testing clone has a default push path back to lib-foo-test.

This means that parsing hg paths default wont be much more reliable than using basename $(hg root) — both can be completely different from the (base)name of the repository that was originally cloned.

If what you really want is to get an "identity" for a repository, then you should instead use

$ hg log -r 0 --template "{node}"

The first changeset hash in a repository will normally uniquely identify the repository and it will be stable even when clones change names. (If a repository has two or more roots, then the zeroth changeset can in principle differ between clones. People will have to actively try to make it differ, though.)

Upvotes: 6

Lazy Badger
Lazy Badger

Reputation: 97282

If you want to get last segment of path for remote default alias, processing output of hg path default will be better choice

If you want to get local directory name of you mercurial repository, I haven't good solution, except checking code of Notify extension (in which, after some tricks, you can get project-name)

Upvotes: 1

Related Questions