Skip to contentSkip to conversation

git-fetch: avoid local fetching from alternate (again)

PATCH In review Shawn O. Pearce 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-fetch: avoid local fetching from alternate (again)

Back in e3c6f240fd9c5bdeb33f2d47adc859f37935e2df Junio taught git-fetch to avoid copying objects when we are fetching from a repository that is already registered as an alternate object database. In such a case there is no reason to copy any objects as we can already obtain them through the alternate.

However we need to ensure the objects are all reachable, so we run git rev-list --objects $theirs --not --all to verify this. If any object is missing or unreadable then we need to instead copy the objects from the remote. When a missing object is detected the git-rev-list process will exit with a non-zero exit status, making this condition quite easy to detect.

Although git-fetch is currently a builtin (and so is rev-list) we really cannot invoke the traverse_objects() API at this point in the transport code. The object walker within traverse_objects() calls die() as soon as it finds an object it cannot read. If that happens we want to resume the fetch process by calling do_fetch_pack(), instead of terminating. To get aroaund this we spawn git-rev-list into a background process to prevent a die() from killing the foreground fetch process.

We aren't interested in the output of rev-list (a list of SHA-1 object names that are reachable) or its errors (a "spurious" error about an object not being found as we need to copy it) so we redirect both stdout and stderr to /dev/null.

---
 run-command.c |    6 ++++--
 run-command.h |    1 +
 transport.c   |   48 +++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 50 insertions(+), 5 deletions(-)
run-command.c +4 -2
index d99a6c4..476d00c 100644
@@ -41,7 +41,7 @@ int start_command(struct child_process *cmd)
 		cmd->close_out = 1;
 	}
 
