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>
Brandon Williams committed
Mar 14, 2018 at 11:31 UTC
a3d6b53e92528d20fdaca504614f260c5b457b29
6 files changed
+109
-83
Makefile
+2
-1
@@ -636,7 +636,6 @@ PROGRAM_OBJS += imap-send.o
636
PROGRAM_OBJS += sh-i18n--envsubst.o
637
PROGRAM_OBJS += shell.o
638
PROGRAM_OBJS += show-index.o
639
-PROGRAM_OBJS += upload-pack.o
639
PROGRAM_OBJS += remote-testsvn.o
640
641
# Binary suffix, set to .exe for Windows builds
@@ -904,6 +903,7 @@ LIB_OBJS += tree-diff.o
903
LIB_OBJS += tree.o
904
LIB_OBJS += tree-walk.o
905
LIB_OBJS += unpack-trees.o
906
+LIB_OBJS += upload-pack.o
907
LIB_OBJS += url.o
908
LIB_OBJS += urlmatch.o
909
LIB_OBJS += usage.o
@@ -1021,6 +1021,7 @@ BUILTIN_OBJS += builtin/update-index.o
1021
BUILTIN_OBJS += builtin/update-ref.o
1022
BUILTIN_OBJS += builtin/update-server-info.o
1023
BUILTIN_OBJS += builtin/upload-archive.o
1024
+BUILTIN_OBJS += builtin/upload-pack.o
1025
BUILTIN_OBJS += builtin/var.o
1026
BUILTIN_OBJS += builtin/verify-commit.o
1027
BUILTIN_OBJS += builtin/verify-pack.o
builtin.h
+1
@@ -231,6 +231,7 @@ extern int cmd_update_ref(int argc, const char **argv, const char *prefix);
231
extern int cmd_update_server_info(int argc, const char **argv, const char *prefix);
232
extern int cmd_upload_archive(int argc, const char **argv, const char *prefix);
233
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);
235
extern int cmd_var(int argc, const char **argv, const char *prefix);
236
extern int cmd_verify_commit(int argc, const char **argv, const char *prefix);
237
extern int cmd_verify_tag(int argc, const char **argv, const char *prefix);
builtin/upload-pack.c
new
+67
@@ -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
+1
@@ -478,6 +478,7 @@ static struct cmd_struct commands[] = {
478
{ "update-server-info", cmd_update_server_info, RUN_SETUP },
479
{ "upload-archive", cmd_upload_archive },
480
{ "upload-archive--writer", cmd_upload_archive_writer },
481
+ { "upload-pack", cmd_upload_pack },
482
{ "var", cmd_var, RUN_SETUP_GENTLY },
483
{ "verify-commit", cmd_verify_commit, RUN_SETUP },
484
{ "verify-pack", cmd_verify_pack },
upload-pack.c
+25
-82
@@ -6,7 +6,6 @@
6
#include "tag.h"
7
#include "object.h"
8
#include "commit.h"
9
-#include "exec_cmd.h"
9
#include "diff.h"
10
#include "revision.h"
11
#include "list-objects.h"
@@ -15,15 +14,10 @@
14
#include "sigchain.h"
15
#include "version.h"
16
#include "string-list.h"
18
-#include "parse-options.h"
17
#include "argv-array.h"
18
#include "prio-queue.h"
19
#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"
21
22
/* Remember to update object flag allocation in object.h */
23
#define THEY_HAVE (1u << 11)
@@ -61,7 +55,6 @@ static int keepalive = 5;
55
* otherwise maximum packet size (up to 65520 bytes).
56
*/
57
static int use_sideband;
64
-static int advertise_refs;
58
static int stateless_rpc;
59
static const char *pack_objects_hook;
60
@@ -977,33 +970,6 @@ static int find_symref(const char *refname, const struct object_id *oid,
970
return 0;
971
}
972
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
-
973
static int upload_pack_config(const char *var, const char *value, void *unused)
974
{
975
if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
@@ -1032,58 +998,35 @@ static int upload_pack_config(const char *var, const char *value, void *unused)
998
return parse_hide_refs_config(var, value, "uploadpack");
999
}
1000
1035
-int cmd_main(int argc, const char **argv)
1001
+void upload_pack(struct upload_pack_options *options)
1002
{
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;
1004
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;
1008
1009
git_config(upload_pack_config, NULL);
1010
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);
1022
}
1023
+ string_list_clear(&symref, 1);
1024
+ if (options->advertise_refs)
1025
+ return;
1026
1088
- return 0;
1027
+ receive_needs();
1028
+ if (want_obj.nr) {
1029
+ get_common_commits();
1030
+ create_pack_file();
1031
+ }
1032
}
upload-pack.h
new
+13
@@ -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 */