Skip to content

Commit 27d46a7

Browse files
committed
Merge branch 'mm/add-u-A-sans-pathspec'
Forbid "git add -u" and "git add -A" without pathspec run from a subdirectory, to train people to type "." (or ":/") to make the choice of default does not matter. * mm/add-u-A-sans-pathspec: add: warn when -u or -A is used without pathspec
2 parents 370855e + 0fa2eb5 commit 27d46a7

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

Documentation/git-add.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ apply to the index. See EDITING PATCHES below.
107107
from the index if the corresponding files in the working tree
108108
have been removed.
109109
+
110-
If no <filepattern> is given, default to "."; in other words,
111-
update all tracked files in the current directory and its
112-
subdirectories.
110+
If no <filepattern> is given, the current version of Git defaults to
111+
"."; in other words, update all tracked files in the current directory
112+
and its subdirectories. This default will change in a future version
113+
of Git, hence the form without <filepattern> should not be used.
113114

114115
-A::
115116
--all::

builtin/add.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,35 @@ static int add_files(struct dir_struct *dir, int flags)
321321
return exit_status;
322322
}
323323

324+
static void warn_pathless_add(const char *option_name, const char *short_name) {
325+
/*
326+
* To be consistent with "git add -p" and most Git
327+
* commands, we should default to being tree-wide, but
328+
* this is not the original behavior and can't be
329+
* changed until users trained themselves not to type
330+
* "git add -u" or "git add -A". For now, we warn and
331+
* keep the old behavior. Later, this warning can be
332+
* turned into a die(...), and eventually we may
333+
* reallow the command with a new behavior.
334+
*/
335+
warning(_("The behavior of 'git add %s (or %s)' with no path argument from a\n"
336+
"subdirectory of the tree will change in Git 2.0 and should not be used anymore.\n"
337+
"To add content for the whole tree, run:\n"
338+
"\n"
339+
" git add %s :/\n"
340+
" (or git add %s :/)\n"
341+
"\n"
342+
"To restrict the command to the current directory, run:\n"
343+
"\n"
344+
" git add %s .\n"
345+
" (or git add %s .)\n"
346+
"\n"
347+
"With the current Git version, the command is restricted to the current directory."),
348+
option_name, short_name,
349+
option_name, short_name,
350+
option_name, short_name);
351+
}
352+
324353
int cmd_add(int argc, const char **argv, const char *prefix)
325354
{
326355
int exit_status = 0;
@@ -331,6 +360,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
331360
int add_new_files;
332361
int require_pathspec;
333362
char *seen = NULL;
363+
const char *option_with_implicit_dot = NULL;
364+
const char *short_option_with_implicit_dot = NULL;
334365

335366
git_config(add_config, NULL);
336367

@@ -350,8 +381,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
350381
die(_("-A and -u are mutually incompatible"));
351382
if (!show_only && ignore_missing)
352383
die(_("Option --ignore-missing can only be used together with --dry-run"));
353-
if ((addremove || take_worktree_changes) && !argc) {
384+
if (addremove) {
385+
option_with_implicit_dot = "--all";
386+
short_option_with_implicit_dot = "-A";
387+
}
388+
if (take_worktree_changes) {
389+
option_with_implicit_dot = "--update";
390+
short_option_with_implicit_dot = "-u";
391+
}
392+
if (option_with_implicit_dot && !argc) {
354393
static const char *here[2] = { ".", NULL };
394+
if (prefix)
395+
warn_pathless_add(option_with_implicit_dot,
396+
short_option_with_implicit_dot);
355397
argc = 1;
356398
argv = here;
357399
}

0 commit comments

Comments
 (0)