Skip to content

Commit 2d6dc18

Browse files
moygitster
authored andcommitted
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>
1 parent e30b2fe commit 2d6dc18

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

Documentation/git-credential.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,20 @@ infomation it has):
6464

6565
2. Ask git-credential to give us a username and password for this
6666
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:
6970

71+
protocol=https
72+
host=example.com
7073
username=bob
7174
password=secr3t
7275
+
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+
+
7381
If the `git credential` knew about the password, this step may
7482
not have involved the user actually typing this password (the
7583
user may have typed a password to unlock the keychain instead,
@@ -86,8 +94,8 @@ unlocked) before it returned `password=secr3t`.
8694
was rejected during the operation, use the "reject" action so
8795
that `git credential` will ask for a new password in its next
8896
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)).
9199

92100
[[IOFMT]]
93101
INPUT/OUTPUT FORMAT

builtin/credential.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ int cmd_credential(int argc, const char **argv, const char *prefix)
1919

2020
if (!strcmp(op, "fill")) {
2121
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);
2623
} else if (!strcmp(op, "approve")) {
2724
credential_approve(&c);
2825
} else if (!strcmp(op, "reject")) {

credential.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static void credential_write_item(FILE *fp, const char *key, const char *value)
191191
fprintf(fp, "%s=%s\n", key, value);
192192
}
193193

194-
static void credential_write(const struct credential *c, FILE *fp)
194+
void credential_write(const struct credential *c, FILE *fp)
195195
{
196196
credential_write_item(fp, "protocol", c->protocol);
197197
credential_write_item(fp, "host", c->host);

credential.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void credential_approve(struct credential *);
2626
void credential_reject(struct credential *);
2727

2828
int credential_read(struct credential *, FILE *);
29+
void credential_write(const struct credential *, FILE *);
2930
void credential_from_url(struct credential *, const char *url);
3031
int credential_match(const struct credential *have,
3132
const struct credential *want);

t/lib-credential.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ helper_test() {
6262
protocol=https
6363
host=example.com
6464
--
65+
protocol=https
66+
host=example.com
6567
username=askpass-username
6668
password=askpass-password
6769
--
@@ -84,6 +86,8 @@ helper_test() {
8486
protocol=https
8587
host=example.com
8688
--
89+
protocol=https
90+
host=example.com
8791
username=store-user
8892
password=store-pass
8993
--
@@ -95,6 +99,8 @@ helper_test() {
9599
protocol=http
96100
host=example.com
97101
--
102+
protocol=http
103+
host=example.com
98104
username=askpass-username
99105
password=askpass-password
100106
--
@@ -108,6 +114,8 @@ helper_test() {
108114
protocol=https
109115
host=other.tld
110116
--
117+
protocol=https
118+
host=other.tld
111119
username=askpass-username
112120
password=askpass-password
113121
--
@@ -122,6 +130,8 @@ helper_test() {
122130
host=example.com
123131
username=other
124132
--
133+
protocol=https
134+
host=example.com
125135
username=other
126136
password=askpass-password
127137
--
@@ -143,6 +153,9 @@ helper_test() {
143153
host=path.tld
144154
path=bar.git
145155
--
156+
protocol=http
157+
host=path.tld
158+
path=bar.git
146159
username=askpass-username
147160
password=askpass-password
148161
--
@@ -160,6 +173,8 @@ helper_test() {
160173
protocol=https
161174
host=example.com
162175
--
176+
protocol=https
177+
host=example.com
163178
username=askpass-username
164179
password=askpass-password
165180
--
@@ -186,6 +201,8 @@ helper_test() {
186201
host=example.com
187202
username=user1
188203
--
204+
protocol=https
205+
host=example.com
189206
username=user1
190207
password=pass1
191208
EOF
@@ -194,6 +211,8 @@ helper_test() {
194211
host=example.com
195212
username=user2
196213
--
214+
protocol=https
215+
host=example.com
197216
username=user2
198217
password=pass2
199218
EOF
@@ -210,6 +229,8 @@ helper_test() {
210229
host=example.com
211230
username=user1
212231
--
232+
protocol=https
233+
host=example.com
213234
username=user1
214235
password=askpass-password
215236
--
@@ -223,6 +244,8 @@ helper_test() {
223244
host=example.com
224245
username=user2
225246
--
247+
protocol=https
248+
host=example.com
226249
username=user2
227250
password=pass2
228251
EOF
@@ -244,6 +267,8 @@ helper_test_timeout() {
244267
protocol=https
245268
host=timeout.tld
246269
--
270+
protocol=https
271+
host=timeout.tld
247272
username=askpass-username
248273
password=askpass-password
249274
--

t/t0300-credentials.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ test_expect_success 'credential_fill passes along metadata' '
8282
host=example.com
8383
path=foo.git
8484
--
85+
protocol=ftp
86+
host=example.com
87+
path=foo.git
8588
username=one
8689
password=two
8790
--
@@ -213,6 +216,8 @@ test_expect_success 'match configured credential' '
213216
host=example.com
214217
path=repo.git
215218
--
219+
protocol=https
220+
host=example.com
216221
username=foo
217222
password=bar
218223
--
@@ -225,6 +230,8 @@ test_expect_success 'do not match configured credential' '
225230
protocol=https
226231
host=bar
227232
--
233+
protocol=https
234+
host=bar
228235
username=askpass-username
229236
password=askpass-password
230237
--
@@ -239,6 +246,8 @@ test_expect_success 'pull username from config' '
239246
protocol=https
240247
host=example.com
241248
--
249+
protocol=https
250+
host=example.com
242251
username=foo
243252
password=askpass-password
244253
--
@@ -252,6 +261,8 @@ test_expect_success 'http paths can be part of context' '
252261
host=example.com
253262
path=foo.git
254263
--
264+
protocol=https
265+
host=example.com
255266
username=foo
256267
password=bar
257268
--
@@ -265,6 +276,9 @@ test_expect_success 'http paths can be part of context' '
265276
host=example.com
266277
path=foo.git
267278
--
279+
protocol=https
280+
host=example.com
281+
path=foo.git
268282
username=foo
269283
password=bar
270284
--

0 commit comments

Comments
 (0)