Skip to contentSkip to conversation

ls-remote: print URL when no repo is specified

PATCH Superseded Tay Ray Chuan 5 participants 12 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ls-remote: print URL when no repo is specified

After 9c00de5 (ls-remote: fall-back to default remotes when no remote specified), when no repository is specified, ls-remote may use the URL/remote in the config "branch.<name>.remote" or the remote "origin"; it may not be immediately obvious to the user which was used.

In such cases, print a simple "From <URL>" line to indicate which repository was used. This message is similar to git-fetch's, and is printed to stderr to avoid breaking existing scripts that depend on ls-remote's output behaviour.

It can also be disabled with -q/--quiet.

Modify tests related to falling back on default remotes to check for this as well, and add a test to check for suppression of the message.

---

This is a logical follow-up to the "optional repo" form of ls-remote feature. Myself, I was using the "no-repo" form of ls-remote and sometimes wondered where it was listing remotes from.

Johnathan, do you think the tests involved might have any cross- platform issues?

 builtin/ls-remote.c  |   10 +++++++++-
 t/t5512-ls-remote.sh |   19 +++++++++++++++----
 2 files changed, 24 insertions(+), 5 deletions(-)
builtin/ls-remote.c +9 -1
index 8ee91eb..34480cf 100644
@@ -5,7 +5,7 @@
 
 static const char ls_remote_usage[] =
 "git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n"
