git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 03/11] protocol: introduce protocol extension mechanisms

From
BWBrandon Williams <bmwill@google.com>
Date
Oct 16, 2017, 17:55 UTC
Message-ID
<20171016175532.73459-4-bmwill@google.com>
In-Reply-To
<20171016175532.73459-1-bmwill@google.com>

Create protocol.{c,h} and provide functions which future servers and clients can use to determine which protocol to use or is being used.

Also introduce the 'GIT_PROTOCOL' environment variable which will be used to communicate a colon separated list of keys with optional values to a server. Unknown keys and values must be tolerated. This mechanism is used to communicate which version of the wire protocol a client would like to use with a server.

Signed-off-by: Brandon Williams <bmwill@google.com>
---
 Documentation/config.txt | 17 +++++++++++
 Documentation/git.txt    |  6 ++++
 Makefile                 |  1 +
 cache.h                  |  8 +++++
 protocol.c               | 79 ++++++++++++++++++++++++++++++++++++++++++++++++
 protocol.h               | 33 ++++++++++++++++++++
 6 files changed, 144 insertions(+)
 create mode 100644 protocol.c
 create mode 100644 protocol.h
diff --git a/Documentation/config.txt b/Documentation/config.txt
index dc4e3f58a..b78747abc 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -2517,6 +2517,23 @@ The protocol names currently used by git are:
     `hg` to allow the `git-remote-hg` helper)
 --
 
+protocol.version::
+	Experimental. If set, clients will attempt to communicate with a
+	server using the specified protocol version.  If unset, no
+	attempt will be made by the client to communicate using a
+	particular protocol version, this results in protocol version 0
+	being used.
+	Supported versions:
++
+--
+
+* `0` - the original wire protocol.
+
+* `1` - the original wire protocol with the addition of a version string
+  in the initial response from the server.
+
+--
+
 pull.ff::
 	By default, Git does not create an extra merge commit when merging
 	a commit that is a descendant of the current commit. Instead, the
diff --git a/Documentation/git.txt b/Documentation/git.txt
index 6e3a6767e..7518ea3af 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -697,6 +697,12 @@ of clones and fetches.
 	which feed potentially-untrusted URLS to git commands.  See
 	linkgit:git-config[1] for more details.
 
+`GIT_PROTOCOL`::
+	For internal use only.  Used in handshaking the wire protocol.
+	Contains a colon ':' separated list of keys with optional values
+	'key[=value]'.  Presence of unknown keys and values must be
+	ignored.
+
 Discussion[[Discussion]]
 ------------------------
 
diff --git a/Makefile b/Makefile
index ed4ca438b..9ce68cded 100644
--- a/Makefile
+++ b/Makefile
@@ -842,6 +842,7 @@ LIB_OBJS += pretty.o
 LIB_OBJS += prio-queue.o
 LIB_OBJS += progress.o
 LIB_OBJS += prompt.o
+LIB_OBJS += protocol.o
 LIB_OBJS += quote.o
 LIB_OBJS += reachable.o
 LIB_OBJS += read-cache.o
diff --git a/cache.h b/cache.h
index 49b083ee0..c74b73671 100644
--- a/cache.h
+++ b/cache.h
@@ -445,6 +445,14 @@ static inline enum object_type object_type(unsigned int mode)
 #define GIT_ICASE_PATHSPECS_ENVIRONMENT "GIT_ICASE_PATHSPECS"
 #define GIT_QUARANTINE_ENVIRONMENT "GIT_QUARANTINE_PATH"
 
