Skip to contentSkip to conversation

remote-hg: fix path when cloning with tilde expansion

PATCH In review Antoine Pelisse 3 participants 20 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 remote-hg: fix path when cloning with tilde expansion

The current code fixes the path to make it absolute when cloning, but doesn't consider tilde expansion, so that scenario fails throwing an exception because /home/myuser/~/my/repository doesn't exists:

    $ git clone hg::~/my/repository && cd repository && git fetch

Fix that by using python os.path.expanduser method.

---
 contrib/remote-helpers/git-remote-hg |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/remote-helpers/git-remote-hg +1 -1
index 02404dc..4bbd296 100755
@@ -1137,7 +1137,7 @@ def fix_path(alias, repo, orig_url):
     url = urlparse.urlparse(orig_url, 'file')
     if url.scheme != 'file' or os.path.isabs(url.path):
         return
-    abs_url = urlparse.urljoin("%s/" % os.getcwd(), orig_url)
+    abs_url = os.path.abspath(os.path.expanduser(orig_url))
     cmd = ['git', 'config', 'remote.%s.url' % alias, "hg::%s" % abs_url]
     subprocess.call(cmd)
 
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 7 lines · Antoine Pelisse

The current code fixes the path to make it absolute when cloning, but doesn't consider tilde expansion, so that scenario fails throwing an exception because /home/myuser/~/my/repository doesn't exists:

    $ git clone hg::~/my/repository && cd repository && git fetch

Fix that by using python os.path.expanduser method.

Shouldn't that be the job of the shell? (s/~/$HOME/)

Signature · 1 line
Felipe Contreras
#2 lore raw
Quoted 6 lines · Felipe Contreras
Quoted 3 lines · Antoine Pelisse
    $ git clone hg::~/my/repository && cd repository && git fetch

Fix that by using python os.path.expanduser method.

Shouldn't that be the job of the shell? (s/~/$HOME/)

I guess it is, as long as it looks like a path:

    $ echo ~
    /home/myuser
    $ echo hg::~
    hg::~
#3 lore raw

PATCH 1/1 remote-hg: fix path when cloning with tilde expansion

In reply to Felipe Contreras

The current code fixes the path to make it absolute when cloning, but doesn't consider tilde expansion, so that scenario fails throwing an exception because /home/myuser/~/my/repository doesn't exists:

    $ git clone hg::~/my/repository && cd repository && git fetch

Expand the tilde when checking if the path is absolute, so that we don't fix a path that doesn't need to be.

---

Quoted 1 line · Felipe Contreras

Shouldn't that be the job of the shell? (s/~/$HOME/)

I'm not sure what you mean here. Does it mean that I should stop cloning using "~" ? I also send this patch as I think it makes more sense to keep the ~ in the path, but just make sure we don't build invalid absolute path.

By the way, I don't exactly understand why:

    abs_url = urlparse.urljoin("%s/" % os.getcwd(), orig_url)

is done right after instead of:

abs_url = os.path.abspath(orig_url)

Cheers,
Antoine

 contrib/remote-helpers/git-remote-hg |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/remote-helpers/git-remote-hg +1 -1
index 1897327..861c498 100755
@@ -1135,7 +1135,7 @@ def do_option(parser):
 
 def fix_path(alias, repo, orig_url):
     url = urlparse.urlparse(orig_url, 'file')
-    if url.scheme != 'file' or os.path.isabs(url.path):
+    if url.scheme != 'file' or os.path.isabs(os.path.expanduser(url.path)):
         return
     abs_url = urlparse.urljoin("%s/" % os.getcwd(), orig_url)
     cmd = ['git', 'config', 'remote.%s.url' % alias, "hg::%s" % abs_url]

--
1.7.9.5

#4 lore raw
Quoted 9 lines · Antoine Pelisse

The current code fixes the path to make it absolute when cloning, but doesn't consider tilde expansion, so that scenario fails throwing an exception because /home/myuser/~/my/repository doesn't exists:

    $ git clone hg::~/my/repository && cd repository && git fetch

Expand the tilde when checking if the path is absolute, so that we don't fix a path that doesn't need to be.

Quoted 6 lines

---

Quoted 1 line · Felipe Contreras

Shouldn't that be the job of the shell? (s/~/$HOME/)

I'm not sure what you mean here. Does it mean that I should stop cloning using "~" ?

I think shells do not expand ~ when it appears in a string (e.g. hg::~/there); you could work it around with

	git clone hg::$(echo ~/there)

and I suspect that is what Felipe is alluding to. A tool (like remote-hg bridge with this patch) that expands ~ in the middle of a string also may be surprising to some people, especially to those who know the shell does not.

