Skip to content

Commit 6f62cd7

Browse files
committed
Merge branch 'jc/receive-verify'
* jc/receive-verify: receive-pack: check connectivity before concluding "git push" check_everything_connected(): libify check_everything_connected(): refactor to use an iterator fetch: verify we have everything we need before updating our ref Conflicts: builtin/fetch.c
2 parents 2e2e7e9 + 52fed6e commit 6f62cd7

File tree

5 files changed

+141
-56
lines changed

5 files changed

+141
-56
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ LIB_H += compat/win32/pthread.h
516516
LIB_H += compat/win32/syslog.h
517517
LIB_H += compat/win32/sys/poll.h
518518
LIB_H += compat/win32/dirent.h
519+
LIB_H += connected.h
519520
LIB_H += csum-file.h
520521
LIB_H += decorate.h
521522
LIB_H += delta.h
@@ -596,6 +597,7 @@ LIB_OBJS += commit.o
596597
LIB_OBJS += compat/obstack.o
597598
LIB_OBJS += config.o
598599
LIB_OBJS += connect.o
600+
LIB_OBJS += connected.o
599601
LIB_OBJS += convert.o
600602
LIB_OBJS += copy.o
601603
LIB_OBJS += csum-file.o

builtin/fetch.c

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "sigchain.h"
1414
#include "transport.h"
1515
#include "submodule.h"
16+
#include "connected.h"
1617

1718
static const char * const builtin_fetch_usage[] = {
1819
"git fetch [<options>] [<repository> [<refspec>...]]",
@@ -345,62 +346,16 @@ static int update_local_ref(struct ref *ref,
345346
}
346347
}
347348

348-
/*
349-
* The ref_map records the tips of the refs we are fetching. If
350-
*
351-
* $ git rev-list --verify-objects --stdin --not --all
352-
*
353-
* (feeding all the refs in ref_map on its standard input) does not
354-
* error out, that means everything reachable from these updated refs
355-
* locally exists and is connected to some of our existing refs.
356-
*
357-
* Returns 0 if everything is connected, non-zero otherwise.
358-
*/
359-
static int check_everything_connected(struct ref *ref_map, int quiet)
349+
static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
360350
{
361-
struct child_process rev_list;
362-
const char *argv[] = {"rev-list", "--verify-objects",
363-
"--stdin", "--not", "--all", NULL, NULL};
364-
char commit[41];
365-
struct ref *ref;
366-
int err = 0;
367-
368-
if (!ref_map)
369-
return 0;
370-
371-
if (quiet)
372-
argv[5] = "--quiet";
373-
374-
memset(&rev_list, 0, sizeof(rev_list));
375-
rev_list.argv = argv;
376-
rev_list.git_cmd = 1;
377-
rev_list.in = -1;
378-
rev_list.no_stdout = 1;
379-
rev_list.no_stderr = quiet;
380-
if (start_command(&rev_list))
381-
return error(_("Could not run 'git rev-list'"));
382-
383-
sigchain_push(SIGPIPE, SIG_IGN);
384-
385-
memcpy(commit + 40, "\n", 2);
386-
for (ref = ref_map; ref; ref = ref->next) {
387-
memcpy(commit, sha1_to_hex(ref->old_sha1), 40);
388-
if (write_in_full(rev_list.in, commit, 41) < 0) {
389-
if (errno != EPIPE && errno != EINVAL)
390-
error(_("failed write to rev-list: %s"),
391-
strerror(errno));
392-
err = -1;
393-
break;
394-
}
395-
}
396-
if (close(rev_list.in)) {
397-
error(_("failed to close rev-list's stdin: %s"), strerror(errno));
398-
err = -1;
399-
}
400-
401-
sigchain_pop(SIGPIPE);
351+
struct ref **rm = cb_data;
352+
struct ref *ref = *rm;
402353

403-
return finish_command(&rev_list) || err;
354+
if (!ref)
355+
return -1; /* end of the list */
356+
*rm = ref->next;
357+
hashcpy(sha1, ref->old_sha1);
358+
return 0;
404359
}
405360

