23
#include "fetch-negotiator.h"
24
#include "fsck.h"
25
#include "shallow.h"
26
+#include "commit-reach.h"
27
+#include "commit-graph.h"
28
29
static int transfer_unpack_limit = -1;
30
static int fetch_unpack_limit = -1;
47
/* Remember to update object flag allocation in object.h */
48
#define COMPLETE (1U << 0)
49
#define ALTERNATE (1U << 1)
50
+#define COMMON (1U << 6)
51
+#define REACH_SCRATCH (1U << 7)
52
53
/*
54
* After sending this many "have"s if we do not get any new ACK , we
1527
FETCH_DONE,
1528
};
1529
1526
-static void do_check_stateless_delimiter(const struct fetch_pack_args *args,
1530
+static void do_check_stateless_delimiter(int stateless_rpc,
1531
struct packet_reader *reader)
1532
{
1529
- check_stateless_delimiter(args->stateless_rpc, reader,
1533
+ check_stateless_delimiter(stateless_rpc, reader,
1534
_("git fetch-pack: expected response end packet"));
1535
}
1536
1626
*/
1627
state = FETCH_GET_PACK;
1628
} else {
1625
- do_check_stateless_delimiter(args, &reader);
1629
+ do_check_stateless_delimiter(args->stateless_rpc, &reader);
1630
state = FETCH_SEND_REQUEST;
1631
}
1632
break;
1649
packfile_uris.nr ? &index_pack_args : NULL,
1650
sought, nr_sought, &fsck_options.gitmodules_found))
1651
die(_("git fetch-pack: fetch failed."));
1648
- do_check_stateless_delimiter(args, &reader);
1652
+ do_check_stateless_delimiter(args->stateless_rpc, &reader);
1653
1654
state = FETCH_DONE;
1655
break;
1966
return ref_cpy;
1967
}
1968
1969
+static int add_to_object_array(const struct object_id *oid, void *data)
1970
+{
1971
+ struct object_array *a = data;
1972
+
1973
+ add_object_array(lookup_object(the_repository, oid), "", a);
1974
+ return 0;
1975
+}
1976
+
1977
+static void clear_common_flag(struct oidset *s)
1978
+{
1979
+ struct oidset_iter iter;
1980
+ const struct object_id *oid;
1981
+ oidset_iter_init(s, &iter);
1982
+
1983
+ while ((oid = oidset_iter_next(&iter))) {
1984
+ struct object *obj = lookup_object(the_repository, oid);
1985
+ obj->flags &= ~COMMON;
1986
+ }
1987
+}
1988
+
1989
+void negotiate_using_fetch(const struct oid_array *negotiation_tips,
1990
+ const struct string_list *server_options,
1991
+ int stateless_rpc,
1992
+ int fd[],
1993
+ struct oidset *acked_commits)
1994
+{
1995
+ struct fetch_negotiator negotiator;
1996
+ struct packet_reader reader;
1997
+ struct object_array nt_object_array = OBJECT_ARRAY_INIT;
1998
+ struct strbuf req_buf = STRBUF_INIT;
1999
+ int haves_to_send = INITIAL_FLUSH;
2000
+ int in_vain = 0;
2001
+ int seen_ack = 0;
2002
+ int last_iteration = 0;
2003
+ timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2004
+
2005
+ fetch_negotiator_init(the_repository, &negotiator);
2006
+ mark_tips(&negotiator, negotiation_tips);
2007
+
2008
+ packet_reader_init(&reader, fd[0], NULL, 0,
2009
+ PACKET_READ_CHOMP_NEWLINE |
2010
+ PACKET_READ_DIE_ON_ERR_PACKET);
2011
+
2012
+ oid_array_for_each((struct oid_array *) negotiation_tips,
2013
+ add_to_object_array,
2014
+ &nt_object_array);
2015
+
2016
+ while (!last_iteration) {
2017
+ int haves_added;
2018
+ struct object_id common_oid;
2019
+ int received_ready = 0;
2020
+
2021
+ strbuf_reset(&req_buf);
2022
+ write_fetch_command_and_capabilities(&req_buf, server_options);
2023
+
2024
+ packet_buf_write(&req_buf, "wait-for-done");
2025
+
2026
+ haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2027
+ in_vain += haves_added;
2028
+ if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2029
+ last_iteration = 1;
2030
+
2031
+ /* Send request */
2032
+ packet_buf_flush(&req_buf);
2033
+ if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2034
+ die_errno(_("unable to write request to remote"));
2035
+
2036
+ /* Process ACKs/NAKs */
2037
+ process_section_header(&reader, "acknowledgments", 0);
2038
+ while (process_ack(&negotiator, &reader, &common_oid,
2039
+ &received_ready)) {
2040
+ struct commit *commit = lookup_commit(the_repository,
2041
+ &common_oid);
2042
+ if (commit) {
2043
+ timestamp_t generation;
2044
+
2045
+ parse_commit_or_die(commit);
2046
+ commit->object.flags |= COMMON;
2047
+ generation = commit_graph_generation(commit);
2048
+ if (generation < min_generation)
2049
+ min_generation = generation;
2050
+ }
2051
+ in_vain = 0;
2052
+ seen_ack = 1;
2053
+ oidset_insert(acked_commits, &common_oid);
2054
+ }
2055
+ if (received_ready)
2056
+ die(_("unexpected 'ready' from remote"));
2057
+ else
2058
+ do_check_stateless_delimiter(stateless_rpc, &reader);
2059
+ if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2060
+ REACH_SCRATCH, 0,
2061
+ min_generation))
2062
+ last_iteration = 1;
2063
+ }
2064
+ clear_common_flag(acked_commits);
2065
+ strbuf_release(&req_buf);
2066
+}
2067
+
2068
int report_unmatched_refs(struct ref **sought, int nr_sought)
2069
{
2070
int i, ret = 0;