3

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.

gavinb
  • 19,278
  • 3
  • 45
  • 60
frankc
  • 33
  • 1
  • 4

2 Answers2

6

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.)

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
  • This explains why it seems so basic but no obvious solution that I can found. Thanks! Super answer! – frankc Jul 07 '13 at 03:46
1

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)

Community
  • 1
  • 1
Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
  • 1
    You can use the `hg root` command to return the local directory name, and use `basename` to extract the last component. So if your project is in `/Users/jrh/code/halo7`, this would yield `halo7`. – gavinb Jul 06 '13 at 02:18
  • hg root returns local path/dir, which could be completely different from the repo name. – frankc Jul 06 '13 at 03:39
  • @gavinb - what about Winboys, which haven't basename on the board? – Lazy Badger Jul 06 '13 at 05:39
  • @frankc As Martin points out above, there is no canonical definition of a unique repo name per se; it is only by convention and is usually the repo name. – gavinb Jul 08 '13 at 00:25
  • @LazyBadger You can always install CygWin to get a complete suite *nix of command-line tools on Windows. Otherwise, you might be able to use some of the %~ features in DOS shell (see http://www.computerhope.com/forhlp.htm). – gavinb Jul 08 '13 at 00:27
  • @gavinb - say it admin in the Corporate Network, ok?! Forget about home-users, think about business-environment – Lazy Badger Jul 08 '13 at 01:06
  • @LazyBadger Nothing in the question (or the phrase "Winboys") implies the context was only for Corporate Networks. Also, Plenty of non-home users use Cygwin. Anyway, as per the reference I posted, you can use the command: "echo %~n1" to extract the base name of the path parameter passed. – gavinb Jul 08 '13 at 01:37