-	need_err = cmd->err < 0;
+	need_err = !cmd->no_stderr && cmd->err < 0;
 	if (need_err) {
 		if (pipe(fderr) < 0) {
 			if (need_in)
@@ -87,7 +87,9 @@ int start_command(struct child_process *cmd)
 			close(cmd->out);
 		}
 
-		if (need_err) {
+		if (cmd->no_stderr)
+			dup_devnull(2);
+		else if (need_err) {
 			dup2(fderr[1], 2);
 			close_pair(fderr);
 		}
run-command.h +1 -0
index 94e1e9d..1fc781d 100644
@@ -23,6 +23,7 @@ struct child_process {
 	unsigned close_out:1;
 	unsigned no_stdin:1;
 	unsigned no_stdout:1;
+	unsigned no_stderr:1;
 	unsigned git_cmd:1; /* if this is to be git sub-command */
 	unsigned stdout_to_stderr:1;
 };
transport.c +45 -3

68 lines not on this page · raw message

Signature · 1 line
1.5.3.5.1590.gfadfad
#1 lore raw

transport.c static struct ref *get_refs_via_connect(struct transport *transport) · line 647

Quoted 3 lines · Shawn O. Pearce
transport.c +3 -0
+	err = start_command(&revlist);
+	if (!err)
+		err |= finish_command(&revlist);

There's a short-hand:

	err = run_command(&revlist);

-- Hannes

#2 lore raw
In reply to Shawn O. Pearce
Quoted 5 lines · Shawn O. Pearce

Back in e3c6f240fd9c5bdeb33f2d47adc859f37935e2df Junio taught git-fetch to avoid copying objects when we are fetching from a repository that is already registered as an alternate object database. In such a case there is no reason to copy any objects as we can already obtain them through the alternate.

Well spotted. It would be a good idea to commit the big comment from contrib/examples/git-fetch.sh to fetch_local_nocopy() function, which would have made us realize that the patch does not refrain from applying this optimization even when shallow is in effect. But I think that is actually a good change.

The run-command change and the main part of the fix are logically independent.

The regression the patch fixes should be testable with a script. Please have a new test for it.

Thanks.

#3 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 5 lines · Shawn O. Pearce

Back in e3c6f240fd9c5bdeb33f2d47adc859f37935e2df Junio taught git-fetch to avoid copying objects when we are fetching from a repository that is already registered as an alternate object database. In such a case there is no reason to copy any objects as we can already obtain them through the alternate.

The regression the patch fixes should be testable with a script. Please have a new test for it.

Hmmph. t5502-quickfetch should have covered this. It obviously wasn't testing the right thing here. I'll figure out why and post a patch to fix t5502.

Signature · 1 line
Shawn.
#4 lore raw
In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Shawn O. Pearce

Back in e3c6f240fd9c5bdeb33f2d47adc859f37935e2df Junio taught git-fetch to avoid copying objects when we are fetching from a repository that is already registered as an alternate object database. In such a case there is no reason to copy any objects as we can already obtain them through the alternate.

Quoted 5 lines

Well spotted. It would be a good idea to commit the big comment from contrib/examples/git-fetch.sh to fetch_local_nocopy() function, which would have made us realize that the patch does not refrain from applying this optimization even when shallow is in effect. But I think that is actually a good change.

Having thought about this further by writing that comment myself (patch attached), I suspect that the test at the beginning of the function to see if we are talking to another local repository is not necessary. Even if we are _not_ fetching from the remote, we could have all the necessary objects connected, albeit the chance of that is rather slim. But that means the rev-list test will error out rather quickly because objects near the tips are more likely to be missing, and if we are talking about a true remote connection, networking cost will most likely outweigh the cost to do this checking.

---

 transport.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
transport.c +14 -0
index 0604dc6..a887491 100644
@@ -614,6 +614,20 @@ static struct ref *get_refs_via_connect(const struct transport *transport)
 	return refs;
 }
 
+/*
+ * We would want to bypass the object transfer altogether if
+ * everything we are going to fetch already exists and connected
+ * locally.
+ *
+ * The refs we are going to fetch are in to_fetch (nr_heads in
+ * total).  If running
+ *
+ *  $ git-rev-list --objects to_fetch[0] to_fetch[1] ... --not --all
+ *
+ * does not error out, that means everything reachable from the
+ * refs we are going to fetch exists and is connected to some of
+ * our existing refs.
+ */
 static int fetch_local_nocopy(struct transport *transport,
 			       int nr_heads, struct ref **to_fetch)
 {
#5 lore raw
In reply to Junio C Hamano
Quoted 5 lines · Junio C Hamano

Well spotted. It would be a good idea to commit the big comment from contrib/examples/git-fetch.sh to fetch_local_nocopy() function, which would have made us realize that the patch does not refrain from applying this optimization even when shallow is in effect. But I think that is actually a good change.

I take this back. This regresses badly.

Why?

Because the optimization is useless when we are trying to deepen the shallow history. When you are trying to deepen a shallow history and the tips of remotes haven't moved since you fetched from there the last time, you have everything near the tip, and becuse your history is shallow, your ancestry chain is cauterized to make it appear that the history is complete. The rev-list reachability test would not fail as we expect.

The breakage can be seen with t5500.

#6 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 5 lines · Junio C Hamano

Well spotted. It would be a good idea to commit the big comment from contrib/examples/git-fetch.sh to fetch_local_nocopy() function, which would have made us realize that the patch does not refrain from applying this optimization even when shallow is in effect. But I think that is actually a good change.

I take this back. This regresses badly.

Why?

Whoops. Good catch. This is why you had a check for the shallow history file in git-fetch.sh before you took this optimization path. My fault for not including it in this patch.

Quoted 7 lines

Because the optimization is useless when we are trying to deepen the shallow history. When you are trying to deepen a shallow history and the tips of remotes haven't moved since you fetched from there the last time, you have everything near the tip, and becuse your history is shallow, your ancestry chain is cauterized to make it appear that the history is complete. The rev-list reachability test would not fail as we expect.

What about just inserting a check to see if --depth was supplied to git-fetch on the command line? If so then we are deepening the history and we just bypass the rev-list "fast path" test.

I will be posting an updated patch (series now) shortly.

Signature · 1 line
Shawn.