Skip to content

Commit a3d6b53

Browse files
bmwillgitster
authored andcommitted
upload-pack: convert to a builtin
In order to allow for code sharing with the server-side of fetch in protocol-v2 convert upload-pack to be a builtin. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a4cfd41 commit a3d6b53

File tree

6 files changed

+109
-83
lines changed

6 files changed

+109
-83
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,6 @@ PROGRAM_OBJS += imap-send.o
636636
PROGRAM_OBJS += sh-i18n--envsubst.o
637637
PROGRAM_OBJS += shell.o
638638
PROGRAM_OBJS += show-index.o
639-
PROGRAM_OBJS += upload-pack.o
640639
PROGRAM_OBJS += remote-testsvn.o
641640

642641
# Binary suffix, set to .exe for Windows builds
@@ -904,6 +903,7 @@ LIB_OBJS += tree-diff.o
904903
LIB_OBJS += tree.o
905904
LIB_OBJS += tree-walk.o
906905
LIB_OBJS += unpack-trees.o
906+
LIB_OBJS += upload-pack.o
907907
LIB_OBJS += url.o
908908
LIB_OBJS += urlmatch.o
909909
LIB_OBJS += usage.o
@@ -1021,6 +1021,7 @@ BUILTIN_OBJS += builtin/update-index.o
10211021
BUILTIN_OBJS += builtin/update-ref.o
10221022
BUILTIN_OBJS += builtin/update-server-info.o
10231023
BUILTIN_OBJS += builtin/upload-archive.o
1024+
BUILTIN_OBJS += builtin/upload-pack.o
10241025
BUILTIN_OBJS += builtin/var.o
10251026
BUILTIN_OBJS += builtin/verify-commit.o
10261027
BUILTIN_OBJS += builtin/verify-pack.o

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ extern int cmd_update_ref(int argc, const char **argv, const char *prefix);
231231
extern int cmd_update_server_info(int argc, const char **argv, const char *prefix);
232232
extern int cmd_upload_archive(int argc, const char **argv, const char *prefix);
233233
extern int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix);
234+
extern int cmd_upload_pack(int argc, const char **argv, const char *prefix);
234235
extern int cmd_var(int argc, const char **argv, const char *prefix);
235236
extern int cmd_verify_commit(int argc, const char **argv, const char *prefix);
236237
extern int cmd_verify_tag(int argc, const char **argv, const char *prefix);