+/*
+ * Environment variable used in handshaking the wire protocol.
+ * Contains a colon ':' separated list of keys with optional values
+ * 'key[=value]'.  Presence of unknown keys and values must be
+ * ignored.
+ */
+#define GIT_PROTOCOL_ENVIRONMENT "GIT_PROTOCOL"
+
 /*
  * This environment variable is expected to contain a boolean indicating
  * whether we should or should not treat:
diff --git a/protocol.c b/protocol.c
new file mode 100644
index 000000000..43012b7eb
--- /dev/null
+++ b/protocol.c
@@ -0,0 +1,79 @@
+#include "cache.h"
+#include "config.h"
+#include "protocol.h"
+
+static enum protocol_version parse_protocol_version(const char *value)
+{
+	if (!strcmp(value, "0"))
+		return protocol_v0;
+	else if (!strcmp(value, "1"))
+		return protocol_v1;
+	else
+		return protocol_unknown_version;
+}
+
+enum protocol_version get_protocol_version_config(void)
+{
+	const char *value;
+	if (!git_config_get_string_const("protocol.version", &value)) {
+		enum protocol_version version = parse_protocol_version(value);
+
+		if (version == protocol_unknown_version)
+			die("unknown value for config 'protocol.version': %s",
+			    value);
+
+		return version;
+	}
+
+	return protocol_v0;
+}
+
+enum protocol_version determine_protocol_version_server(void)
+{
+	const char *git_protocol = getenv(GIT_PROTOCOL_ENVIRONMENT);
+	enum protocol_version version = protocol_v0;
+
+	/*
+	 * Determine which protocol version the client has requested.  Since
+	 * multiple 'version' keys can be sent by the client, indicating that
+	 * the client is okay to speak any of them, select the greatest version
+	 * that the client has requested.  This is due to the assumption that
+	 * the most recent protocol version will be the most state-of-the-art.
+	 */
+	if (git_protocol) {
+		struct string_list list = STRING_LIST_INIT_DUP;
+		const struct string_list_item *item;
+		string_list_split(&list, git_protocol, ':', -1);
+
+		for_each_string_list_item(item, &list) {
+			const char *value;
+			enum protocol_version v;
+
+			if (skip_prefix(item->string, "version=", &value)) {
+				v = parse_protocol_version(value);
+				if (v > version)
+					version = v;
+			}
+		}
+
+		string_list_clear(&list, 0);
+	}
+
+	return version;
+}
+
+enum protocol_version determine_protocol_version_client(const char *server_response)
+{
+	enum protocol_version version = protocol_v0;
+
+	if (skip_prefix(server_response, "version ", &server_response)) {
+		version = parse_protocol_version(server_response);
+
+		if (version == protocol_unknown_version)
+			die("server is speaking an unknown protocol");
+		if (version == protocol_v0)
+			die("protocol error: server explicitly said version 0");
+	}
+
+	return version;
+}
diff --git a/protocol.h b/protocol.h
new file mode 100644
index 000000000..1b2bc94a8
--- /dev/null
+++ b/protocol.h
@@ -0,0 +1,33 @@
+#ifndef PROTOCOL_H
+#define PROTOCOL_H
+
+enum protocol_version {
+	protocol_unknown_version = -1,
+	protocol_v0 = 0,
+	protocol_v1 = 1,
+};
+
+/*
+ * Used by a client to determine which protocol version to request be used when
+ * communicating with a server, reflecting the configured value of the
+ * 'protocol.version' config.  If unconfigured, a value of 'protocol_v0' is
+ * returned.
+ */
+extern enum protocol_version get_protocol_version_config(void);
+
+/*
+ * Used by a server to determine which protocol version should be used based on
+ * a client's request, communicated via the 'GIT_PROTOCOL' environment variable
+ * by setting appropriate values for the key 'version'.  If a client doesn't
+ * request a particular protocol version, a default of 'protocol_v0' will be
+ * used.
+ */
+extern enum protocol_version determine_protocol_version_server(void);
+
+/*
+ * Used by a client to determine which protocol version the server is speaking
+ * based on the server's initial response.
+ */
+extern enum protocol_version determine_protocol_version_client(const char *server_response);
+
+#endif /* PROTOCOL_H */
-- 
2.15.0.rc0.271.g36b669edcc-goog
Previous: Brandon WilliamsNext: Kevin Daudt
Message 152 of 161 in “protocol transition”
  1. 0/8 protocol transitionBrandon Williams, Sep 13, 2017
  2. 1/8 pkt-line: add packet_write functionBrandon Williams, Sep 13, 2017
  3. 2/8 protocol: introduce protocol extention mechanismsBrandon Williams, Sep 13, 2017
  4. Stefan BellerSep 13, 2017
  5. Brandon WilliamsSep 18, 2017
  6. Stefan BellerSep 18, 2017
  7. Brandon WilliamsSep 18, 2017
  8. Stefan BellerSep 18, 2017
  9. 3/8 daemon: recognize hidden request argumentsBrandon Williams, Sep 13, 2017
  10. Stefan BellerSep 13, 2017
  11. Brandon WilliamsSep 18, 2017
  12. Jonathan TanSep 21, 2017
  13. Jonathan TanSep 21, 2017
  14. Brandon WilliamsSep 21, 2017
  15. 5/8 connect: teach client to recognize v1 server responseBrandon Williams, Sep 13, 2017
  16. 4/8 upload-pack, receive-pack: introduce protocol version 1Brandon Williams, Sep 13, 2017
  17. 6/8 connect: tell server that the client understands v1Brandon Williams, Sep 13, 2017
  18. 8/8 i5700: add interop test for protocol transitionBrandon Williams, Sep 13, 2017
  19. 7/8 http: tell server that the client understands v1Brandon Williams, Sep 13, 2017
  20. 5/8 connect: die when a capability line comes after a refBrandon Williams, Sep 20, 2017
  21. Jeff KingSep 20, 2017
  22. Brandon WilliamsSep 20, 2017
  23. Jonathan NiederSep 20, 2017
  24. Junio C HamanoSep 21, 2017
  25. connect: in ref advertisement, shallows are lastJonathan Tan, Sep 21, 2017
  26. connect: in ref advertisement, shallows are lastJonathan Tan, Sep 21, 2017
  27. Brandon WilliamsSep 22, 2017
  28. connect: in ref advertisement, shallows are lastJonathan Tan, Sep 22, 2017
  29. Junio C HamanoSep 22, 2017
  30. Junio C HamanoSep 22, 2017
  31. Brandon WilliamsSep 22, 2017
  32. connect: in ref advertisement, shallows are lastJonathan Tan, Sep 22, 2017
  33. Brandon WilliamsSep 22, 2017
  34. Jonathan TanSep 22, 2017
  35. Junio C HamanoSep 24, 2017
  36. connect: in ref advertisement, shallows are lastJonathan Tan, Sep 26, 2017
  37. Brandon WilliamsSep 26, 2017
  38. 0/9 protocol transitionBrandon Williams, Sep 26, 2017
  39. 2/9 pkt-line: add packet_write functionBrandon Williams, Sep 26, 2017
  40. 3/9 protocol: introduce protocol extention mechanismsBrandon Williams, Sep 26, 2017
  41. Junio C HamanoSep 27, 2017
  42. Junio C HamanoSep 27, 2017
  43. Brandon WilliamsSep 29, 2017
  44. Brandon WilliamsSep 28, 2017
  45. Stefan BellerSep 27, 2017
  46. Brandon WilliamsSep 28, 2017
  47. 1/9 connect: in ref advertisement, shallows are lastBrandon Williams, Sep 26, 2017
  48. 6/9 connect: teach client to recognize v1 server responseBrandon Williams, Sep 26, 2017
  49. Junio C HamanoSep 27, 2017
  50. Brandon WilliamsSep 27, 2017
  51. Junio C HamanoSep 27, 2017
  52. Brandon WilliamsSep 28, 2017
  53. 7/9 connect: tell server that the client understands v1Brandon Williams, Sep 26, 2017
  54. Junio C HamanoSep 27, 2017
  55. Junio C HamanoSep 27, 2017
  56. Brandon WilliamsSep 29, 2017
  57. Brandon WilliamsSep 28, 2017
  58. 9/9 i5700: add interop test for protocol transitionBrandon Williams, Sep 26, 2017
  59. 8/9 http: tell server that the client understands v1Brandon Williams, Sep 26, 2017
  60. Junio C HamanoSep 27, 2017
  61. Brandon WilliamsSep 27, 2017
  62. 5/9 upload-pack, receive-pack: introduce protocol version 1Brandon Williams, Sep 26, 2017
  63. Junio C HamanoSep 27, 2017
  64. Brandon WilliamsSep 27, 2017
  65. 4/9 daemon: recognize hidden request argumentsBrandon Williams, Sep 26, 2017
  66. Junio C HamanoSep 27, 2017
  67. Brandon WilliamsSep 27, 2017
  68. Brandon WilliamsSep 28, 2017
  69. 00/10 protocol transitionBrandon Williams, Oct 3, 2017
  70. 04/10 daemon: recognize hidden request argumentsBrandon Williams, Oct 3, 2017
  71. Jonathan TanOct 10, 2017
  72. Brandon WilliamsOct 13, 2017
  73. 03/10 protocol: introduce protocol extention mechanismsBrandon Williams, Oct 3, 2017
  74. Simon RuderichOct 6, 2017
  75. Junio C HamanoOct 6, 2017
  76. Martin ÅgrenOct 6, 2017
  77. Junio C HamanoOct 6, 2017
  78. Martin ÅgrenOct 6, 2017
  79. Stefan BellerOct 6, 2017
  80. Martin ÅgrenOct 8, 2017
  81. Brandon WilliamsOct 10, 2017
  82. Jonathan NiederOct 10, 2017
  83. Stefan BellerOct 10, 2017
  84. Junio C HamanoOct 11, 2017
  85. Brandon WilliamsOct 13, 2017
  86. Martin ÅgrenOct 9, 2017
  87. Jonathan TanOct 10, 2017
  88. 02/10 pkt-line: add packet_write functionBrandon Williams, Oct 3, 2017
  89. Jonathan TanOct 10, 2017
  90. 05/10 upload-pack, receive-pack: introduce protocol version 1Brandon Williams, Oct 3, 2017
  91. Jonathan TanOct 10, 2017
  92. Brandon WilliamsOct 13, 2017
  93. 01/10 connect: in ref advertisement, shallows are lastBrandon Williams, Oct 3, 2017
  94. Jonathan TanOct 10, 2017
  95. 09/10 i5700: add interop test for protocol transitionBrandon Williams, Oct 3, 2017
  96. 08/10 http: tell server that the client understands v1Brandon Williams, Oct 3, 2017
  97. 10/10 ssh: introduce a 'simple' ssh variantBrandon Williams, Oct 3, 2017
  98. Jonathan NiederOct 3, 2017
  99. Brandon WilliamsOct 16, 2017
  100. 0/5 Coping with unrecognized ssh wrapper scripts in GIT_SSHJonathan Nieder, Oct 23, 2017
  101. 1/5 connect: split git:// setup into a separate functionJonathan Nieder, Oct 23, 2017
  102. Stefan BellerOct 23, 2017
  103. diff: add option to ignore whitespaces for move detection onlyStefan Beller, Oct 24, 2017
  104. Brandon WilliamsOct 24, 2017
  105. Junio C HamanoOct 25, 2017
  106. Junio C HamanoOct 25, 2017
  107. Brandon WilliamsOct 25, 2017
  108. Junio C HamanoOct 24, 2017
  109. Stefan BellerOct 24, 2017
  110. 2/5 connect: split ssh command line options into separate functionJonathan Nieder, Oct 23, 2017
  111. Stefan BellerOct 23, 2017
  112. 3/5 ssh: 'auto' variant to select between 'ssh' and 'simple'Jonathan Nieder, Oct 23, 2017
  113. Jonathan TanOct 23, 2017
  114. Jonathan NiederOct 23, 2017
  115. Brandon WilliamsOct 23, 2017
  116. Jonathan TanOct 23, 2017
  117. 0/5 Coping with unrecognized ssh wrapper scripts in GIT_SSHJonathan Nieder, Oct 23, 2017
  118. 1/5 connect: split git:// setup into a separate functionJonathan Nieder, Oct 23, 2017
  119. Junio C HamanoOct 24, 2017
  120. Jonathan NiederNov 15, 2017
  121. Junio C HamanoNov 17, 2017
  122. 2/5 connect: split ssh command line options into separate functionJonathan Nieder, Oct 23, 2017
  123. Junio C HamanoOct 24, 2017
  124. 3/5 ssh: 'auto' variant to select between 'ssh' and 'simple'Jonathan Nieder, Oct 23, 2017
  125. Brandon WilliamsOct 23, 2017
  126. Stefan BellerOct 23, 2017
  127. 4/5 ssh: 'simple' variant does not support -4/-6Jonathan Nieder, Oct 23, 2017
  128. 5/5 ssh: 'simple' variant does not support --portJonathan Nieder, Oct 23, 2017
  129. Junio C HamanoOct 24, 2017
  130. Jonathan NiederOct 23, 2017
  131. Stefan BellerOct 23, 2017
  132. Jonathan NiederOct 23, 2017
  133. Junio C HamanoOct 24, 2017
  134. Johannes SchindelinOct 25, 2017
  135. Stefan BellerOct 25, 2017
  136. Jonathan NiederOct 25, 2017
  137. Junio C HamanoOct 30, 2017
  138. Johannes SchindelinOct 30, 2017
  139. 4/5 ssh: 'simple' variant does not support -4/-6Jonathan Nieder, Oct 23, 2017
  140. 5/5 ssh: 'simple' variant does not support --portJonathan Nieder, Oct 23, 2017
  141. Stefan BellerOct 23, 2017
  142. 07/10 connect: tell server that the client understands v1Brandon Williams, Oct 3, 2017
  143. Jonathan TanOct 10, 2017
  144. Brandon WilliamsOct 13, 2017
  145. 06/10 connect: teach client to recognize v1 server responseBrandon Williams, Oct 3, 2017
  146. Junio C HamanoOct 4, 2017
  147. Documentation: document Extra ParametersJonathan Tan, Oct 10, 2017
  148. Brandon WilliamsOct 13, 2017
  149. 00/11 protocol transitionBrandon Williams, Oct 16, 2017
  150. 01/11 connect: in ref advertisement, shallows are lastBrandon Williams, Oct 16, 2017
  151. 02/11 pkt-line: add packet_write functionBrandon Williams, Oct 16, 2017
  152. 03/11 protocol: introduce protocol extension mechanismsBrandon Williams, Oct 16, 2017
  153. Kevin DaudtOct 16, 2017
  154. 06/11 connect: teach client to recognize v1 server responseBrandon Williams, Oct 16, 2017
  155. 04/11 daemon: recognize hidden request argumentsBrandon Williams, Oct 16, 2017
  156. 09/11 i5700: add interop test for protocol transitionBrandon Williams, Oct 16, 2017
  157. 08/11 http: tell server that the client understands v1Brandon Williams, Oct 16, 2017
  158. 05/11 upload-pack, receive-pack: introduce protocol version 1Brandon Williams, Oct 16, 2017
  159. 07/11 connect: tell server that the client understands v1Brandon Williams, Oct 16, 2017
  160. 10/11 ssh: introduce a 'simple' ssh variantBrandon Williams, Oct 16, 2017
  161. 11/11 Documentation: document Extra ParametersBrandon Williams, Oct 16, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.