Skip to content

Commit 172786e

Browse files
committed
examples: use username provided via URL
The credentials callback may be passed a username in case where the URL already includes the expected username. As we usually cannot use a different username in such context, we should use that one if provided and not ask the user for a diferent username.
1 parent 611fbe4 commit 172786e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

examples/common.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,19 @@ int cred_acquire_cb(git_cred **out,
353353
int error = 1;
354354

355355
UNUSED(url);
356-
UNUSED(username_from_url);
357356
UNUSED(payload);
358357

358+
if (username_from_url) {
359+
if ((username = strdup(username_from_url)) == NULL)
360+
goto out;
361+
} else if ((error = ask(&username, "Username:", 0)) < 0) {
362+
goto out;
363+
}
364+
359365
if (allowed_types & GIT_CREDTYPE_SSH_KEY) {
360366
int n;
361367

362-
if ((error = ask(&username, "Username:", 0)) < 0 ||
363-
(error = ask(&privkey, "SSH Key:", 0)) < 0 ||
368+
if ((error = ask(&privkey, "SSH Key:", 0)) < 0 ||
364369
(error = ask(&password, "Password:", 1)) < 0)
365370
goto out;
366371

@@ -371,15 +376,11 @@ int cred_acquire_cb(git_cred **out,
371376

372377
error = git_cred_ssh_key_new(out, username, pubkey, privkey, password);
373378
} else if (allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT) {
374-
if ((error = ask(&username, "Username:", 0)) < 0 ||
375-
(error = ask(&password, "Password:", 1)) < 0)
379+
if ((error = ask(&password, "Password:", 1)) < 0)
376380
goto out;
377381

378382
error = git_cred_userpass_plaintext_new(out, username, password);
379383
} else if (allowed_types & GIT_CREDTYPE_USERNAME) {
380-
if ((error = ask(&username, "Username:", 0)) < 0)
381-
goto out;
382-
383384
error = git_cred_username_new(out, username);
384385
}
385386

0 commit comments

Comments
 (0)