git credential fill: output the whole 'struct credential'

Instead of outputing only the username and password, print all the attributes, even those that already appeared in the input. This is closer to what the C API does, and allows one to take the exact output of "git credential fill" as input to "git credential approve" or "git credential reject". Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Jun 24, 2012 at 13:40 UTC 2d6dc182b8fa171a6b283ce6e8e75a35e13ea67a
6 files changed +54 -9
Documentation/git-credential.txt
+12 -4
@@ -64,12 +64,20 @@ infomation it has):
64
65 2. Ask git-credential to give us a username and password for this
66 description. This is done by running `git credential fill`,
67 - feeding the description from step (1) to its standard input. The
68 - credential will be produced on standard output, like:
67 + feeding the description from step (1) to its standard input. The complete
68 + credential description (including the credential per se, i.e. the
69 + login and password) will be produced on standard output, like:
70
71 + protocol=https
72 + host=example.com
73 username=bob
74 password=secr3t
75 +
76 +In most cases, this means the attributes given in the input will be
77 +repeated in the output, but git may also modify the credential
78 +description, for example by removing the `path` attribute when the
79 +protocol is HTTP(s) and `credential.useHttpPath` is false.
80 ++
81 If the `git credential` knew about the password, this step may
82 not have involved the user actually typing this password (the
83 user may have typed a password to unlock the keychain instead,
@@ -86,8 +94,8 @@ unlocked) before it returned `password=secr3t`.
94 was rejected during the operation, use the "reject" action so
95 that `git credential` will ask for a new password in its next
96 invocation. In either case, `git credential` should be fed with
89 - the credential description obtained from step (2) together with
90 - the ones already provided in step (1).
97 + the credential description obtained from step (2) (which also
98 + contain the ones provided in step (1)).
99
100 [[IOFMT]]
101 INPUT/OUTPUT FORMAT
builtin/credential.c
+1 -4
@@ -19,10 +19,7 @@ int cmd_credential(int argc, const char **argv, const char *prefix)
19
20 if (!strcmp(op, "fill")) {
21 credential_fill(&c);
22 - if (c.username)
23 - printf("username=%s\n", c.username);
24 - if (c.password)
25 - printf("password=%s\n", c.password);
22 + credential_write(&c, stdout);
23 } else if (!strcmp(op, "approve")) {
24 credential_approve(&c);
25 } else if (!strcmp(op, "reject")) {
credential.c
+1 -1
@@ -191,7 +191,7 @@ static void credential_write_item(FILE *fp, const char *key, const char *value)
191 fprintf(fp, "%s=%s\n", key, value);
192 }
193
194 -static void credential_write(const struct credential *c, FILE *fp)
194 +void credential_write(const struct credential *c, FILE *fp)
195 {
196 credential_write_item(fp, "protocol", c->protocol);
197 credential_write_item(fp, "host", c->host);
credential.h
+1
@@ -26,6 +26,7 @@ void credential_approve(struct credential *);
26 void credential_reject(struct credential *);
27
28 int credential_read(struct credential *, FILE *);
29 +void credential_write(const struct credential *, FILE *);
30 void credential_from_url(struct credential *, const char *url);
31 int credential_match(const struct credential *have,
32 const struct credential *want);
t/lib-credential.sh
+25
@@ -62,6 +62,8 @@ helper_test() {
62 protocol=https
63 host=example.com
64 --
65 + protocol=https
66 + host=example.com
67 username=askpass-username
68 password=askpass-password
69 --
@@ -84,6 +86,8 @@ helper_test() {
86 protocol=https
87 host=example.com
88 --
89 + protocol=https
90 + host=example.com
91 username=store-user
92 password=store-pass
93 --
@@ -95,6 +99,8 @@ helper_test() {
99 protocol=http
100 host=example.com
101 --
102 + protocol=http
103 + host=example.com
104 username=askpass-username
105 password=askpass-password
106 --
@@ -108,6 +114,8 @@ helper_test() {
114 protocol=https
115 host=other.tld
116 --
117 + protocol=https
118 + host=other.tld
119 username=askpass-username
120 password=askpass-password
121 --
@@ -122,6 +130,8 @@ helper_test() {
130 host=example.com
131 username=other
132 --
133 + protocol=https
134 + host=example.com
135 username=other
136 password=askpass-password
137 --
@@ -143,6 +153,9 @@ helper_test() {
153 host=path.tld
154 path=bar.git
155 --
156 + protocol=http
157 + host=path.tld
158 + path=bar.git
159 username=askpass-username
160 password=askpass-password
161 --
@@ -160,6 +173,8 @@ helper_test() {
173 protocol=https
174 host=example.com
175 --
176 + protocol=https
177 + host=example.com
178 username=askpass-username
179 password=askpass-password
180 --
@@ -186,6 +201,8 @@ helper_test() {
201 host=example.com
202 username=user1
203 --
204 + protocol=https
205 + host=example.com
206 username=user1
207 password=pass1
208 EOF
@@ -194,6 +211,8 @@ helper_test() {
211 host=example.com
212 username=user2
213 --
214 + protocol=https
215 + host=example.com
216 username=user2
217 password=pass2
218 EOF
@@ -210,6 +229,8 @@ helper_test() {
229 host=example.com
230 username=user1
231 --
232 + protocol=https
233 + host=example.com
234 username=user1
235 password=askpass-password
236 --
@@ -223,6 +244,8 @@ helper_test() {
244 host=example.com
245 username=user2
246 --
247 + protocol=https
248 + host=example.com
249 username=user2
250 password=pass2
251 EOF
@@ -244,6 +267,8 @@ helper_test_timeout() {
267 protocol=https
268 host=timeout.tld
269 --
270 + protocol=https
271 + host=timeout.tld
272 username=askpass-username
273 password=askpass-password
274 --
t/t0300-credentials.sh
+14
@@ -82,6 +82,9 @@ test_expect_success 'credential_fill passes along metadata' '
82 host=example.com
83 path=foo.git
84 --
85 + protocol=ftp
86 + host=example.com
87 + path=foo.git
88 username=one
89 password=two
90 --
@@ -213,6 +216,8 @@ test_expect_success 'match configured credential' '
216 host=example.com
217 path=repo.git
218 --
219 + protocol=https
220 + host=example.com
221 username=foo
222 password=bar
223 --
@@ -225,6 +230,8 @@ test_expect_success 'do not match configured credential' '
230 protocol=https
231 host=bar
232 --
233 + protocol=https
234 + host=bar
235 username=askpass-username
236 password=askpass-password
237 --
@@ -239,6 +246,8 @@ test_expect_success 'pull username from config' '
246 protocol=https
247 host=example.com
248 --
249 + protocol=https
250 + host=example.com
251 username=foo
252 password=askpass-password
253 --
@@ -252,6 +261,8 @@ test_expect_success 'http paths can be part of context' '
261 host=example.com
262 path=foo.git
263 --
264 + protocol=https
265 + host=example.com
266 username=foo
267 password=bar
268 --
@@ -265,6 +276,9 @@ test_expect_success 'http paths can be part of context' '
276 host=example.com
277 path=foo.git
278 --
279 + protocol=https
280 + host=example.com
281 + path=foo.git
282 username=foo
283 password=bar
284 --