Quoted 11 lines

I also send this patch as I think it makes more sense to keep the ~ in the path, but just make sure we don't build invalid absolute path.

By the way, I don't exactly understand why:

    abs_url = urlparse.urljoin("%s/" % os.getcwd(), orig_url)

is done right after instead of:

abs_url = os.path.abspath(orig_url)

That looks like a good cleanup to me, too, but I may be missing some subtle points...

By the way, you earlier sent an updated 1/2; is this supposed to be 2/2 to conclude the two-patch series?

Quoted 21 lines

Cheers,
Antoine

 contrib/remote-helpers/git-remote-hg |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/remote-helpers/git-remote-hg +1 -2
index 1897327..861c498 100755
@@ -1135,7 +1135,7 @@ def do_option(parser):
 
 def fix_path(alias, repo, orig_url):
     url = urlparse.urlparse(orig_url, 'file')
-    if url.scheme != 'file' or os.path.isabs(url.path):
+    if url.scheme != 'file' or os.path.isabs(os.path.expanduser(url.path)):
         return
     abs_url = urlparse.urljoin("%s/" % os.getcwd(), orig_url)
     cmd = ['git', 'config', 'remote.%s.url' % alias, "hg::%s" % abs_url]
--

1.7.9.5

#5 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 4 lines · Antoine Pelisse
Quoted 1 line · Felipe Contreras

Shouldn't that be the job of the shell? (s/~/$HOME/)

I'm not sure what you mean here. Does it mean that I should stop cloning using "~" ?

I think shells do not expand ~ when it appears in a string (e.g. hg::~/there); you could work it around with

Quoted 6 lines
        git clone hg::$(echo ~/there)

and I suspect that is what Felipe is alluding to. A tool (like remote-hg bridge with this patch) that expands ~ in the middle of a string also may be surprising to some people, especially to those who know the shell does not.

It looks like mercurial will expand the tilde (it it starts with it):

hg init \~

will create a $HOME/.hg. (while git init \~ will create ./~).

So when we run:

git clone hg::~/my/repo

Git will remove the "hg::" part, and Mercurial will expand tilde and clone $HOME/my/repo.

So what should we do ? I think we should stick as close as possible to Hg behavior:
That is consider that a path starting with tilde is absolute, and not try to fix it by building /home/user/~/repo/path.
Of course if we could not depend on "I think Hg works like that", it would be better if we could resolve that by asking Mercurial. I will dig into it.

Quoted 2 lines

By the way, you earlier sent an updated 1/2; is this supposed to be 2/2 to conclude the two-patch series?

Those two patches don't interact with each other, but you can of course join them if it makes it easier for you (and I don't think one is going to have to go "faster" than the other anyway).

#6 lore raw
Quoted 6 lines · Antoine Pelisse

So when we run:

git clone hg::~/my/repo

Git will remove the "hg::" part, and Mercurial will expand tilde and clone $HOME/my/repo.

Now you confused me. If the implementation were for us to remove the hg:: prefix and let Mercurial do whatever it wants to do with the rest, you are right that we will not have to do any expansion like your patch. But you sent a patch to do so, so apparently it is not what happens. So where does it go wrong?

Puzzled...

Quoted 6 lines
Quoted 2 lines

By the way, you earlier sent an updated 1/2; is this supposed to be 2/2 to conclude the two-patch series?

Those two patches don't interact with each other, but you can of course join them if it makes it easier for you (and I don't think one is going to have to go "faster" than the other anyway).

Hmph, so there is a different 2/2 that we haven't seen recently on the list (meaning you have three patches)?

Thanks.

#7 lore raw

Confusion everywhere :-)

Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Antoine Pelisse

So when we run:

git clone hg::~/my/repo

Git will remove the "hg::" part, and Mercurial will expand tilde and clone $HOME/my/repo.

Quoted 7 lines

Now you confused me. If the implementation were for us to remove the hg:: prefix and let Mercurial do whatever it wants to do with the rest, you are right that we will not have to do any expansion like your patch. But you sent a patch to do so, so apparently it is not what happens. So where does it go wrong?

Puzzled...

OK, I think I see why you are puzzled.

Cloning works fine because we "fix the path" *after* the clone is done successfully, for the following reason:
If you run:

git clone hg::./my_repo my_new_repo