406361
static int store_updated_refs(const char *raw_url, const char *remote_name,
@@ -423,7 +378,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
423378
else
424379
url = xstrdup("foreign");
425380

426-
if (check_everything_connected(ref_map, 0))
381+
rm = ref_map;
382+
if (check_everything_connected(iterate_ref_map, 0, &rm))
427383
return error(_("%s did not send all necessary objects\n"), url);
428384

429385
for (rm = ref_map; rm; rm = rm->next) {
@@ -522,6 +478,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
522478
*/
523479
static int quickfetch(struct ref *ref_map)
524480
{
481+
struct ref *rm = ref_map;
482+
525483
/*
526484
* If we are deepening a shallow clone we already have these
527485
* objects reachable. Running rev-list here will return with
@@ -531,7 +489,7 @@ static int quickfetch(struct ref *ref_map)
531489
*/
532490
if (depth)
533491
return -1;
534-
return check_everything_connected(ref_map, 1);
492+
return check_everything_connected(iterate_ref_map, 1, &rm);
535493
}
536494

537495
static int fetch_refs(struct transport *transport, struct ref *ref_map)

builtin/receive-pack.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "transport.h"
1212
#include "string-list.h"
1313
#include "sha1-array.h"
14+
#include "connected.h"
1415

1516
static const char receive_pack_usage[] = "git receive-pack <git-dir>";
1617

@@ -585,6 +586,43 @@ static void check_aliased_updates(struct command *commands)
585586
string_list_clear(&ref_list, 0);
586587
}
587588

589+
static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
590+
{
591+
struct command **cmd_list = cb_data;
592+
struct command *cmd = *cmd_list;
593+
594+
if (!cmd)
595+
return -1; /* end of list */
596+
*cmd_list = NULL; /* this returns only one */
597+
hashcpy(sha1, cmd->new_sha1);
598+
return 0;
599+
}
600+
601+
static void set_connectivity_errors(struct command *commands)
602+
{
603+
struct command *cmd;
604+
605+
for (cmd = commands; cmd; cmd = cmd->next) {
606+
struct command *singleton = cmd;
607+
if (!check_everything_connected(command_singleton_iterator,
608+
0, &singleton))
609+
continue;
610+
cmd->error_string = "missing necessary objects";
611+
}
612+
}
613+
614+
static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
615+
{
616+
struct command **cmd_list = cb_data;
617+
struct command *cmd = *cmd_list;
618+
619+
if (!cmd)
620+
return -1; /* end of list */
621+
*cmd_list = cmd->next;
622+
hashcpy(sha1, cmd->new_sha1);
623+
return 0;
624+
}
625+
588626
static void execute_commands(struct command *commands, const char *unpacker_error)
589627
{
590628
struct command *cmd;
@@ -596,6 +634,11 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
596634
return;
597635
}
598636

637+
cmd = commands;
638+
if (check_everything_connected(iterate_receive_command_list,
639+
0, &cmd))
640+
set_connectivity_errors(commands);
641+
599642
if (run_receive_hook(commands, pre_receive_hook)) {
600643
for (cmd = commands; cmd; cmd = cmd->next)
601644
cmd->error_string = "pre-receive hook declined";

connected.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "cache.h"
2+
#include "run-command.h"
3+
#include "sigchain.h"
4+
#include "connected.h"
5+
6+
/*
7+
* If we feed all the commits we want to verify to this command
8+
*
9+
* $ git rev-list --verify-objects --stdin --not --all
10+
*
11+
* and if it does not error out, that means everything reachable from
12+
* these commits locally exists and is connected to some of our
13+
* existing refs.
14+
*
15+
* Returns 0 if everything is connected, non-zero otherwise.
16+
*/
17+
int check_everything_connected(sha1_iterate_fn fn, int quiet, void *cb_data)
18+
{
19+
struct child_process rev_list;
20+
const char *argv[] = {"rev-list", "--verify-objects",
21+
"--stdin", "--not", "--all", NULL, NULL};
22+
char commit[41];
23+
unsigned char sha1[20];
24+
int err = 0;
25+
26+
if (fn(cb_data, sha1))
27+
return err;
28+
29+
if (quiet)
30+
argv[5] = "--quiet";
31+
32+
memset(&rev_list, 0, sizeof(rev_list));
33+
rev_list.argv = argv;
34+
rev_list.git_cmd = 1;
35+
rev_list.in = -1;
36+
rev_list.no_stdout = 1;
37+
rev_list.no_stderr = quiet;
38+
if (start_command(&rev_list))
39+
return error(_("Could not run 'git rev-list'"));
40+
41+
sigchain_push(SIGPIPE, SIG_IGN);
42+
43+
commit[40] = '\n';
44+
do {
45+
memcpy(commit, sha1_to_hex(sha1), 40);
46+
if (write_in_full(rev_list.in, commit, 41) < 0) {
47+
if (errno != EPIPE && errno != EINVAL)
48+
error(_("failed write to rev-list: %s"),
49+
strerror(errno));
50+
err = -1;
51+
break;
52+
}
53+
} while (!fn(cb_data, sha1));
54+
55+
if (close(rev_list.in)) {
56+
error(_("failed to close rev-list's stdin: %s"), strerror(errno));
57+
err = -1;
58+
}
59+
60+
sigchain_pop(SIGPIPE);
61+
return finish_command(&rev_list) || err;
62+
}

connected.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef CONNECTED_H
2+
#define CONNECTED_H
3+
4+
/*
5+
* Take callback data, and return next object name in the buffer.
6+
* When called after returning the name for the last object, return -1
7+
* to signal EOF, otherwise return 0.
8+
*/
9+
typedef int (*sha1_iterate_fn)(void *, unsigned char [20]);
10+
11+
/*
12+
* Make sure that our object store has all the commits necessary to
13+
* connect the ancestry chain to some of our existing refs, and all
14+
* the trees and blobs that these commits use.
15+
*
16+
* Return 0 if Ok, non zero otherwise (i.e. some missing objects)
17+
*/
18+
extern int check_everything_connected(sha1_iterate_fn, int quiet, void *cb_data);
19+
20+
#endif /* CONNECTED_H */

0 commit comments

Comments
 (0)