builtin/upload-pack.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "cache.h"
2+
#include "builtin.h"
3+
#include "exec_cmd.h"
4+
#include "pkt-line.h"
5+
#include "parse-options.h"
6+
#include "protocol.h"
7+
#include "upload-pack.h"
8+
9+
static const char * const upload_pack_usage[] = {
10+
N_("git upload-pack [<options>] <dir>"),
11+
NULL
12+
};
13+
14+
int cmd_upload_pack(int argc, const char **argv, const char *prefix)
15+
{
16+
const char *dir;
17+
int strict = 0;
18+
struct upload_pack_options opts = { 0 };
19+
struct option options[] = {
20+
OPT_BOOL(0, "stateless-rpc", &opts.stateless_rpc,
21+
N_("quit after a single request/response exchange")),
22+
OPT_BOOL(0, "advertise-refs", &opts.advertise_refs,
23+
N_("exit immediately after initial ref advertisement")),
24+
OPT_BOOL(0, "strict", &strict,
25+
N_("do not try <directory>/.git/ if <directory> is no Git directory")),
26+
OPT_INTEGER(0, "timeout", &opts.timeout,
27+
N_("interrupt transfer after <n> seconds of inactivity")),
28+
OPT_END()
29+
};
30+
31+
packet_trace_identity("upload-pack");
32+
check_replace_refs = 0;
33+
34+
argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
35+
36+
if (argc != 1)
37+
usage_with_options(upload_pack_usage, options);
38+
39+
if (opts.timeout)
40+
opts.daemon_mode = 1;
41+
42+
setup_path();
43+
44+
dir = argv[0];
45+
46+
if (!enter_repo(dir, strict))
47+
die("'%s' does not appear to be a git repository", dir);
48+
49+
switch (determine_protocol_version_server()) {
50+
case protocol_v1:
51+
/*
52+
* v1 is just the original protocol with a version string,
53+
* so just fall through after writing the version string.
54+
*/
55+
if (opts.advertise_refs || !opts.stateless_rpc)
56+
packet_write_fmt(1, "version 1\n");
57+
58+
/* fallthrough */
59+
case protocol_v0:
60+
upload_pack(&opts);
61+
break;
62+
case protocol_unknown_version:
63+
BUG("unknown protocol version");
64+
}
65+
66+
return 0;
67+
}

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ static struct cmd_struct commands[] = {
478478
{ "update-server-info", cmd_update_server_info, RUN_SETUP },
479479
{ "upload-archive", cmd_upload_archive },
480480
{ "upload-archive--writer", cmd_upload_archive_writer },
481+
{ "upload-pack", cmd_upload_pack },
481482
{ "var", cmd_var, RUN_SETUP_GENTLY },
482483
{ "verify-commit", cmd_verify_commit, RUN_SETUP },
483484
{ "verify-pack", cmd_verify_pack },

upload-pack.c

Lines changed: 25 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "tag.h"
77
#include "object.h"
88
#include "commit.h"
9-
#include "exec_cmd.h"
109
#include "diff.h"
1110
#include "revision.h"
1211
#include "list-objects.h"
@@ -15,15 +14,10 @@
1514
#include "sigchain.h"
1615
#include "version.h"
1716
#include "string-list.h"
18-
#include "parse-options.h"
1917
#include "argv-array.h"
2018
#include "prio-queue.h"
2119
#include "protocol.h"
22-
23-
static const char * const upload_pack_usage[] = {
24-
N_("git upload-pack [<options>] <dir>"),
25-
NULL
26-
};
20+
#include "upload-pack.h"
2721

2822
/* Remember to update object flag allocation in object.h */
2923
#define THEY_HAVE (1u << 11)
@@ -61,7 +55,6 @@ static int keepalive = 5;
6155
* otherwise maximum packet size (up to 65520 bytes).
6256
*/
6357
static int use_sideband;
64-
static int advertise_refs;
6558
static int stateless_rpc;
6659
static const char *pack_objects_hook;
6760

@@ -977,33 +970,6 @@ static int find_symref(const char *refname, const struct object_id *oid,
977970
return 0;
978971
}
979972

980-
static void upload_pack(void)
981-
{
982-
struct string_list symref = STRING_LIST_INIT_DUP;
983-
984-
head_ref_namespaced(find_symref, &symref);
985-
986-
if (advertise_refs || !stateless_rpc) {
987-
reset_timeout();
988-
head_ref_namespaced(send_ref, &symref);
989-
for_each_namespaced_ref(send_ref, &symref);
990-
advertise_shallow_grafts(1);
991-
packet_flush(1);
992-
} else {
993-
head_ref_namespaced(check_ref, NULL);
994-
for_each_namespaced_ref(check_ref, NULL);
995-
}
996-
string_list_clear(&symref, 1);
997-
if (advertise_refs)
998-
return;
999-
1000-
receive_needs();
1001-
if (want_obj.nr) {
1002-
get_common_commits();
1003-
create_pack_file();
1004-
}
1005-
}
1006-
1007973
static int upload_pack_config(const char *var, const char *value, void *unused)
1008974
{
1009975
if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
@@ -1032,58 +998,35 @@ static int upload_pack_config(const char *var, const char *value, void *unused)
1032998
return parse_hide_refs_config(var, value, "uploadpack");
1033999
}
10341000

1035-
int cmd_main(int argc, const char **argv)
1001+
void upload_pack(struct upload_pack_options *options)
10361002
{
1037-
const char *dir;
1038-
int strict = 0;
1039-
struct option options[] = {
1040-
OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1041-
N_("quit after a single request/response exchange")),
1042-
OPT_BOOL(0, "advertise-refs", &advertise_refs,
1043-
N_("exit immediately after initial ref advertisement")),
1044-
OPT_BOOL(0, "strict", &strict,
1045-
N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1046-
OPT_INTEGER(0, "timeout", &timeout,
1047-
N_("interrupt transfer after <n> seconds of inactivity")),
1048-
OPT_END()
1049-
};
1050-
1051-
packet_trace_identity("upload-pack");
1052-
check_replace_refs = 0;
1053-
1054-
argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
1055-
1056-
if (argc != 1)
1057-
usage_with_options(upload_pack_usage, options);
1058-
1059-
if (timeout)
1060-
daemon_mode = 1;
1061-
1062-
setup_path();
1063-
1064-
dir = argv[0];
1003+
struct string_list symref = STRING_LIST_INIT_DUP;
10651004

1066-
if (!enter_repo(dir, strict))
1067-
die("'%s' does not appear to be a git repository", dir);
1005+
stateless_rpc = options->stateless_rpc;
1006+
timeout = options->timeout;
1007+
daemon_mode = options->daemon_mode;
10681008

10691009
git_config(upload_pack_config, NULL);
10701010

1071-
switch (determine_protocol_version_server()) {
1072-
case protocol_v1:
1073-
/*
1074-
* v1 is just the original protocol with a version string,
1075-
* so just fall through after writing the version string.
1076-
*/
1077-
if (advertise_refs || !stateless_rpc)
1078-
packet_write_fmt(1, "version 1\n");
1079-
1080-
/* fallthrough */
1081-
case protocol_v0:
1082-
upload_pack();
1083-
break;
1084-
case protocol_unknown_version:
1085-
BUG("unknown protocol version");
1011+
head_ref_namespaced(find_symref, &symref);
1012+
1013+
if (options->advertise_refs || !stateless_rpc) {
1014+
reset_timeout();
1015+
head_ref_namespaced(send_ref, &symref);
1016+
for_each_namespaced_ref(send_ref, &symref);
1017+
advertise_shallow_grafts(1);
1018+
packet_flush(1);
1019+
} else {
1020+
head_ref_namespaced(check_ref, NULL);
1021+
for_each_namespaced_ref(check_ref, NULL);
10861022
}
1023+
string_list_clear(&symref, 1);
1024+
if (options->advertise_refs)
1025+
return;
10871026

1088-
return 0;
1027+
receive_needs();
1028+
if (want_obj.nr) {
1029+
get_common_commits();
1030+
create_pack_file();
1031+
}
10891032
}

upload-pack.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UPLOAD_PACK_H
2+
#define UPLOAD_PACK_H
3+
4+
struct upload_pack_options {
5+
int stateless_rpc;
6+
int advertise_refs;
7+
unsigned int timeout;
8+
int daemon_mode;
9+
};
10+
11+
void upload_pack(struct upload_pack_options *options);
12+
13+
#endif /* UPLOAD_PACK_H */

0 commit comments

Comments
 (0)