The remote path will be hg::./my_repo, so we have to fix this path (otherwise you won't be able to run git fetch from inside my_new_repo). It's currently done by checking if ./my_repo is an absolute path or not, and try to make it absolute if required.

But my issue is when I do that:

git clone hg::~/my_repo my_new_repo

The clone works successfully by cloning $HOME/my_repo, but then, when we try to fix the repo path, we think that ~/my_repo is not an absolute path, so we make it absolute: /home/user/~/my_repo which is now off. So I'm not able to fetch that remote.

What the current patch does, is to expand the tilde before checking if the path is absolute. So that fixes the bug, but that indeed can be confusing to another user that would expect hg::~/my_repo/ to *not be* hg::$HOME/my_repo (because he knows the expansion should not happen in that case).

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

By the way, you earlier sent an updated 1/2; is this supposed to be 2/2 to conclude the two-patch series?

Those two patches don't interact with each other, but you can of course join them if it makes it easier for you (and I don't think one is going to have to go "faster" than the other anyway).

Hmph, so there is a different 2/2 that we haven't seen recently on the list (meaning you have three patches)?

I have 2 patch (1 from me, 1 from Felipe):
One with the tilde expansion, the other one with shared_path initialization (which now conflicts with the resend from Felipe)

I will try to provide a better versioning of the patches next time.

Sorry for the confusion,
Thanks,

#8 lore raw
Quoted 10 lines · Antoine Pelisse

OK, I think I see why you are puzzled.
...
But my issue is when I do that:

git clone hg::~/my_repo my_new_repo

The clone works successfully by cloning $HOME/my_repo, but then, when we try to fix the repo path, we think that ~/my_repo is not an absolute path, so we make it absolute: /home/user/~/my_repo which is now off. So I'm not able to fetch that remote.

OK, so clone works, but subsequent fetch from the cloned resoitory does not? "git fetch hg::~/my_repo" will still work but the call to "git config" done near the place your patch touches does not store "hg::~/my_repo" because it thinks "~/my_repo" refers to "./~/my_repo" and tries to come up with an absolute path. The patch tries to notice this case and return without rewriting, so that remote.*.url is kept as "hg::~/my_repo".

Assuming that I am following your reasoning so far, I think I can agree with the patch (not that my agreement matters that much, as you seem to be a lot more familiar with this codepath).

Thanks for explaining.

#9 lore raw
Quoted 8 lines · Junio C Hamano

OK, so clone works, but subsequent fetch from the cloned resoitory does not? "git fetch hg::~/my_repo" will still work but the call to "git config" done near the place your patch touches does not store "hg::~/my_repo" because it thinks "~/my_repo" refers to "./~/my_repo" and tries to come up with an absolute path. The patch tries to notice this case and return without rewriting, so that remote.*.url is kept as "hg::~/my_repo".

Quoted 5 lines

Assuming that I am following your reasoning so far, I think I can agree with the patch (not that my agreement matters that much, as you seem to be a lot more familiar with this codepath).

Thanks for explaining.

Thanks

#10 lore raw
In reply to Antoine Pelisse
Quoted 18 lines · Antoine Pelisse

Confusion everywhere :-)

Quoted 15 lines · Junio C Hamano
Quoted 6 lines · Antoine Pelisse

So when we run:

git clone hg::~/my/repo

Git will remove the "hg::" part, and Mercurial will expand tilde and clone $HOME/my/repo.

Now you confused me. If the implementation were for us to remove the hg:: prefix and let Mercurial do whatever it wants to do with the rest, you are right that we will not have to do any expansion like your patch. But you sent a patch to do so, so apparently it is not what happens. So where does it go wrong?

Quoted 6 lines
Quoted 1 line

Puzzled...

OK, I think I see why you are puzzled.

Cloning works fine because we "fix the path" *after* the clone is done successfully, for the following reason:

So if we didn't store a different path, it would work. So instead of expanding '~' ourselves, it would be better to don't expand anything, and leave it as it is, but how to detect that in fix_path()?

Signature · 1 line
Felipe Contreras
#11 lore raw
Quoted 8 lines · Felipe Contreras
Quoted 4 lines

OK, I think I see why you are puzzled.

Cloning works fine because we "fix the path" *after* the clone is done successfully, for the following reason:

So if we didn't store a different path, it would work. So instead of expanding '~' ourselves, it would be better to don't expand anything, and leave it as it is, but how to detect that in fix_path()?

I think that the patch relies on that os.path.expanduser(), if url.path is such a path that begins with "~" (or "~whom"), returns an absolute path. When given an absolute path, or "~whom/path", fix_path returns without running 'git config' on remote.<alias>.url configuration.

