Skip to content

Commit 524caf8

Browse files
committed
Merge branch 'js/reflog-anonymize-for-clone-and-fetch'
The reflog entries for "git clone" and "git fetch" did not anonymize the URL they operated on. * js/reflog-anonymize-for-clone-and-fetch: clone/fetch: anonymize URLs in the reflog
2 parents abacefe + 46da295 commit 524caf8

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

builtin/clone.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
945945
{
946946
int is_bundle = 0, is_local;
947947
const char *repo_name, *repo, *work_tree, *git_dir;
948-
char *path, *dir;
948+
char *path, *dir, *display_repo = NULL;
949949
int dest_exists;
950950
const struct ref *refs, *remote_head;
951951
const struct ref *remote_head_points_at;
@@ -1000,10 +1000,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10001000
path = get_repo_path(repo_name, &is_bundle);
10011001
if (path)
10021002
repo = absolute_pathdup(repo_name);
1003-
else if (!strchr(repo_name, ':'))
1004-
die(_("repository '%s' does not exist"), repo_name);
1005-
else
1003+
else if (strchr(repo_name, ':')) {
10061004
repo = repo_name;
1005+
display_repo = transport_anonymize_url(repo);
1006+
} else
1007+
die(_("repository '%s' does not exist"), repo_name);
10071008

10081009
/* no need to be strict, transport_set_option() will validate it again */
10091010
if (option_depth && atoi(option_depth) < 1)
@@ -1020,7 +1021,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10201021
die(_("destination path '%s' already exists and is not "
10211022
"an empty directory."), dir);
10221023

1023-
strbuf_addf(&reflog_msg, "clone: from %s", repo);
1024+
strbuf_addf(&reflog_msg, "clone: from %s",
1025+
display_repo ? display_repo : repo);
1026+
free(display_repo);
10241027

10251028
if (option_bare)
10261029
work_tree = NULL;

builtin/fetch.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,8 +1758,13 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
17581758

17591759
/* Record the command line for the reflog */
17601760
strbuf_addstr(&default_rla, "fetch");
1761-
for (i = 1; i < argc; i++)
1762-
strbuf_addf(&default_rla, " %s", argv[i]);
1761+
for (i = 1; i < argc; i++) {
1762+
/* This handles non-URLs gracefully */
1763+
char *anon = transport_anonymize_url(argv[i]);
1764+
1765+
strbuf_addf(&default_rla, " %s", anon);
1766+
free(anon);
1767+
}
17631768

17641769
fetch_config_from_gitmodules(&submodule_fetch_jobs_config,
17651770
&recurse_submodules);

t/t5541-http-push-smart.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,21 @@ test_expect_success 'push status output scrubs password' '
464464
grep "^To $HTTPD_URL/smart/test_repo.git" status
465465
'
466466

467+
test_expect_success 'clone/fetch scrubs password from reflogs' '
468+
cd "$ROOT_PATH" &&
469+
git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
470+
reflog-test &&
471+
cd reflog-test &&
472+
test_commit prepare-for-force-fetch &&
473+
git switch -c away &&
474+
git fetch "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
475+
+master:master &&
476+
# should have been scrubbed down to vanilla URL
477+
git log -g master >reflog &&
478+
grep "$HTTPD_URL" reflog &&
479+
! grep "$HTTPD_URL_USER_PASS" reflog
480+
'
481+
467482
test_expect_success 'colorize errors/hints' '
468483
cd "$ROOT_PATH"/test_repo_clone &&
469484
test_must_fail git -c color.transport=always -c color.advice=always \

0 commit comments

Comments
 (0)