Repository: pypa/pip

Issue:
pip freeze changed behaviour in 8.1.0
- Pip version: >=8.1.0
- Python version: any
- Operating System: any
### Description:

https://github.com/pypa/pip/commit/0c221076e54cd510b51c4d92374c44d7664d5516 introduced a regression in the sense that pip freeze now returns a different remote than before even when origin is present. This breaks existing workflows.
### What I've run:

Before: pip freeze returned the origin remote
After: pip freeze return the "first" remote

Is there a chance to have the new code trigger only when origin is not found?

cc/ @hashar 

pip freeze changed behaviour in 8.1.0
- Pip version: >=8.1.0
- Python version: any
- Operating System: any
### Description:

https://github.com/pypa/pip/commit/0c221076e54cd510b51c4d92374c44d7664d5516 introduced a regression in the sense that pip freeze now returns a different remote than before even when origin is present. This breaks existing workflows.
### What I've run:

Before: pip freeze returned the origin remote
After: pip freeze return the "first" remote

Is there a chance to have the new code trigger only when origin is not found?

cc/ @hashar 



Discussion:
> Is there a chance to have the new code trigger only when origin is not found?

Since `origin` is the default remote name, it could make sense. PR welcome ;)

> Is there a chance to have the new code trigger only when origin is not found?

Since `origin` is the default remote name, it could make sense. PR welcome ;)


Resolving patch:
diff --git a/pip/vcs/git.py b/pip/vcs/git.py
--- a/pip/vcs/git.py
+++ b/pip/vcs/git.py
@@ -139,8 +139,13 @@ def get_url(self, location):
         remotes = self.run_command(
             ['config', '--get-regexp', 'remote\..*\.url'],
             show_stdout=False, cwd=location)
-        first_remote = remotes.splitlines()[0]
-        url = first_remote.split(' ')[1]
+        remotes = remotes.splitlines()
+        found_remote = remotes[0]
+        for remote in remotes:
+            if remote.startswith('remote.origin.url '):
+                found_remote = remote
+                break
+        url = found_remote.split(' ')[1]
         return url.strip()
 
     def get_revision(self, location):


Test patch:
diff --git a/tests/functional/test_freeze.py b/tests/functional/test_freeze.py
--- a/tests/functional/test_freeze.py
+++ b/tests/functional/test_freeze.py
@@ -193,6 +193,59 @@ def test_freeze_git_clone_srcdir(script, tmpdir):
     _check_output(result.stdout, expected)
 
 
+@pytest.mark.git
+def test_freeze_git_remote(script, tmpdir):
+    """
+    Test freezing a Git clone.
+    """
+    # Returns path to a generated package called "version_pkg"
+    pkg_version = _create_test_package(script)
+
+    result = script.run(
+        'git', 'clone', pkg_version, 'pip-test-package',
+        expect_stderr=True,
+    )
+    repo_dir = script.scratch_path / 'pip-test-package'
+    result = script.run(
+        'python', 'setup.py', 'develop',
+        cwd=repo_dir,
+        expect_stderr=True,
+    )
+    origin_remote = pkg_version
+    other_remote = pkg_version + '-other'
+    # check frozen remote after clone
+    result = script.pip('freeze', expect_stderr=True)
+    expected = textwrap.dedent(
+        """
+            ...-e git+{remote}@...#egg=version_pkg
+            ...
+        """
+    ).format(remote=origin_remote).strip()
+    _check_output(result.stdout, expected)
+    # check frozen remote when there is no remote named origin
+    script.run('git', 'remote', 'remove', 'origin', cwd=repo_dir)
+    script.run('git', 'remote', 'add', 'other', other_remote, cwd=repo_dir)
+    result = script.pip('freeze', expect_stderr=True)
+    expected = textwrap.dedent(
+        """
+            ...-e git+{remote}@...#egg=version_pkg
+            ...
+        """
+    ).format(remote=other_remote).strip()
+    _check_output(result.stdout, expected)
+    # when there are more than one origin, priority is given to the
+    # remote named origin
+    script.run('git', 'remote', 'add', 'origin', origin_remote, cwd=repo_dir)
+    result = script.pip('freeze', expect_stderr=True)
+    expected = textwrap.dedent(
+        """
+            ...-e git+{remote}@...#egg=version_pkg
+            ...
+        """
+    ).format(remote=origin_remote).strip()
+    _check_output(result.stdout, expected)
+
+
 @pytest.mark.mercurial
 def test_freeze_mercurial_clone(script, tmpdir):
     """