Skip to content

Commit 8f31543

Browse files
committed
Merge branch 'rj/use-adv-if-enabled'
Use advice_if_enabled() API to rewrite a simple pattern to call advise() after checking advice_enabled(). * rj/use-adv-if-enabled: add: use advise_if_enabled for ADVICE_ADD_EMBEDDED_REPO add: use advise_if_enabled for ADVICE_ADD_EMPTY_PATHSPEC add: use advise_if_enabled for ADVICE_ADD_IGNORED_FILE
2 parents eacfd58 + 6412d01 commit 8f31543

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

builtin/add.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,9 @@ static void check_embedded_repo(const char *path)
310310
strbuf_strip_suffix(&name, "/");
311311

312312
warning(_("adding embedded git repository: %s"), name.buf);
313-
if (!adviced_on_embedded_repo &&
314-
advice_enabled(ADVICE_ADD_EMBEDDED_REPO)) {
315-
advise(embedded_advice, name.buf, name.buf);
313+
if (!adviced_on_embedded_repo) {
314+
advise_if_enabled(ADVICE_ADD_EMBEDDED_REPO,
315+
embedded_advice, name.buf, name.buf);
316316
adviced_on_embedded_repo = 1;
317317
}
318318

@@ -328,10 +328,8 @@ static int add_files(struct dir_struct *dir, int flags)
328328
fprintf(stderr, _(ignore_error));
329329
for (i = 0; i < dir->ignored_nr; i++)
330330
fprintf(stderr, "%s\n", dir->ignored[i]->name);
331-
if (advice_enabled(ADVICE_ADD_IGNORED_FILE))
332-
advise(_("Use -f if you really want to add them.\n"
333-
"Turn this message off by running\n"
334-
"\"git config advice.addIgnoredFile false\""));
331+
advise_if_enabled(ADVICE_ADD_IGNORED_FILE,
332+
_("Use -f if you really want to add them."));
335333
exit_status = 1;
336334
}
337335

@@ -440,10 +438,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
440438

441439
if (require_pathspec && pathspec.nr == 0) {
442440
fprintf(stderr, _("Nothing specified, nothing added.\n"));
443-
if (advice_enabled(ADVICE_ADD_EMPTY_PATHSPEC))
444-
advise( _("Maybe you wanted to say 'git add .'?\n"
445-
"Turn this message off by running\n"
446-
"\"git config advice.addEmptyPathspec false\""));
441+
advise_if_enabled(ADVICE_ADD_EMPTY_PATHSPEC,
442+
_("Maybe you wanted to say 'git add .'?"));
447443
return 0;
448444
}
449445

t/t3700-add.sh

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ test_expect_success 'Test of git add' '
2828
touch foo && git add foo
2929
'
3030

31+
test_expect_success 'Test with no pathspecs' '
32+
cat >expect <<-EOF &&
33+
Nothing specified, nothing added.
34+
hint: Maybe you wanted to say ${SQ}git add .${SQ}?
35+
hint: Disable this message with "git config advice.addEmptyPathspec false"
36+
EOF
37+
git add 2>actual &&
38+
test_cmp expect actual
39+
'
40+
3141
test_expect_success 'Post-check that foo is in the index' '
3242
git ls-files foo | grep foo
3343
'
@@ -339,6 +349,40 @@ test_expect_success '"git add ." in empty repo' '
339349
)
340350
'
341351

352+
test_expect_success '"git add" a embedded repository' '
353+
rm -fr outer && git init outer &&
354+
(
355+
cd outer &&
356+
for i in 1 2
357+
do
358+
name=inner$i &&
359+
git init $name &&
360+
git -C $name commit --allow-empty -m $name ||
361+
return 1
362+
done &&
363+
git add . 2>actual &&
364+
cat >expect <<-EOF &&
365+
warning: adding embedded git repository: inner1
366+
hint: You${SQ}ve added another git repository inside your current repository.
367+
hint: Clones of the outer repository will not contain the contents of
368+
hint: the embedded repository and will not know how to obtain it.
369+
hint: If you meant to add a submodule, use:
370+
hint:
371+
hint: git submodule add <url> inner1
372+
hint:
373+
hint: If you added this path by mistake, you can remove it from the
374+
hint: index with:
375+
hint:
376+
hint: git rm --cached inner1
377+
hint:
378+
hint: See "git help submodule" for more information.
379+
hint: Disable this message with "git config advice.addEmbeddedRepo false"
380+
warning: adding embedded git repository: inner2
381+
EOF
382+
test_cmp expect actual
383+
)
384+
'
385+
342386
test_expect_success 'error on a repository with no commits' '
343387
rm -fr empty &&
344388
git init empty &&
@@ -370,8 +414,7 @@ cat >expect.err <<\EOF
370414
The following paths are ignored by one of your .gitignore files:
371415
ignored-file
372416
hint: Use -f if you really want to add them.
373-
hint: Turn this message off by running
374-
hint: "git config advice.addIgnoredFile false"
417+
hint: Disable this message with "git config advice.addIgnoredFile false"
375418
EOF
376419
cat >expect.out <<\EOF
377420
add 'track-this'

t/t7400-submodule-basic.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ test_expect_success 'submodule add to .gitignored path fails' '
212212
The following paths are ignored by one of your .gitignore files:
213213
submod
214214
hint: Use -f if you really want to add them.
215-
hint: Turn this message off by running
216-
hint: "git config advice.addIgnoredFile false"
215+
hint: Disable this message with "git config advice.addIgnoredFile false"
217216
EOF
218217
# Does not use test_commit due to the ignore
219218
echo "*" > .gitignore &&

0 commit comments

Comments
 (0)