Presumably this "git config" is to "fix" what is already there, and in the case where the path is already absolute
(e.g. "/home/ap/hgrepo" as opposed to "~ap/hgrepo") the resulting repository has a correct value for the variable set already without the need to fix it (that is why the original code just returns from the function), so doing the same for "~whom" case with this patch should leave the setting, which presumably is "hg::~ap/hgrepo"?

#12 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Felipe Contreras
Quoted 4 lines

OK, I think I see why you are puzzled.

Cloning works fine because we "fix the path" *after* the clone is done successfully, for the following reason:

So if we didn't store a different path, it would work. So instead of expanding '~' ourselves, it would be better to don't expand anything, and leave it as it is, but how to detect that in fix_path()?

Quoted 5 lines

I think that the patch relies on that os.path.expanduser(), if url.path is such a path that begins with "~" (or "~whom"), returns an absolute path. When given an absolute path, or "~whom/path", fix_path returns without running 'git config' on remote.<alias>.url configuration.

I think ~whom/path would run 'git config'.

Signature · 1 line
Felipe Contreras
#13 lore raw
Quoted 12 lines · Felipe Contreras
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Felipe Contreras
Quoted 4 lines

OK, I think I see why you are puzzled.

Cloning works fine because we "fix the path" *after* the clone is done successfully, for the following reason:

So if we didn't store a different path, it would work. So instead of expanding '~' ourselves, it would be better to don't expand anything, and leave it as it is, but how to detect that in fix_path()?

Quoted 7 lines
Quoted 5 lines

I think that the patch relies on that os.path.expanduser(), if url.path is such a path that begins with "~" (or "~whom"), returns an absolute path. When given an absolute path, or "~whom/path", fix_path returns without running 'git config' on remote.<alias>.url configuration.

I think ~whom/path would run 'git config'.

Hmph, do you mean the third example of this?

        $ python
        >>> import os
        >>> os.path.expanduser("~/repo")
        '/home/junio/repo'
        >>> os.path.expanduser("~junio/repo")
        '/home/junio/repo'
        >>> os.path.expanduser("~felipe/repo")
        '~felipe/repo'

which will give "~felipe/repo" that is _not_ an absolute repository because no such user exists on this box?

It is true that in that case fix_path() will not return early and will throw a bogus path at "git config", but if the "~whom" does not resolve to an existing home directory of a user, I am not sure what we can do better than what Antoine's patch does.

#14 lore raw
Quoted 33 lines · Junio C Hamano
Quoted 12 lines · Felipe Contreras
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Felipe Contreras
Quoted 4 lines

OK, I think I see why you are puzzled.

Cloning works fine because we "fix the path" *after* the clone is done successfully, for the following reason:

So if we didn't store a different path, it would work. So instead of expanding '~' ourselves, it would be better to don't expand anything, and leave it as it is, but how to detect that in fix_path()?

Quoted 7 lines
Quoted 5 lines

I think that the patch relies on that os.path.expanduser(), if url.path is such a path that begins with "~" (or "~whom"), returns an absolute path. When given an absolute path, or "~whom/path", fix_path returns without running 'git config' on remote.<alias>.url configuration.

I think ~whom/path would run 'git config'.

Hmph, do you mean the third example of this?

        $ python
        >>> import os
        >>> os.path.expanduser("~/repo")
        '/home/junio/repo'
        >>> os.path.expanduser("~junio/repo")
        '/home/junio/repo'
        >>> os.path.expanduser("~felipe/repo")
        '~felipe/repo'
Quoted 7 lines

which will give "~felipe/repo" that is _not_ an absolute repository because no such user exists on this box?

It is true that in that case fix_path() will not return early and will throw a bogus path at "git config", but if the "~whom" does not resolve to an existing home directory of a user, I am not sure what we can do better than what Antoine's patch does.

I was thinking something like this:

if url.scheme != 'file' or os.path.isabs(url.path) or url.path[0] == '~':
return

Signature · 1 line
Felipe Contreras
#15 lore raw
Quoted 19 lines · Felipe Contreras
Quoted 11 lines

Hmph, do you mean the third example of this?

        $ python
        >>> import os
        >>> os.path.expanduser("~/repo")
        '/home/junio/repo'
        >>> os.path.expanduser("~junio/repo")
        '/home/junio/repo'
        >>> os.path.expanduser("~felipe/repo")
        '~felipe/repo'
Quoted 7 lines

which will give "~felipe/repo" that is _not_ an absolute repository because no such user exists on this box?

It is true that in that case fix_path() will not return early and will throw a bogus path at "git config", but if the "~whom" does not resolve to an existing home directory of a user, I am not sure what we can do better than what Antoine's patch does.