-"                     [<repository> [<refs>...]]";
+"                     [-q|--quiet] [<repository> [<refs>...]]";
 
 /*
  * Is there one among the list of patterns that match the tail part
@@ -34,6 +34,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	const char *dest = NULL;
 	int nongit;
 	unsigned flags = 0;
+	int quiet = 0;
 	const char *uploadpack = NULL;
 	const char **pattern = NULL;
 
@@ -67,6 +68,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 				flags |= REF_NORMAL;
 				continue;
 			}
+			if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
+				quiet = 1;
+				continue;
+			}
 			usage(ls_remote_usage);
 		}
 		dest = arg;
@@ -99,6 +104,9 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	ref = transport_get_remote_refs(transport);
 	if (transport_disconnect(transport))
 		return 1;
+
+	if (!dest && !quiet)
+		fprintf(stderr, "From %s\n", *remote->url);
 	for ( ; ref; ref = ref->next) {
 		if (!check_ref_type(ref, flags))
 			continue;
t/t5512-ls-remote.sh +15 -4
index 3cf1b3d..8508df8 100755
@@ -57,12 +57,21 @@ test_expect_success 'dies when no remote specified and no default remotes found'
 
 test_expect_success 'use "origin" when no remote specified' '
 
-	git remote add origin "$(pwd)/.git" &&
-	git ls-remote >actual &&
+	URL="$(pwd)/.git" &&
+	git remote add origin "$URL" &&
+	git ls-remote 2>actual_err >actual &&
+	grep "From $URL" actual_err &&
 	test_cmp expected.all actual
 
 '
 
+test_expect_success 'suppress "From <url>" with -q' '
+
+	git ls-remote -q 2>actual_err &&
+	test_must_fail grep "From $URL" actual_err
+
+'
+
 test_expect_success 'use branch.<name>.remote if possible' '
 
 	#
@@ -78,10 +87,12 @@ test_expect_success 'use branch.<name>.remote if possible' '
 		git show-ref	| sed -e "s/ /	/"
 	) >exp &&
 
-	git remote add other other.git &&
+	URL="other.git" &&
+	git remote add other $URL &&
 	git config branch.master.remote other &&
 
-	git ls-remote >actual &&
+	git ls-remote 2>actual_err >actual &&
+	grep "From $URL" actual_err &&
 	test_cmp exp actual
 
 '

--
1.7.1.189.g07419

#1 lore raw
Quoted 3 lines · Tay Ray Chuan

when no repository is specified, ls-remote may use
the URL/remote in the config "branch.<name>.remote" or the remote "origin"

[...]

Quoted 2 lines

In such cases, print a simple "From <URL>" line to indicate which repository was used. This message is similar to git-fetch's

Makes sense. The code and tests look sane.

Testing on Linux and Msysgit does not reveal any problems, either.

Thanks,
Jonathan

#2 lore raw

PATCH v2 1/1 ls-remote: print URL when no repo is specified

In reply to Tay Ray Chuan

After 9c00de5 (ls-remote: fall-back to default remotes when no remote specified), when no repository is specified, ls-remote may use the URL/remote in the config "branch.<name>.remote" or the remote "origin"; it may not be immediately obvious to the user which was used.

In such cases, print a simple "From <URL>" line to indicate which repository was used. This message is similar to git-fetch's, and is printed to stderr to avoid breaking existing scripts that depend on ls-remote's output behaviour.

It can also be disabled with -q/--quiet.

Modify tests related to falling back on default remotes to check for this as well, and add a test to check for suppression of the message.

---

The sole difference from the previous patch is the use of test_cmp instead of grep in the tests.

 builtin/ls-remote.c  |   10 +++++++++-
 t/t5512-ls-remote.sh |   24 ++++++++++++++++++++----
 2 files changed, 29 insertions(+), 5 deletions(-)
builtin/ls-remote.c +9 -1
index 8ee91eb..34480cf 100644
@@ -5,7 +5,7 @@
 
 static const char ls_remote_usage[] =
 "git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n"
-"                     [<repository> [<refs>...]]";
+"                     [-q|--quiet] [<repository> [<refs>...]]";
 
 /*
  * Is there one among the list of patterns that match the tail part
@@ -34,6 +34,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	const char *dest = NULL;
 	int nongit;
 	unsigned flags = 0;
+	int quiet = 0;
 	const char *uploadpack = NULL;
 	const char **pattern = NULL;
 
@@ -67,6 +68,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 				flags |= REF_NORMAL;
 				continue;
 			}
+			if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
+				quiet = 1;
+				continue;
+			}
 			usage(ls_remote_usage);
 		}
 		dest = arg;
@@ -99,6 +104,9 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	ref = transport_get_remote_refs(transport);
 	if (transport_disconnect(transport))
 		return 1;
+
+	if (!dest && !quiet)
+		fprintf(stderr, "From %s\n", *remote->url);
 	for ( ; ref; ref = ref->next) {
 		if (!check_ref_type(ref, flags))
 			continue;
t/t5512-ls-remote.sh +20 -4
index 3cf1b3d..d191235 100755
@@ -57,12 +57,24 @@ test_expect_success 'dies when no remote specified and no default remotes found'
 
 test_expect_success 'use "origin" when no remote specified' '
 
-	git remote add origin "$(pwd)/.git" &&
-	git ls-remote >actual &&
+	URL="$(pwd)/.git" &&
+	echo "From $URL" >exp_err &&
+
+	git remote add origin "$URL" &&
+	git ls-remote 2>actual_err >actual &&
+
+	test_cmp exp_err actual_err &&
 	test_cmp expected.all actual
 
 '
 
+test_expect_success 'suppress "From <url>" with -q' '
+
+	git ls-remote -q 2>actual_err &&
+	test_must_fail test_cmp exp_err actual_err
+
+'
+
 test_expect_success 'use branch.<name>.remote if possible' '
 
 	#
@@ -78,10 +90,14 @@ test_expect_success 'use branch.<name>.remote if possible' '
 		git show-ref	| sed -e "s/ /	/"
 	) >exp &&
 
-	git remote add other other.git &&
+	URL="other.git" &&
+	echo "From $URL" >exp_err &&
+
+	git remote add other $URL &&
 	git config branch.master.remote other &&
 
-	git ls-remote >actual &&
+	git ls-remote 2>actual_err >actual &&
+	test_cmp exp_err actual_err &&
 	test_cmp exp actual
 
 '

--
1.7.1.189.g07419

#3 lore raw
Quoted 4 lines · Tay Ray Chuan

After 9c00de5 (ls-remote: fall-back to default remotes when no remote specified), when no repository is specified, ls-remote may use the URL/remote in the config "branch.<name>.remote" or the remote "origin"; it may not be immediately obvious to the user which was used.

I cannot convince myself that this is a good change, as I've always thought "ls-remote" output as something people want to let their scripts read and parse. 9c00de5 may have given an enhancement to these scripts in the sense that they can now respond to an empty input from the end user, but this patch forces them to change the way they parse the output from the command.

I also think this patch is solving a wrong problem.

When an end user does not know which remote ls-remote would be talking to by default, what else does he *not* know? Probably which remote "pull" would be fetching from and what branch it would be merging with? Doesn't he have a better command to use to learn that information to reorient himself when he is lost that way?

#4 lore raw

Hi,

Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Tay Ray Chuan

After 9c00de5 (ls-remote: fall-back to default remotes when no remote specified), when no repository is specified, ls-remote may use the URL/remote in the config "branch.<name>.remote" or the remote "origin"; it may not be immediately obvious to the user which was used.

Quoted 6 lines

I cannot convince myself that this is a good change, as I've always thought "ls-remote" output as something people want to let their scripts read and parse.  9c00de5 may have given an enhancement to these scripts in the sense that they can now respond to an empty input from the end user, but this patch forces them to change the way they parse the output from the command.

in this patch, the remote url is printed to stderr, instead of stdout, so existing scripts should be safe.

Quoted 7 lines

I also think this patch is solving a wrong problem.

When an end user does not know which remote ls-remote would be talking to by default, what else does he *not* know?  Probably which remote "pull" would be fetching from and what branch it would be merging with?  Doesn't he have a better command to use to learn that information to reorient himself when he is lost that way?

I'm not sure if there's a command to determine the remote - I'd be interested to know it, if there's one.

That aside, I believe this patch as an attempt at improving usability.

Compare (pre-patch):

  $ git ls-remote
  (scratch head)
  $ git-x # to determine which remote we listed refs from

with (post-patch):

  $ git ls-remote

The advantage is minor, but I feel there's some added convenience.

Signature · 2 lines
Cheers,
Ray Chuan
#5 lore raw
Quoted 8 lines · Tay Ray Chuan
Quoted 6 lines · Junio C Hamano

I cannot convince myself that this is a good change, as I've always thought "ls-remote" output as something people want to let their scripts read and parse.  9c00de5 may have given an enhancement to these scripts in the sense that they can now respond to an empty input from the end user, but this patch forces them to change the way they parse the output from the command.

Would 9c00de5 be so useful for scripts? I suspect the typical script does

git ls-remote "$remote"

so to use the new default it would need adjusting.

Run by hand, 'ls-remote | grep heads' can be quite useful.

Quoted 8 lines

in this patch, the remote url is printed to stderr, instead of stdout, so existing scripts should be safe.

Quoted 5 lines

I also think this patch is solving a wrong problem.

When an end user does not know which remote ls-remote would be talking to by default, what else does he *not* know?  Probably which remote "pull" would be fetching from

[...]

I think I see what you are saying, and for scripts, that really would be the most useful thing. Then the script could use something like

if test -z "$remote"
then

	remote=$(git branch --get-remote --current)

fi
git ls-remote "$remote"

which would be much better than

  git ls-remote ${remote:+"$remote"} 2>/dev/null

because it does not suppress error messages.

For manual use of ls-remote, on the other hand, I can see the use of the reminder.

Jonathan

#6 lore raw
Quoted 10 lines · Jonathan Nieder
Quoted 8 lines · Tay Ray Chuan
Quoted 6 lines · Junio C Hamano

I cannot convince myself that this is a good change, as I've always thought "ls-remote" output as something people want to let their scripts read and parse.  9c00de5 may have given an enhancement to these scripts in the sense that they can now respond to an empty input from the end user, but this patch forces them to change the way they parse the output from the command.

Quoted 6 lines

Would 9c00de5 be so useful for scripts?  I suspect the typical script does

 git ls-remote "$remote"

so to use the new default it would need adjusting.

Right, existing scripts that use git-ls-remote are unlikely to be affected by 9c00de5, or this patch, for that matter.

Quoted 10 lines
Quoted 8 lines

in this patch, the remote url is printed to stderr, instead of stdout, so existing scripts should be safe.

Quoted 5 lines

I also think this patch is solving a wrong problem.

When an end user does not know which remote ls-remote would be talking to by default, what else does he *not* know?  Probably which remote "pull" would be fetching from

[...]

Quoted 8 lines

I think I see what you are saying, and for scripts, that really would be the most useful thing.  Then the script could use something like

 if test -z "$remote"
 then
       remote=$(git branch --get-remote --current) fi
 git ls-remote "$remote"

Just curious - when did git-branch learn "--get-remote"? Or "--current"?

Signature · 2 lines
Cheers,
Ray Chuan
#7 lore raw
Quoted 5 lines · Tay Ray Chuan
Quoted 3 lines · Jonathan Nieder

I think I see what you are saying, and for scripts, that really would be the most useful thing.  Then the script could use something like

Quoted 7 lines
Quoted 5 lines

 if test -z "$remote"
 then
       remote=$(git branch --get-remote --current) fi
 git ls-remote "$remote"

Just curious - when did git-branch learn "--get-remote"? Or "--current"?

It didn’t. I was just saying that it should (or we should provide some commands to that effect). What do you think?

Maybe later today...
Jonathan

#8 lore raw
Quoted 12 lines · Jonathan Nieder
Quoted 11 lines · Tay Ray Chuan
Quoted 8 lines · Jonathan Nieder

I think I see what you are saying, and for scripts, that really would be the most useful thing.  Then the script could use something like

 if test -z "$remote"
 then
       remote=$(git branch --get-remote --current) fi
 git ls-remote "$remote"

Quoted 4 lines
Quoted 1 line

Just curious - when did git-branch learn "--get-remote"? Or "--current"?

It didn’t. I was just saying that it should (or we should provide some commands to that effect). What do you think?

I think you said what I meant to say much more clearly. Thanks.

#9 lore raw
In reply to Tay Ray Chuan
Quoted 8 lines · Tay Ray Chuan
Quoted 5 lines

When an end user does not know which remote ls-remote would be talking to by default, what else does he *not* know? Probably which remote "pull" would be fetching from and what branch it would be merging with? Doesn't he have a better command to use to learn that information to reorient himself when he is lost that way?

I'm not sure if there's a command to determine the remote - I'd be interested to know it, if there's one.

What git seems to be missing is a "git info" command, which would print out essentially something like what "svn info" does:
name of remote repo that we're tracking, name of current branch, date/author/subject of last commit.

Regards,
-Geert

#10 lore raw

Heya,

Quoted 4 lines · Geert Bosch

What git seems to be missing is a "git info" command, which would print out essentially something like what "svn info" does:
name of remote repo that we're tracking, name of current branch, date/author/subject of last commit.

You can get information about the remote with:

$ git remote -v

And then for the branch etc you can do (although it doesn't list the author):

$ git branch -v
Signature · 3 lines
Cheers,

Sverre Rabbelier
#11 lore raw
Quoted 9 lines · Sverre Rabbelier
Quoted 4 lines · Geert Bosch

What git seems to be missing is a "git info" command, which would print out essentially something like what "svn info" does:
name of remote repo that we're tracking, name of current branch, date/author/subject of last commit.

You can get information about the remote with:

$ git remote -v

geert-boschs-computer:/tmp%ssh kwai git init --bare /work/bosch/temp.git Initialized empty Git repository in /export/work/bosch/temp.git/ geert-boschs-computer:/tmp%git init temp
Initialized empty Git repository in /private/tmp/temp/.git/ geert-boschs-computer:/tmp%cd temp geert-boschs-computer:/tmp/temp%echo hello>world geert-boschs-computer:/tmp/temp%git add world geert-boschs-computer:/tmp/temp%git commit -m "Add hello" world [master (root-commit) 10837b9] Add hello

 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 world

geert-boschs-computer:/tmp/temp%git push --all kwai:/work/bosch/temp.git Counting objects: 3, done.
Writing objects: 100% (3/3), 211 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To kwai:/work/bosch/temp.git
* [new branch] master -> master
geert-boschs-computer:/tmp/temp%git push -u --all kwai:/work/bosch/temp.git Branch master set up to track remote branch master from kwai:/work/bosch/temp.git. Everything up-to-date
geert-boschs-computer:/tmp/temp%git remote -v geert-boschs-computer:/tmp/temp%git push
Everything up-to-date

So, there is a remote that gets used automatically by git push and git pull, but it's not obvious which one. Basically, a "git info" command should give a dozen lines or so that allows one quickly to identify a repository, including current branch, any remote it tracks, last commit, wether the working directory is clean, wether anything is staged for commit, wether the repository tracks a non-git (svn, bzr, hg) repository.

Quoted 4 lines

And then for the branch etc you can do (although it doesn't list the author):

$ git branch -v

While both commands are very useful, I think a case can be made for a single command that pulls together enough information in a few lines that it is clear.

I'll see if I can cobble something up.