Quoted 4 lines

I was thinking something like this:

if url.scheme != 'file' or os.path.isabs(url.path) or url.path[0] == '~':
return

That did cross my mind.

I know ~/ and ~who/ are expanded on UNIXy systems, and I read in Python documentation that Python on Windows treats ~/ and ~who/ the same way as on UNIXy systems, so the "begins with ~" test would work on both systems. But it is probably a better design to outsource that knowledge to os.path.expanduser(), with the emphasis on "os." part of that function. That way, we do not even have to care about such potential platform specifics, which is a big plus. The only possible difference that approach makes is the above example of naming a non-existent ~user, but that will not work anyway, so...

#16 lore raw
Quoted 28 lines · Junio C Hamano
Quoted 19 lines · Felipe Contreras
Quoted 11 lines

Hmph, do you mean the third example of this?

        $ python
        >>> import os
        >>> os.path.expanduser("~/repo")
        '/home/junio/repo'
        >>> os.path.expanduser("~junio/repo")
        '/home/junio/repo'
        >>> os.path.expanduser("~felipe/repo")
        '~felipe/repo'
Quoted 7 lines

which will give "~felipe/repo" that is _not_ an absolute repository because no such user exists on this box?

It is true that in that case fix_path() will not return early and will throw a bogus path at "git config", but if the "~whom" does not resolve to an existing home directory of a user, I am not sure what we can do better than what Antoine's patch does.

Quoted 4 lines

I was thinking something like this:

if url.scheme != 'file' or os.path.isabs(url.path) or url.path[0] == '~':
return

That did cross my mind.

Quoted 9 lines

I know ~/ and ~who/ are expanded on UNIXy systems, and I read in Python documentation that Python on Windows treats ~/ and ~who/ the same way as on UNIXy systems, so the "begins with ~" test would work on both systems. But it is probably a better design to outsource that knowledge to os.path.expanduser(), with the emphasis on "os." part of that function. That way, we do not even have to care about such potential platform specifics, which is a big plus. The only possible difference that approach makes is the above example of naming a non-existent ~user, but that will not work anyway, so...

We would be doing better than os.path.expanduser(), because if Mercurial somehow decided to treat ~ differently, our code would still work just fine. If we do os.path.expanduser(), then we are not outsourcing anything, we are taking ownership and fixing the path by ourselves and dealing with all the consequences.

If I clone ~/git, and then change my username, and move my home directory, doing a 'git fetch' in ~/git wouldn't work anymore, because we have expanded the path and fixed it to my old home, if instead we simply return without fixing, it would still work just fine.

Signature · 1 line
Felipe Contreras
#17 lore raw
Quoted 4 lines · Felipe Contreras

If I clone ~/git, and then change my username, and move my home directory, doing a 'git fetch' in ~/git wouldn't work anymore, because we have expanded the path and fixed it to my old home, if instead we simply return without fixing, it would still work just fine.

Antoine's patch runs expanduser() only to see if the given one gets modified to absolute path, and makes fix_path() return without calling the extra 'git config', so it is my understanding that the above describes exactly what the patch does. Am I reading the patch incorrectly?

It outsources the determination of "is this a special notation to name a home directory?" logic to .isabs(.expanduser()) instead of doing a .beginswith('~') ourselves.

#18 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Felipe Contreras

If I clone ~/git, and then change my username, and move my home directory, doing a 'git fetch' in ~/git wouldn't work anymore, because we have expanded the path and fixed it to my old home, if instead we simply return without fixing, it would still work just fine.

Quoted 5 lines

Antoine's patch runs expanduser() only to see if the given one gets modified to absolute path, and makes fix_path() return without calling the extra 'git config', so it is my understanding that the above describes exactly what the patch does. Am I reading the patch incorrectly?

Antoine's *second* patch, which I missed, does that, yeah. That should work fine.

Signature · 1 line
Felipe Contreras
#19 lore raw
Quoted 8 lines · Felipe Contreras
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Felipe Contreras

If I clone ~/git, and then change my username, and move my home directory, doing a 'git fetch' in ~/git wouldn't work anymore, because we have expanded the path and fixed it to my old home, if instead we simply return without fixing, it would still work just fine.

Quoted 8 lines
Quoted 5 lines

Antoine's patch runs expanduser() only to see if the given one gets modified to absolute path, and makes fix_path() return without calling the extra 'git config', so it is my understanding that the above describes exactly what the patch does. Am I reading the patch incorrectly?

Antoine's *second* patch, which I missed, does that, yeah. That should work fine.

OK Cool,
Thank you both,