2007-11-12 01:48:47 +00:00
|
|
|
/*
|
|
|
|
* "git clean" builtin command
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Shawn Bohrer
|
|
|
|
*
|
|
|
|
* Based on git-clean.sh by Pavel Roskin
|
|
|
|
*/
|
|
|
|
|
2022-11-19 13:07:38 +00:00
|
|
|
#define USE_THE_INDEX_VARIABLE
|
2007-11-12 01:48:47 +00:00
|
|
|
#include "builtin.h"
|
2023-03-21 06:25:58 +00:00
|
|
|
#include "abspath.h"
|
2007-11-12 01:48:47 +00:00
|
|
|
#include "cache.h"
|
2017-06-14 18:07:36 +00:00
|
|
|
#include "config.h"
|
2007-11-12 01:48:47 +00:00
|
|
|
#include "dir.h"
|
2023-03-21 06:25:54 +00:00
|
|
|
#include "gettext.h"
|
2007-11-12 01:48:47 +00:00
|
|
|
#include "parse-options.h"
|
2023-03-21 06:26:05 +00:00
|
|
|
#include "setup.h"
|
2010-07-20 19:35:56 +00:00
|
|
|
#include "string-list.h"
|
2008-03-07 01:13:17 +00:00
|
|
|
#include "quote.h"
|
2013-06-25 15:53:49 +00:00
|
|
|
#include "column.h"
|
2013-06-25 15:53:50 +00:00
|
|
|
#include "color.h"
|
2013-07-14 08:35:37 +00:00
|
|
|
#include "pathspec.h"
|
2018-05-26 13:55:24 +00:00
|
|
|
#include "help.h"
|
2020-04-10 11:27:50 +00:00
|
|
|
#include "prompt.h"
|
2007-11-12 01:48:47 +00:00
|
|
|
|
2007-11-13 05:13:05 +00:00
|
|
|
static int force = -1; /* unset */
|
2013-06-25 15:53:48 +00:00
|
|
|
static int interactive;
|
2013-06-25 15:53:47 +00:00
|
|
|
static struct string_list del_list = STRING_LIST_INIT_DUP;
|
2013-06-25 15:53:49 +00:00
|
|
|
static unsigned int colopts;
|
2007-11-12 01:48:47 +00:00
|
|
|
|
|
|
|
static const char *const builtin_clean_usage[] = {
|
2022-10-13 15:39:21 +00:00
|
|
|
N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] [<pathspec>...]"),
|
2007-11-12 01:48:47 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
static const char *msg_remove = N_("Removing %s\n");
|
|
|
|
static const char *msg_would_remove = N_("Would remove %s\n");
|
|
|
|
static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
|
|
|
|
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
|
|
|
|
static const char *msg_warn_remove_failed = N_("failed to remove %s");
|
2019-07-18 09:30:33 +00:00
|
|
|
static const char *msg_warn_lstat_failed = N_("could not lstat %s\n");
|
2021-12-09 05:08:30 +00:00
|
|
|
static const char *msg_skip_cwd = N_("Refusing to remove current working directory\n");
|
|
|
|
static const char *msg_would_skip_cwd = N_("Would refuse to remove current working directory\n");
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
|
2013-06-25 15:53:50 +00:00
|
|
|
enum color_clean {
|
|
|
|
CLEAN_COLOR_RESET = 0,
|
|
|
|
CLEAN_COLOR_PLAIN = 1,
|
|
|
|
CLEAN_COLOR_PROMPT = 2,
|
|
|
|
CLEAN_COLOR_HEADER = 3,
|
|
|
|
CLEAN_COLOR_HELP = 4,
|
2014-07-02 18:24:05 +00:00
|
|
|
CLEAN_COLOR_ERROR = 5
|
2013-06-25 15:53:50 +00:00
|
|
|
};
|
|
|
|
|
2018-05-26 13:55:21 +00:00
|
|
|
static const char *color_interactive_slots[] = {
|
|
|
|
[CLEAN_COLOR_ERROR] = "error",
|
|
|
|
[CLEAN_COLOR_HEADER] = "header",
|
|
|
|
[CLEAN_COLOR_HELP] = "help",
|
|
|
|
[CLEAN_COLOR_PLAIN] = "plain",
|
|
|
|
[CLEAN_COLOR_PROMPT] = "prompt",
|
|
|
|
[CLEAN_COLOR_RESET] = "reset",
|
|
|
|
};
|
|
|
|
|
2017-07-14 15:38:09 +00:00
|
|
|
static int clean_use_color = -1;
|
|
|
|
static char clean_colors[][COLOR_MAXLEN] = {
|
|
|
|
[CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
|
|
|
|
[CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,
|
|
|
|
[CLEAN_COLOR_HELP] = GIT_COLOR_BOLD_RED,
|
|
|
|
[CLEAN_COLOR_PLAIN] = GIT_COLOR_NORMAL,
|
|
|
|
[CLEAN_COLOR_PROMPT] = GIT_COLOR_BOLD_BLUE,
|
|
|
|
[CLEAN_COLOR_RESET] = GIT_COLOR_RESET,
|
|
|
|
};
|
|
|
|
|
2013-06-25 15:53:51 +00:00
|
|
|
#define MENU_OPTS_SINGLETON 01
|
|
|
|
#define MENU_OPTS_IMMEDIATE 02
|
|
|
|
#define MENU_OPTS_LIST_ONLY 04
|
|
|
|
|
|
|
|
struct menu_opts {
|
|
|
|
const char *header;
|
|
|
|
const char *prompt;
|
|
|
|
int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MENU_RETURN_NO_LOOP 10
|
|
|
|
|
|
|
|
struct menu_item {
|
|
|
|
char hotkey;
|
|
|
|
const char *title;
|
|
|
|
int selected;
|
2014-08-16 11:16:56 +00:00
|
|
|
int (*fn)(void);
|
2013-06-25 15:53:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum menu_stuff_type {
|
|
|
|
MENU_STUFF_TYPE_STRING_LIST = 1,
|
|
|
|
MENU_STUFF_TYPE_MENU_ITEM
|
|
|
|
};
|
|
|
|
|
|
|
|
struct menu_stuff {
|
|
|
|
enum menu_stuff_type type;
|
|
|
|
int nr;
|
|
|
|
void *stuff;
|
|
|
|
};
|
|
|
|
|
2018-05-26 13:55:24 +00:00
|
|
|
define_list_config_array(color_interactive_slots);
|
|
|
|
|
2008-05-14 17:46:53 +00:00
|
|
|
static int git_clean_config(const char *var, const char *value, void *cb)
|
2007-11-12 01:48:47 +00:00
|
|
|
{
|
2014-10-04 18:54:50 +00:00
|
|
|
const char *slot_name;
|
|
|
|
|
2013-11-30 20:55:40 +00:00
|
|
|
if (starts_with(var, "column."))
|
2013-06-25 15:53:49 +00:00
|
|
|
return git_column_config(var, value, "clean", &colopts);
|
|
|
|
|
2013-06-25 15:53:50 +00:00
|
|
|
/* honors the color.interactive* config variables which also
|
|
|
|
applied in git-add--interactive and git-stash */
|
|
|
|
if (!strcmp(var, "color.interactive")) {
|
|
|
|
clean_use_color = git_config_colorbool(var, value);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-04 18:54:50 +00:00
|
|
|
if (skip_prefix(var, "color.interactive.", &slot_name)) {
|
2018-05-26 13:55:21 +00:00
|
|
|
int slot = LOOKUP_CONFIG(color_interactive_slots, slot_name);
|
2013-06-25 15:53:50 +00:00
|
|
|
if (slot < 0)
|
|
|
|
return 0;
|
|
|
|
if (!value)
|
|
|
|
return config_error_nonbool(var);
|
2014-10-07 19:33:09 +00:00
|
|
|
return color_parse(value, clean_colors[slot]);
|
2013-06-25 15:53:50 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:49 +00:00
|
|
|
if (!strcmp(var, "clean.requireforce")) {
|
2007-11-12 01:48:47 +00:00
|
|
|
force = !git_config_bool(var, value);
|
2013-06-25 15:53:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-06-25 15:53:50 +00:00
|
|
|
|
2017-10-13 17:24:31 +00:00
|
|
|
/* inspect the color.ui config variable and others */
|
|
|
|
return git_color_default_config(var, value, cb);
|
2013-06-25 15:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *clean_get_color(enum color_clean ix)
|
|
|
|
{
|
|
|
|
if (want_color(clean_use_color))
|
|
|
|
return clean_colors[ix];
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clean_print_color(enum color_clean ix)
|
|
|
|
{
|
|
|
|
printf("%s", clean_get_color(ix));
|
2007-11-12 01:48:47 +00:00
|
|
|
}
|
|
|
|
|
2010-07-20 19:35:56 +00:00
|
|
|
static int exclude_cb(const struct option *opt, const char *arg, int unset)
|
|
|
|
{
|
|
|
|
struct string_list *exclude_list = opt->value;
|
assert NOARG/NONEG behavior of parse-options callbacks
When we define a parse-options callback, the flags we put in the option
struct must match what the callback expects. For example, a callback
which does not handle the "unset" parameter should only be used with
PARSE_OPT_NONEG. But since the callback and the option struct are not
defined next to each other, it's easy to get this wrong (as earlier
patches in this series show).
Fortunately, the compiler can help us here: compiling with
-Wunused-parameters can show us which callbacks ignore their "unset"
parameters (and likewise, ones that ignore "arg" expect to be triggered
with PARSE_OPT_NOARG).
But after we've inspected a callback and determined that all of its
callers use the right flags, what do we do next? We'd like to silence
the compiler warning, but do so in a way that will catch any wrong calls
in the future.
We can do that by actually checking those variables and asserting that
they match our expectations. Because this is such a common pattern,
we'll introduce some helper macros. The resulting messages aren't
as descriptive as we could make them, but the file/line information from
BUG() is enough to identify the problem (and anyway, the point is that
these should never be seen).
Each of the annotated callbacks in this patch triggers
-Wunused-parameters, and was manually inspected to make sure all callers
use the correct options (so none of these BUGs should be triggerable).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-11-05 06:45:42 +00:00
|
|
|
BUG_ON_OPT_NEG(unset);
|
2010-07-20 19:35:56 +00:00
|
|
|
string_list_append(exclude_list, arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
|
|
|
|
int dry_run, int quiet, int *dir_gone)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct strbuf quoted = STRBUF_INIT;
|
2021-12-09 05:08:30 +00:00
|
|
|
struct strbuf realpath = STRBUF_INIT;
|
|
|
|
struct strbuf real_ocwd = STRBUF_INIT;
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
struct dirent *e;
|
2014-01-29 13:36:15 +00:00
|
|
|
int res = 0, ret = 0, gone = 1, original_len = path->len, len;
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
struct string_list dels = STRING_LIST_INIT_DUP;
|
|
|
|
|
|
|
|
*dir_gone = 1;
|
|
|
|
|
2019-09-17 16:35:03 +00:00
|
|
|
if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
|
|
|
|
is_nonbare_repository_dir(path)) {
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
if (!quiet) {
|
2020-09-10 17:01:54 +00:00
|
|
|
quote_path(path->buf, prefix, "ed, 0);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
|
|
|
|
quoted.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
*dir_gone = 0;
|
2017-08-30 17:49:36 +00:00
|
|
|
goto out;
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dir = opendir(path->buf);
|
|
|
|
if (!dir) {
|
|
|
|
/* an empty dir could be removed even if it is unreadble */
|
|
|
|
res = dry_run ? 0 : rmdir(path->buf);
|
|
|
|
if (res) {
|
2017-02-14 09:54:49 +00:00
|
|
|
int saved_errno = errno;
|
2020-09-10 17:01:54 +00:00
|
|
|
quote_path(path->buf, prefix, "ed, 0);
|
2017-02-14 09:54:49 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
warning_errno(_(msg_warn_remove_failed), quoted.buf);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
*dir_gone = 0;
|
|
|
|
}
|
2017-08-30 17:49:36 +00:00
|
|
|
ret = res;
|
|
|
|
goto out;
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
}
|
|
|
|
|
use strbuf_complete to conditionally append slash
When working with paths in strbufs, we frequently want to
ensure that a directory contains a trailing slash before
appending to it. We can shorten this code (and make the
intent more obvious) by calling strbuf_complete.
Most of these cases are trivially identical conversions, but
there are two things to note:
- in a few cases we did not check that the strbuf is
non-empty (which would lead to an out-of-bounds memory
access). These were generally not triggerable in
practice, either from earlier assertions, or typically
because we would have just fed the strbuf to opendir(),
which would choke on an empty path.
- in a few cases we indexed the buffer with "original_len"
or similar, rather than the current sb->len, and it is
not immediately obvious from the diff that they are the
same. In all of these cases, I manually verified that
the strbuf does not change between the assignment and
the strbuf_complete call.
This does not convert cases which look like:
if (sb->len && !is_dir_sep(sb->buf[sb->len - 1]))
strbuf_addch(sb, '/');
as those are obviously semantically different. Some of these
cases arguably should be doing that, but that is out of
scope for this change, which aims purely for cleanup with no
behavior change (and at least it will make such sites easier
to find and examine in the future, as we can grep for
strbuf_complete).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-09-24 21:08:35 +00:00
|
|
|
strbuf_complete(path, '/');
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
|
|
|
|
len = path->len;
|
2021-05-12 17:28:22 +00:00
|
|
|
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
strbuf_setlen(path, len);
|
|
|
|
strbuf_addstr(path, e->d_name);
|
|
|
|
if (lstat(path->buf, &st))
|
2019-07-18 09:30:33 +00:00
|
|
|
warning_errno(_(msg_warn_lstat_failed), path->buf);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
else if (S_ISDIR(st.st_mode)) {
|
|
|
|
if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone))
|
|
|
|
ret = 1;
|
|
|
|
if (gone) {
|
2020-09-10 17:01:54 +00:00
|
|
|
quote_path(path->buf, prefix, "ed, 0);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
string_list_append(&dels, quoted.buf);
|
|
|
|
} else
|
|
|
|
*dir_gone = 0;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
res = dry_run ? 0 : unlink(path->buf);
|
|
|
|
if (!res) {
|
2020-09-10 17:01:54 +00:00
|
|
|
quote_path(path->buf, prefix, "ed, 0);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
string_list_append(&dels, quoted.buf);
|
|
|
|
} else {
|
2017-02-14 09:54:49 +00:00
|
|
|
int saved_errno = errno;
|
2020-09-10 17:01:54 +00:00
|
|
|
quote_path(path->buf, prefix, "ed, 0);
|
2017-02-14 09:54:49 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
warning_errno(_(msg_warn_remove_failed), quoted.buf);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
*dir_gone = 0;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* path too long, stat fails, or non-directory still exists */
|
|
|
|
*dir_gone = 0;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
strbuf_setlen(path, original_len);
|
|
|
|
|
|
|
|
if (*dir_gone) {
|
2021-12-09 05:08:30 +00:00
|
|
|
/*
|
|
|
|
* Normalize path components in path->buf, e.g. change '\' to
|
|
|
|
* '/' on Windows.
|
|
|
|
*/
|
|
|
|
strbuf_realpath(&realpath, path->buf, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* path and realpath are absolute; for comparison, we would
|
|
|
|
* like to transform startup_info->original_cwd to an absolute
|
|
|
|
* path too.
|
|
|
|
*/
|
|
|
|
if (startup_info->original_cwd)
|
|
|
|
strbuf_realpath(&real_ocwd,
|
|
|
|
startup_info->original_cwd, 1);
|
|
|
|
|
|
|
|
if (!strbuf_cmp(&realpath, &real_ocwd)) {
|
|
|
|
printf("%s", dry_run ? _(msg_would_skip_cwd) : _(msg_skip_cwd));
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
*dir_gone = 0;
|
2021-12-09 05:08:30 +00:00
|
|
|
} else {
|
|
|
|
res = dry_run ? 0 : rmdir(path->buf);
|
|
|
|
if (!res)
|
|
|
|
*dir_gone = 1;
|
|
|
|
else {
|
|
|
|
int saved_errno = errno;
|
|
|
|
quote_path(path->buf, prefix, "ed, 0);
|
|
|
|
errno = saved_errno;
|
|
|
|
warning_errno(_(msg_warn_remove_failed), quoted.buf);
|
|
|
|
*dir_gone = 0;
|
|
|
|
ret = 1;
|
|
|
|
}
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*dir_gone && !quiet) {
|
2014-01-29 13:36:15 +00:00
|
|
|
int i;
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
for (i = 0; i < dels.nr; i++)
|
|
|
|
printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string);
|
|
|
|
}
|
2017-08-30 17:49:36 +00:00
|
|
|
out:
|
2021-12-09 05:08:30 +00:00
|
|
|
strbuf_release(&realpath);
|
|
|
|
strbuf_release(&real_ocwd);
|
2017-08-30 17:49:36 +00:00
|
|
|
strbuf_release("ed);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
string_list_clear(&dels, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:49 +00:00
|
|
|
static void pretty_print_dels(void)
|
2013-06-25 15:53:48 +00:00
|
|
|
{
|
2013-06-25 15:53:49 +00:00
|
|
|
struct string_list list = STRING_LIST_INIT_DUP;
|
2013-06-25 15:53:48 +00:00
|
|
|
struct string_list_item *item;
|
2013-06-25 15:53:49 +00:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2013-06-25 15:53:48 +00:00
|
|
|
const char *qname;
|
2013-06-25 15:53:49 +00:00
|
|
|
struct column_options copts;
|
|
|
|
|
|
|
|
for_each_string_list_item(item, &del_list) {
|
2020-09-10 17:01:54 +00:00
|
|
|
qname = quote_path(item->string, NULL, &buf, 0);
|
2013-06-25 15:53:49 +00:00
|
|
|
string_list_append(&list, qname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* always enable column display, we only consult column.*
|
|
|
|
* about layout strategy and stuff
|
|
|
|
*/
|
|
|
|
colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
|
|
|
|
memset(&copts, 0, sizeof(copts));
|
|
|
|
copts.indent = " ";
|
|
|
|
copts.padding = 2;
|
|
|
|
print_columns(&list, colopts, &copts);
|
|
|
|
strbuf_release(&buf);
|
|
|
|
string_list_clear(&list, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:51 +00:00
|
|
|
static void pretty_print_menus(struct string_list *menu_list)
|
|
|
|
{
|
|
|
|
unsigned int local_colopts = 0;
|
|
|
|
struct column_options copts;
|
|
|
|
|
|
|
|
local_colopts = COL_ENABLED | COL_ROW;
|
|
|
|
memset(&copts, 0, sizeof(copts));
|
|
|
|
copts.indent = " ";
|
|
|
|
copts.padding = 2;
|
|
|
|
print_columns(menu_list, local_colopts, &copts);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void prompt_help_cmd(int singleton)
|
|
|
|
{
|
|
|
|
clean_print_color(CLEAN_COLOR_HELP);
|
2016-12-14 12:54:28 +00:00
|
|
|
printf(singleton ?
|
2013-06-25 15:53:51 +00:00
|
|
|
_("Prompt help:\n"
|
|
|
|
"1 - select a numbered item\n"
|
|
|
|
"foo - select item based on unique prefix\n"
|
2016-12-14 12:54:28 +00:00
|
|
|
" - (empty) select nothing\n") :
|
2013-06-25 15:53:51 +00:00
|
|
|
_("Prompt help:\n"
|
|
|
|
"1 - select a single item\n"
|
|
|
|
"3-5 - select a range of items\n"
|
|
|
|
"2-3,6-9 - select multiple ranges\n"
|
|
|
|
"foo - select item based on unique prefix\n"
|
|
|
|
"-... - unselect specified items\n"
|
|
|
|
"* - choose all items\n"
|
2016-12-14 12:54:28 +00:00
|
|
|
" - (empty) finish selecting\n"));
|
2013-06-25 15:53:51 +00:00
|
|
|
clean_print_color(CLEAN_COLOR_RESET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* display menu stuff with number prefix and hotkey highlight
|
|
|
|
*/
|
|
|
|
static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen)
|
|
|
|
{
|
|
|
|
struct string_list menu_list = STRING_LIST_INIT_DUP;
|
|
|
|
struct strbuf menu = STRBUF_INIT;
|
|
|
|
struct menu_item *menu_item;
|
|
|
|
struct string_list_item *string_list_item;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (stuff->type) {
|
|
|
|
default:
|
2014-12-19 08:37:47 +00:00
|
|
|
die("Bad type of menu_stuff when print menu");
|
2013-06-25 15:53:51 +00:00
|
|
|
case MENU_STUFF_TYPE_MENU_ITEM:
|
|
|
|
menu_item = (struct menu_item *)stuff->stuff;
|
|
|
|
for (i = 0; i < stuff->nr; i++, menu_item++) {
|
|
|
|
const char *p;
|
|
|
|
int highlighted = 0;
|
|
|
|
|
|
|
|
p = menu_item->title;
|
|
|
|
if ((*chosen)[i] < 0)
|
|
|
|
(*chosen)[i] = menu_item->selected ? 1 : 0;
|
|
|
|
strbuf_addf(&menu, "%s%2d: ", (*chosen)[i] ? "*" : " ", i+1);
|
|
|
|
for (; *p; p++) {
|
|
|
|
if (!highlighted && *p == menu_item->hotkey) {
|
|
|
|
strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_PROMPT));
|
|
|
|
strbuf_addch(&menu, *p);
|
|
|
|
strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_RESET));
|
|
|
|
highlighted = 1;
|
|
|
|
} else {
|
|
|
|
strbuf_addch(&menu, *p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
string_list_append(&menu_list, menu.buf);
|
|
|
|
strbuf_reset(&menu);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MENU_STUFF_TYPE_STRING_LIST:
|
|
|
|
i = 0;
|
|
|
|
for_each_string_list_item(string_list_item, (struct string_list *)stuff->stuff) {
|
|
|
|
if ((*chosen)[i] < 0)
|
|
|
|
(*chosen)[i] = 0;
|
|
|
|
strbuf_addf(&menu, "%s%2d: %s",
|
|
|
|
(*chosen)[i] ? "*" : " ", i+1, string_list_item->string);
|
|
|
|
string_list_append(&menu_list, menu.buf);
|
|
|
|
strbuf_reset(&menu);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pretty_print_menus(&menu_list);
|
|
|
|
|
|
|
|
strbuf_release(&menu);
|
|
|
|
string_list_clear(&menu_list, 0);
|
|
|
|
}
|
|
|
|
|
2013-07-24 02:22:04 +00:00
|
|
|
static int find_unique(const char *choice, struct menu_stuff *menu_stuff)
|
|
|
|
{
|
|
|
|
struct menu_item *menu_item;
|
|
|
|
struct string_list_item *string_list_item;
|
|
|
|
int i, len, found = 0;
|
|
|
|
|
|
|
|
len = strlen(choice);
|
|
|
|
switch (menu_stuff->type) {
|
|
|
|
default:
|
|
|
|
die("Bad type of menu_stuff when parse choice");
|
|
|
|
case MENU_STUFF_TYPE_MENU_ITEM:
|
|
|
|
|
|
|
|
menu_item = (struct menu_item *)menu_stuff->stuff;
|
|
|
|
for (i = 0; i < menu_stuff->nr; i++, menu_item++) {
|
|
|
|
if (len == 1 && *choice == menu_item->hotkey) {
|
|
|
|
found = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!strncasecmp(choice, menu_item->title, len)) {
|
|
|
|
if (found) {
|
|
|
|
if (len == 1) {
|
|
|
|
/* continue for hotkey matching */
|
|
|
|
found = -1;
|
|
|
|
} else {
|
|
|
|
found = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
found = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MENU_STUFF_TYPE_STRING_LIST:
|
|
|
|
string_list_item = ((struct string_list *)menu_stuff->stuff)->items;
|
|
|
|
for (i = 0; i < menu_stuff->nr; i++, string_list_item++) {
|
|
|
|
if (!strncasecmp(choice, string_list_item->string, len)) {
|
|
|
|
if (found) {
|
|
|
|
found = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
found = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:51 +00:00
|
|
|
/*
|
|
|
|
* Parse user input, and return choice(s) for menu (menu_stuff).
|
|
|
|
*
|
|
|
|
* Input
|
|
|
|
* (for single choice)
|
|
|
|
* 1 - select a numbered item
|
|
|
|
* foo - select item based on menu title
|
|
|
|
* - (empty) select nothing
|
|
|
|
*
|
|
|
|
* (for multiple choice)
|
|
|
|
* 1 - select a single item
|
|
|
|
* 3-5 - select a range of items
|
|
|
|
* 2-3,6-9 - select multiple ranges
|
|
|
|
* foo - select item based on menu title
|
|
|
|
* -... - unselect specified items
|
|
|
|
* * - choose all items
|
|
|
|
* - (empty) finish selecting
|
|
|
|
*
|
|
|
|
* The parse result will be saved in array **chosen, and
|
|
|
|
* return number of total selections.
|
|
|
|
*/
|
|
|
|
static int parse_choice(struct menu_stuff *menu_stuff,
|
|
|
|
int is_single,
|
|
|
|
struct strbuf input,
|
|
|
|
int **chosen)
|
|
|
|
{
|
|
|
|
struct strbuf **choice_list, **ptr;
|
|
|
|
int nr = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (is_single) {
|
|
|
|
choice_list = strbuf_split_max(&input, '\n', 0);
|
|
|
|
} else {
|
|
|
|
char *p = input.buf;
|
|
|
|
do {
|
|
|
|
if (*p == ',')
|
|
|
|
*p = ' ';
|
|
|
|
} while (*p++);
|
|
|
|
choice_list = strbuf_split_max(&input, ' ', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ptr = choice_list; *ptr; ptr++) {
|
|
|
|
char *p;
|
|
|
|
int choose = 1;
|
|
|
|
int bottom = 0, top = 0;
|
|
|
|
int is_range, is_number;
|
|
|
|
|
|
|
|
strbuf_trim(*ptr);
|
|
|
|
if (!(*ptr)->len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Input that begins with '-'; unchoose */
|
|
|
|
if (*(*ptr)->buf == '-') {
|
|
|
|
choose = 0;
|
|
|
|
strbuf_remove((*ptr), 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
is_range = 0;
|
|
|
|
is_number = 1;
|
|
|
|
for (p = (*ptr)->buf; *p; p++) {
|
|
|
|
if ('-' == *p) {
|
|
|
|
if (!is_range) {
|
|
|
|
is_range = 1;
|
|
|
|
is_number = 0;
|
|
|
|
} else {
|
|
|
|
is_number = 0;
|
|
|
|
is_range = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (!isdigit(*p)) {
|
|
|
|
is_number = 0;
|
|
|
|
is_range = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_number) {
|
|
|
|
bottom = atoi((*ptr)->buf);
|
|
|
|
top = bottom;
|
|
|
|
} else if (is_range) {
|
|
|
|
bottom = atoi((*ptr)->buf);
|
|
|
|
/* a range can be specified like 5-7 or 5- */
|
|
|
|
if (!*(strchr((*ptr)->buf, '-') + 1))
|
|
|
|
top = menu_stuff->nr;
|
|
|
|
else
|
|
|
|
top = atoi(strchr((*ptr)->buf, '-') + 1);
|
|
|
|
} else if (!strcmp((*ptr)->buf, "*")) {
|
|
|
|
bottom = 1;
|
|
|
|
top = menu_stuff->nr;
|
|
|
|
} else {
|
2013-07-24 02:22:04 +00:00
|
|
|
bottom = find_unique((*ptr)->buf, menu_stuff);
|
|
|
|
top = bottom;
|
2013-06-25 15:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top ||
|
|
|
|
(is_single && bottom != top)) {
|
|
|
|
clean_print_color(CLEAN_COLOR_ERROR);
|
2016-12-14 12:54:28 +00:00
|
|
|
printf(_("Huh (%s)?\n"), (*ptr)->buf);
|
2013-06-25 15:53:51 +00:00
|
|
|
clean_print_color(CLEAN_COLOR_RESET);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = bottom; i <= top; i++)
|
|
|
|
(*chosen)[i-1] = choose;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_list_free(choice_list);
|
|
|
|
|
|
|
|
for (i = 0; i < menu_stuff->nr; i++)
|
|
|
|
nr += (*chosen)[i];
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implement a git-add-interactive compatible UI, which is borrowed
|
2023-02-06 22:58:58 +00:00
|
|
|
* from add-interactive.c.
|
2013-06-25 15:53:51 +00:00
|
|
|
*
|
|
|
|
* Return value:
|
|
|
|
*
|
|
|
|
* - Return an array of integers
|
|
|
|
* - , and it is up to you to free the allocated memory.
|
|
|
|
* - The array ends with EOF.
|
|
|
|
* - If user pressed CTRL-D (i.e. EOF), no selection returned.
|
|
|
|
*/
|
|
|
|
static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
|
|
|
|
{
|
|
|
|
struct strbuf choice = STRBUF_INIT;
|
|
|
|
int *chosen, *result;
|
|
|
|
int nr = 0;
|
|
|
|
int eof = 0;
|
|
|
|
int i;
|
|
|
|
|
2016-02-22 22:44:25 +00:00
|
|
|
ALLOC_ARRAY(chosen, stuff->nr);
|
2013-06-25 15:53:51 +00:00
|
|
|
/* set chosen as uninitialized */
|
|
|
|
for (i = 0; i < stuff->nr; i++)
|
|
|
|
chosen[i] = -1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (opts->header) {
|
|
|
|
printf_ln("%s%s%s",
|
|
|
|
clean_get_color(CLEAN_COLOR_HEADER),
|
|
|
|
_(opts->header),
|
|
|
|
clean_get_color(CLEAN_COLOR_RESET));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* chosen will be initialized by print_highlight_menu_stuff */
|
|
|
|
print_highlight_menu_stuff(stuff, &chosen);
|
|
|
|
|
|
|
|
if (opts->flags & MENU_OPTS_LIST_ONLY)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (opts->prompt) {
|
|
|
|
printf("%s%s%s%s",
|
|
|
|
clean_get_color(CLEAN_COLOR_PROMPT),
|
|
|
|
_(opts->prompt),
|
|
|
|
opts->flags & MENU_OPTS_SINGLETON ? "> " : ">> ",
|
|
|
|
clean_get_color(CLEAN_COLOR_RESET));
|
|
|
|
}
|
|
|
|
|
2020-04-10 11:27:50 +00:00
|
|
|
if (git_read_line_interactively(&choice) == EOF) {
|
2013-06-25 15:53:51 +00:00
|
|
|
eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* help for prompt */
|
|
|
|
if (!strcmp(choice.buf, "?")) {
|
|
|
|
prompt_help_cmd(opts->flags & MENU_OPTS_SINGLETON);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for a multiple-choice menu, press ENTER (empty) will return back */
|
|
|
|
if (!(opts->flags & MENU_OPTS_SINGLETON) && !choice.len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
nr = parse_choice(stuff,
|
|
|
|
opts->flags & MENU_OPTS_SINGLETON,
|
|
|
|
choice,
|
|
|
|
&chosen);
|
|
|
|
|
|
|
|
if (opts->flags & MENU_OPTS_SINGLETON) {
|
|
|
|
if (nr)
|
|
|
|
break;
|
|
|
|
} else if (opts->flags & MENU_OPTS_IMMEDIATE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eof) {
|
|
|
|
result = xmalloc(sizeof(int));
|
|
|
|
*result = EOF;
|
|
|
|
} else {
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* recalculate nr, if return back from menu directly with
|
|
|
|
* default selections.
|
|
|
|
*/
|
|
|
|
if (!nr) {
|
|
|
|
for (i = 0; i < stuff->nr; i++)
|
|
|
|
nr += chosen[i];
|
|
|
|
}
|
|
|
|
|
2021-03-13 16:17:22 +00:00
|
|
|
CALLOC_ARRAY(result, st_add(nr, 1));
|
2013-06-25 15:53:51 +00:00
|
|
|
for (i = 0; i < stuff->nr && j < nr; i++) {
|
|
|
|
if (chosen[i])
|
|
|
|
result[j++] = i;
|
|
|
|
}
|
|
|
|
result[j] = EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(chosen);
|
|
|
|
strbuf_release(&choice);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clean_cmd(void)
|
2013-06-25 15:53:49 +00:00
|
|
|
{
|
2013-06-25 15:53:51 +00:00
|
|
|
return MENU_RETURN_NO_LOOP;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:52 +00:00
|
|
|
static int filter_by_patterns_cmd(void)
|
|
|
|
{
|
2021-07-01 10:51:27 +00:00
|
|
|
struct dir_struct dir = DIR_INIT;
|
2013-06-25 15:53:52 +00:00
|
|
|
struct strbuf confirm = STRBUF_INIT;
|
|
|
|
struct strbuf **ignore_list;
|
|
|
|
struct string_list_item *item;
|
2019-09-03 18:04:56 +00:00
|
|
|
struct pattern_list *pl;
|
2013-06-25 15:53:52 +00:00
|
|
|
int changed = -1, i;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!del_list.nr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
pretty_print_dels();
|
|
|
|
|
|
|
|
clean_print_color(CLEAN_COLOR_PROMPT);
|
|
|
|
printf(_("Input ignore patterns>> "));
|
|
|
|
clean_print_color(CLEAN_COLOR_RESET);
|
2020-04-10 11:27:50 +00:00
|
|
|
if (git_read_line_interactively(&confirm) == EOF)
|
2013-06-25 15:53:52 +00:00
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
/* quit filter_by_pattern mode if press ENTER or Ctrl-D */
|
|
|
|
if (!confirm.len)
|
|
|
|
break;
|
|
|
|
|
2019-09-03 18:04:57 +00:00
|
|
|
pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude");
|
2013-06-25 15:53:52 +00:00
|
|
|
ignore_list = strbuf_split_max(&confirm, ' ', 0);
|
|
|
|
|
|
|
|
for (i = 0; ignore_list[i]; i++) {
|
|
|
|
strbuf_trim(ignore_list[i]);
|
|
|
|
if (!ignore_list[i]->len)
|
|
|
|
continue;
|
|
|
|
|
2019-09-03 18:04:57 +00:00
|
|
|
add_pattern(ignore_list[i]->buf, "", 0, pl, -(i+1));
|
2013-06-25 15:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
changed = 0;
|
|
|
|
for_each_string_list_item(item, &del_list) {
|
|
|
|
int dtype = DT_UNKNOWN;
|
|
|
|
|
2017-05-05 19:53:30 +00:00
|
|
|
if (is_excluded(&dir, &the_index, item->string, &dtype)) {
|
2013-06-25 15:53:52 +00:00
|
|
|
*item->string = '\0';
|
|
|
|
changed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
string_list_remove_empty_items(&del_list, 0);
|
|
|
|
} else {
|
|
|
|
clean_print_color(CLEAN_COLOR_ERROR);
|
|
|
|
printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm.buf);
|
|
|
|
clean_print_color(CLEAN_COLOR_RESET);
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_list_free(ignore_list);
|
dir: fix problematic API to avoid memory leaks
The dir structure seemed to have a number of leaks and problems around
it. First I noticed that parent_hashmap and recursive_hashmap were
being leaked (though Peff noticed and submitted fixes before me). Then
I noticed in the previous commit that clear_directory() was only taking
responsibility for a subset of fields within dir_struct, despite the
fact that entries[] and ignored[] we allocated internally to dir.c.
That, of course, resulted in many callers either leaking or haphazardly
trying to free these arrays and their contents.
Digging further, I found that despite the pretty clear documentation
near the top of dir.h that folks were supposed to call clear_directory()
when the user no longer needed the dir_struct, there were four callers
that didn't bother doing that at all. However, two of them clearly
thought about leaks since they had an UNLEAK(dir) directive, which to me
suggests that the method to free the data was too unclear. I suspect
the non-obviousness of the API and its holes led folks to avoid it,
which then snowballed into further problems with the entries[],
ignored[], parent_hashmap, and recursive_hashmap problems.
Rename clear_directory() to dir_clear() to be more in line with other
data structures in git, and introduce a dir_init() to handle the
suggested memsetting of dir_struct to all zeroes. I hope that a name
like "dir_clear()" is more clear, and that the presence of dir_init()
will provide a hint to those looking at the code that they need to look
for either a dir_clear() or a dir_free() and lead them to find
dir_clear().
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-08-18 22:58:26 +00:00
|
|
|
dir_clear(&dir);
|
2013-06-25 15:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_release(&confirm);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:53 +00:00
|
|
|
static int select_by_numbers_cmd(void)
|
|
|
|
{
|
|
|
|
struct menu_opts menu_opts;
|
|
|
|
struct menu_stuff menu_stuff;
|
|
|
|
struct string_list_item *items;
|
|
|
|
int *chosen;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
menu_opts.header = NULL;
|
|
|
|
menu_opts.prompt = N_("Select items to delete");
|
|
|
|
menu_opts.flags = 0;
|
|
|
|
|
|
|
|
menu_stuff.type = MENU_STUFF_TYPE_STRING_LIST;
|
|
|
|
menu_stuff.stuff = &del_list;
|
|
|
|
menu_stuff.nr = del_list.nr;
|
|
|
|
|
|
|
|
chosen = list_and_choose(&menu_opts, &menu_stuff);
|
|
|
|
items = del_list.items;
|
|
|
|
for (i = 0, j = 0; i < del_list.nr; i++) {
|
|
|
|
if (i < chosen[j]) {
|
|
|
|
*(items[i].string) = '\0';
|
|
|
|
} else if (i == chosen[j]) {
|
|
|
|
/* delete selected item */
|
|
|
|
j++;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* end of chosen (chosen[j] == EOF), won't delete */
|
|
|
|
*(items[i].string) = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string_list_remove_empty_items(&del_list, 0);
|
|
|
|
|
|
|
|
free(chosen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:54 +00:00
|
|
|
static int ask_each_cmd(void)
|
|
|
|
{
|
|
|
|
struct strbuf confirm = STRBUF_INIT;
|
|
|
|
struct strbuf buf = STRBUF_INIT;
|
|
|
|
struct string_list_item *item;
|
|
|
|
const char *qname;
|
|
|
|
int changed = 0, eof = 0;
|
|
|
|
|
|
|
|
for_each_string_list_item(item, &del_list) {
|
|
|
|
/* Ctrl-D should stop removing files */
|
|
|
|
if (!eof) {
|
2020-09-10 17:01:54 +00:00
|
|
|
qname = quote_path(item->string, NULL, &buf, 0);
|
2015-03-01 11:58:25 +00:00
|
|
|
/* TRANSLATORS: Make sure to keep [y/N] as is */
|
|
|
|
printf(_("Remove %s [y/N]? "), qname);
|
2020-04-10 11:27:50 +00:00
|
|
|
if (git_read_line_interactively(&confirm) == EOF) {
|
2013-06-25 15:53:54 +00:00
|
|
|
putchar('\n');
|
|
|
|
eof = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!confirm.len || strncasecmp(confirm.buf, "yes", confirm.len)) {
|
|
|
|
*item->string = '\0';
|
|
|
|
changed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
string_list_remove_empty_items(&del_list, 0);
|
|
|
|
|
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&confirm);
|
|
|
|
return MENU_RETURN_NO_LOOP;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:51 +00:00
|
|
|
static int quit_cmd(void)
|
|
|
|
{
|
|
|
|
string_list_clear(&del_list, 0);
|
2016-12-14 12:54:28 +00:00
|
|
|
printf(_("Bye.\n"));
|
2013-06-25 15:53:51 +00:00
|
|
|
return MENU_RETURN_NO_LOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int help_cmd(void)
|
|
|
|
{
|
|
|
|
clean_print_color(CLEAN_COLOR_HELP);
|
|
|
|
printf_ln(_(
|
|
|
|
"clean - start cleaning\n"
|
2013-06-25 15:53:52 +00:00
|
|
|
"filter by pattern - exclude items from deletion\n"
|
2013-06-25 15:53:53 +00:00
|
|
|
"select by numbers - select items to be deleted by numbers\n"
|
2013-06-25 15:53:54 +00:00
|
|
|
"ask each - confirm each deletion (like \"rm -i\")\n"
|
2013-06-25 15:53:51 +00:00
|
|
|
"quit - stop cleaning\n"
|
|
|
|
"help - this screen\n"
|
|
|
|
"? - help for prompt selection"
|
|
|
|
));
|
|
|
|
clean_print_color(CLEAN_COLOR_RESET);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-25 15:53:48 +00:00
|
|
|
|
2013-06-25 15:53:51 +00:00
|
|
|
static void interactive_main_loop(void)
|
|
|
|
{
|
2013-06-25 15:53:48 +00:00
|
|
|
while (del_list.nr) {
|
2013-06-25 15:53:51 +00:00
|
|
|
struct menu_opts menu_opts;
|
|
|
|
struct menu_stuff menu_stuff;
|
|
|
|
struct menu_item menus[] = {
|
|
|
|
{'c', "clean", 0, clean_cmd},
|
2013-06-25 15:53:52 +00:00
|
|
|
{'f', "filter by pattern", 0, filter_by_patterns_cmd},
|
2013-06-25 15:53:53 +00:00
|
|
|
{'s', "select by numbers", 0, select_by_numbers_cmd},
|
2013-06-25 15:53:54 +00:00
|
|
|
{'a', "ask each", 0, ask_each_cmd},
|
2013-06-25 15:53:51 +00:00
|
|
|
{'q', "quit", 0, quit_cmd},
|
|
|
|
{'h', "help", 0, help_cmd},
|
|
|
|
};
|
|
|
|
int *chosen;
|
|
|
|
|
|
|
|
menu_opts.header = N_("*** Commands ***");
|
|
|
|
menu_opts.prompt = N_("What now");
|
|
|
|
menu_opts.flags = MENU_OPTS_SINGLETON;
|
|
|
|
|
|
|
|
menu_stuff.type = MENU_STUFF_TYPE_MENU_ITEM;
|
|
|
|
menu_stuff.stuff = menus;
|
|
|
|
menu_stuff.nr = sizeof(menus) / sizeof(struct menu_item);
|
|
|
|
|
2013-06-25 15:53:50 +00:00
|
|
|
clean_print_color(CLEAN_COLOR_HEADER);
|
2013-06-25 15:53:49 +00:00
|
|
|
printf_ln(Q_("Would remove the following item:",
|
|
|
|
"Would remove the following items:",
|
|
|
|
del_list.nr));
|
2013-06-25 15:53:50 +00:00
|
|
|
clean_print_color(CLEAN_COLOR_RESET);
|
2013-06-25 15:53:48 +00:00
|
|
|
|
2013-06-25 15:53:49 +00:00
|
|
|
pretty_print_dels();
|
|
|
|
|
2013-06-25 15:53:51 +00:00
|
|
|
chosen = list_and_choose(&menu_opts, &menu_stuff);
|
|
|
|
|
|
|
|
if (*chosen != EOF) {
|
|
|
|
int ret;
|
|
|
|
ret = menus[*chosen].fn();
|
|
|
|
if (ret != MENU_RETURN_NO_LOOP) {
|
2017-06-15 23:15:46 +00:00
|
|
|
FREE_AND_NULL(chosen);
|
2013-06-25 15:53:51 +00:00
|
|
|
if (!del_list.nr) {
|
|
|
|
clean_print_color(CLEAN_COLOR_ERROR);
|
|
|
|
printf_ln(_("No more files to clean, exiting."));
|
|
|
|
clean_print_color(CLEAN_COLOR_RESET);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-25 15:53:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-25 15:53:51 +00:00
|
|
|
} else {
|
|
|
|
quit_cmd();
|
2013-06-25 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 23:15:46 +00:00
|
|
|
FREE_AND_NULL(chosen);
|
2013-06-25 15:53:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-25 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
clean: teach clean -d to preserve ignored paths
There is an implicit assumption that a directory containing only
untracked and ignored paths should itself be considered untracked. This
makes sense in use cases where we're asking if a directory should be
added to the git database, but not when we're asking if a directory can
be safely removed from the working tree; as a result, clean -d would
assume that an "untracked" directory containing ignored paths could be
deleted, even though doing so would also remove the ignored paths.
To get around this, we teach clean -d to collect ignored paths and skip
an untracked directory if it contained an ignored path, instead just
removing the untracked contents thereof. To achieve this, cmd_clean()
has to collect all untracked contents of untracked directories, in
addition to all ignored paths, to determine which untracked dirs must be
skipped (because they contain ignored paths) and which ones should *not*
be skipped.
For this purpose, correct_untracked_entries() is introduced to prune a
given dir_struct of untracked entries containing ignored paths and those
untracked entries encompassed by the untracked entries which are not
pruned away.
A memory leak is also fixed in cmd_clean().
This also fixes the known breakage in t7300, since clean -d now skips
untracked directories containing ignored paths.
Signed-off-by: Samuel Lijin <sxlijin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-05-23 10:09:37 +00:00
|
|
|
static void correct_untracked_entries(struct dir_struct *dir)
|
|
|
|
{
|
|
|
|
int src, dst, ign;
|
|
|
|
|
|
|
|
for (src = dst = ign = 0; src < dir->nr; src++) {
|
|
|
|
/* skip paths in ignored[] that cannot be inside entries[src] */
|
|
|
|
while (ign < dir->ignored_nr &&
|
|
|
|
0 <= cmp_dir_entry(&dir->entries[src], &dir->ignored[ign]))
|
|
|
|
ign++;
|
|
|
|
|
|
|
|
if (ign < dir->ignored_nr &&
|
|
|
|
check_dir_entry_contains(dir->entries[src], dir->ignored[ign])) {
|
|
|
|
/* entries[src] contains an ignored path, so we drop it */
|
|
|
|
free(dir->entries[src]);
|
|
|
|
} else {
|
|
|
|
struct dir_entry *ent = dir->entries[src++];
|
|
|
|
|
|
|
|
/* entries[src] does not contain an ignored path, so we keep it */
|
|
|
|
dir->entries[dst++] = ent;
|
|
|
|
|
|
|
|
/* then discard paths in entries[] contained inside entries[src] */
|
|
|
|
while (src < dir->nr &&
|
|
|
|
check_dir_entry_contains(ent, dir->entries[src]))
|
|
|
|
free(dir->entries[src++]);
|
|
|
|
|
|
|
|
/* compensate for the outer loop's loop control */
|
|
|
|
src--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dir->nr = dst;
|
|
|
|
}
|
|
|
|
|
2007-11-12 01:48:47 +00:00
|
|
|
int cmd_clean(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
int i, res;
|
|
|
|
int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;
|
|
|
|
int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
|
2009-06-30 22:33:45 +00:00
|
|
|
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
|
2013-06-25 15:53:47 +00:00
|
|
|
struct strbuf abs_path = STRBUF_INIT;
|
2021-07-01 10:51:27 +00:00
|
|
|
struct dir_struct dir = DIR_INIT;
|
2013-07-14 08:35:37 +00:00
|
|
|
struct pathspec pathspec;
|
2008-10-09 19:12:12 +00:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2010-09-06 23:32:55 +00:00
|
|
|
struct string_list exclude_list = STRING_LIST_INIT_NODUP;
|
2019-09-03 18:04:56 +00:00
|
|
|
struct pattern_list *pl;
|
2013-06-25 15:53:47 +00:00
|
|
|
struct string_list_item *item;
|
2008-03-07 01:13:17 +00:00
|
|
|
const char *qname;
|
2007-11-12 01:48:47 +00:00
|
|
|
struct option options[] = {
|
2012-08-20 12:32:01 +00:00
|
|
|
OPT__QUIET(&quiet, N_("do not print names of files removed")),
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
OPT__DRY_RUN(&dry_run, N_("dry run")),
|
2018-02-09 11:01:50 +00:00
|
|
|
OPT__FORCE(&force, N_("force"), PARSE_OPT_NOCOMPLETE),
|
2013-06-25 15:53:48 +00:00
|
|
|
OPT_BOOL('i', "interactive", &interactive, N_("interactive cleaning")),
|
2013-08-03 11:51:19 +00:00
|
|
|
OPT_BOOL('d', NULL, &remove_directories,
|
2012-08-20 12:32:01 +00:00
|
|
|
N_("remove whole directories")),
|
Use OPT_CALLBACK and OPT_CALLBACK_F
In the codebase, there are many options which use OPTION_CALLBACK in a
plain ol' struct definition. However, we have the OPT_CALLBACK and
OPT_CALLBACK_F macros which are meant to abstract these plain struct
definitions away. These macros are useful as they semantically signal to
developers that these are just normal callback option with nothing fancy
happening.
Replace plain struct definitions of OPTION_CALLBACK with OPT_CALLBACK or
OPT_CALLBACK_F where applicable. The heavy lifting was done using the
following (disgusting) shell script:
#!/bin/sh
do_replacement () {
tr '\n' '\r' |
sed -e 's/{\s*OPTION_CALLBACK,\s*\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\s*0,\(\s*[^[:space:]}]*\)\s*}/OPT_CALLBACK(\1,\2,\3,\4,\5,\6)/g' |
sed -e 's/{\s*OPTION_CALLBACK,\s*\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\(\s*[^[:space:]}]*\)\s*}/OPT_CALLBACK_F(\1,\2,\3,\4,\5,\6,\7)/g' |
tr '\r' '\n'
}
for f in $(git ls-files \*.c)
do
do_replacement <"$f" >"$f.tmp"
mv "$f.tmp" "$f"
done
The result was manually inspected and then reformatted to match the
style of the surrounding code. Finally, using
`git grep OPTION_CALLBACK \*.c`, leftover results which were not handled
by the script were manually transformed.
Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-04-28 08:36:28 +00:00
|
|
|
OPT_CALLBACK_F('e', "exclude", &exclude_list, N_("pattern"),
|
|
|
|
N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb),
|
2013-08-03 11:51:19 +00:00
|
|
|
OPT_BOOL('x', NULL, &ignored, N_("remove ignored files, too")),
|
|
|
|
OPT_BOOL('X', NULL, &ignored_only,
|
2012-08-20 12:32:01 +00:00
|
|
|
N_("remove only ignored files")),
|
2007-11-12 01:48:47 +00:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2008-05-14 17:46:53 +00:00
|
|
|
git_config(git_clean_config, NULL);
|
2007-11-13 05:13:05 +00:00
|
|
|
if (force < 0)
|
|
|
|
force = 0;
|
|
|
|
else
|
|
|
|
config_set = 1;
|
|
|
|
|
2009-05-23 18:53:12 +00:00
|
|
|
argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
|
|
|
|
0);
|
2007-11-12 01:48:47 +00:00
|
|
|
|
2013-06-25 15:53:48 +00:00
|
|
|
if (!interactive && !dry_run && !force) {
|
2011-02-22 23:42:22 +00:00
|
|
|
if (config_set)
|
2014-03-31 22:11:47 +00:00
|
|
|
die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
|
2011-02-22 23:42:22 +00:00
|
|
|
"refusing to clean"));
|
|
|
|
else
|
2014-03-31 22:11:47 +00:00
|
|
|
die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
|
|
|
|
" refusing to clean"));
|
2011-02-22 23:42:22 +00:00
|
|
|
}
|
2007-11-12 01:48:47 +00:00
|
|
|
|
2009-06-30 22:33:45 +00:00
|
|
|
if (force > 1)
|
|
|
|
rm_flags = 0;
|
clean: avoid removing untracked files in a nested git repository
Users expect files in a nested git repository to be left alone unless
sufficiently forced (with two -f's). Unfortunately, in certain
circumstances, git would delete both tracked (and possibly dirty) files
and untracked files within a nested repository. To explain how this
happens, let's contrast a couple cases. First, take the following
example setup (which assumes we are already within a git repo):
git init nested
cd nested
>tracked
git add tracked
git commit -m init
>untracked
cd ..
In this setup, everything works as expected; running 'git clean -fd'
will result in fill_directory() returning the following paths:
nested/
nested/tracked
nested/untracked
and then correct_untracked_entries() would notice this can be compressed
to
nested/
and then since "nested/" is a directory, we would call
remove_dirs("nested/", ...), which would
check is_nonbare_repository_dir() and then decide to skip it.
However, if someone also creates an ignored file:
>nested/ignored
then running 'git clean -fd' would result in fill_directory() returning
the same paths:
nested/
nested/tracked
nested/untracked
but correct_untracked_entries() will notice that we had ignored entries
under nested/ and thus simplify this list to
nested/tracked
nested/untracked
Since these are not directories, we do not call remove_dirs() which was
the only place that had the is_nonbare_repository_dir() safety check --
resulting in us deleting both the untracked file and the tracked (and
possibly dirty) file.
One possible fix for this issue would be walking the parent directories
of each path and checking if they represent nonbare repositories, but
that would be wasteful. Even if we added caching of some sort, it's
still a waste because we should have been able to check that "nested/"
represented a nonbare repository before even descending into it in the
first place. Add a DIR_SKIP_NESTED_GIT flag to dir_struct.flags and use
it to prevent fill_directory() and friends from descending into nested
git repos.
With this change, we also modify two regression tests added in commit
91479b9c72f1 ("t7300: add tests to document behavior of clean and nested
git", 2015-06-15). That commit, nor its series, nor the six previous
iterations of that series on the mailing list discussed why those tests
coded the expectation they did. In fact, it appears their purpose was
simply to test _existing_ behavior to make sure that the performance
changes didn't change the behavior. However, these two tests directly
contradicted the manpage's claims that two -f's were required to delete
files/directories under a nested git repository. While one could argue
that the user gave an explicit path which matched files/directories that
were within a nested repository, there's a slippery slope that becomes
very difficult for users to understand once you go down that route (e.g.
what if they specified "git clean -f -d '*.c'"?) It would also be hard
to explain what the exact behavior was; avoid such problems by making it
really simple.
Also, clean up some grammar errors describing this functionality in the
git-clean manpage.
Finally, there are still a couple bugs with -ffd not cleaning out enough
(e.g. missing the nested .git) and with -ffdX possibly cleaning out the
wrong files (paying attention to outer .gitignore instead of inner).
This patch does not address these cases at all (and does not change the
behavior relative to those flags), it only fixes the handling when given
a single -f. See
https://public-inbox.org/git/20190905212043.GC32087@szeder.dev/ for more
discussion of the -ffd[X?] bugs.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2019-09-17 16:35:02 +00:00
|
|
|
else
|
|
|
|
dir.flags |= DIR_SKIP_NESTED_GIT;
|
2009-06-30 22:33:45 +00:00
|
|
|
|
2009-02-16 12:20:25 +00:00
|
|
|
dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
|
2007-11-12 01:48:47 +00:00
|
|
|
|
2020-06-11 06:59:32 +00:00
|
|
|
if (ignored && ignored_only)
|
|
|
|
die(_("-x and -X cannot be used together"));
|
|
|
|
if (!ignored)
|
|
|
|
setup_standard_excludes(&dir);
|
|
|
|
if (ignored_only)
|
|
|
|
dir.flags |= DIR_SHOW_IGNORED;
|
|
|
|
|
clean: disambiguate the definition of -d
The -d flag pre-dated git-clean's ability to have paths specified. As
such, the default for git-clean was to only remove untracked files in
the current directory, and -d existed to allow it to recurse into
subdirectories.
The interaction of paths and the -d option appears to not have been
carefully considered, as evidenced by numerous bugs and a dearth of
tests covering such pairings in the testsuite. The definition turns out
to be important, so let's look at some of the various ways one could
interpret the -d option:
A) Without -d, only look in subdirectories which contain tracked
files under them; with -d, also look in subdirectories which
are untracked for files to clean.
B) Without specified paths from the user for us to delete, we need to
have some kind of default, so...without -d, only look in
subdirectories which contain tracked files under them; with -d,
also look in subdirectories which are untracked for files to clean.
The important distinction here is that choice B says that the presence
or absence of '-d' is irrelevant if paths are specified. The logic
behind option B is that if a user explicitly asked us to clean a
specified pathspec, then we should clean anything that matches that
pathspec. Some examples may clarify. Should
git clean -f untracked_dir/file
remove untracked_dir/file or not? It seems crazy not to, but a strict
reading of option A says it shouldn't be removed. How about
git clean -f untracked_dir/file1 tracked_dir/file2
or
git clean -f untracked_dir_1/file1 untracked_dir_2/file2
? Should it remove either or both of these files? Should it require
multiple runs to remove both the files listed? (If this sounds like a
crazy question to even ask, see the commit message of "t7300: Add some
testcases showing failure to clean specified pathspecs" added earlier in
this patch series.) What if -ffd were used instead of -f -- should that
allow these to be removed? Should it take multiple invocations with
-ffd? What if a glob (such as '*tracked*') were used instead of
spelling out the directory names? What if the filenames involved globs,
such as
git clean -f '*.o'
or
git clean -f '*/*.o'
?
The current documentation actually suggests a definition that is
slightly different than choice A, and the implementation prior to this
series provided something radically different than either choices A or
B. (The implementation, though, was clearly just buggy). There may be
other choices as well. However, for almost any given choice of
definition for -d that I can think of, some of the examples above will
appear buggy to the user. The only case that doesn't have negative
surprises is choice B: treat a user-specified path as a request to clean
all untracked files which match that path specification, including
recursing into any untracked directories.
Change the documentation and basic implementation to use this
definition.
There were two regression tests that indirectly depended on the current
implementation, but neither was about subdirectory handling. These two
tests were introduced in commit 5b7570cfb41c ("git-clean: add tests for
relative path", 2008-03-07) which was solely created to add coverage for
the changes in commit fb328947c8e ("git-clean: correct printing relative
path", 2008-03-07). Both tests specified a directory that happened to
have an untracked subdirectory, but both were only checking that the
resulting printout of a file that was removed was shown with a relative
path. Update these tests appropriately.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2019-09-17 16:35:01 +00:00
|
|
|
if (argc) {
|
|
|
|
/*
|
|
|
|
* Remaining args implies pathspecs specified, and we should
|
|
|
|
* recurse within those.
|
|
|
|
*/
|
|
|
|
remove_directories = 1;
|
|
|
|
}
|
|
|
|
|
clean: optimize and document cases where we recurse into subdirectories
Commit 6b1db43109 ("clean: teach clean -d to preserve ignored paths",
2017-05-23) added the following code block (among others) to git-clean:
if (remove_directories)
dir.flags |= DIR_SHOW_IGNORED_TOO | DIR_KEEP_UNTRACKED_CONTENTS;
The reason for these flags is well documented in the commit message, but
isn't obvious just from looking at the code. Add some explanations to
the code to make it clearer.
Further, it appears git-2.26 did not correctly handle this combination
of flags from git-clean. With both these flags and without
DIR_SHOW_IGNORED_TOO_MODE_MATCHING set, git is supposed to recurse into
all untracked AND ignored directories. git-2.26.0 clearly was not doing
that. I don't know the full reasons for that or whether git < 2.27.0
had additional unknown bugs because of that misbehavior, because I don't
feel it's worth digging into. As per the huge changes and craziness
documented in commit 8d92fb2927 ("dir: replace exponential algorithm
with a linear one", 2020-04-01), the old algorithm was a mess and was
thrown out. What I can say is that git-2.27.0 correctly recurses into
untracked AND ignored directories with that combination.
However, in clean's case we don't need to recurse into ignored
directories; that is just a waste of time. Thus, when git-2.27.0
started correctly handling those flags, we got a performance regression
report. Rather than relying on other bugs in fill_directory()'s former
logic to provide the behavior of skipping ignored directories, make use
of the DIR_SHOW_IGNORED_TOO_MODE_MATCHING value specifically added in
commit eec0f7f2b7 ("status: add option to show ignored files
differently", 2017-10-30) for this purpose.
Reported-by: Brian Malehorn <bmalehorn@gmail.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-06-11 06:59:33 +00:00
|
|
|
if (remove_directories && !ignored_only) {
|
|
|
|
/*
|
|
|
|
* We need to know about ignored files too:
|
|
|
|
*
|
|
|
|
* If (ignored), then we will delete ignored files as well.
|
|
|
|
*
|
|
|
|
* If (!ignored), then even though we not are doing
|
|
|
|
* anything with ignored files, we need to know about them
|
|
|
|
* so that we can avoid deleting a directory of untracked
|
|
|
|
* files that also contains an ignored file within it.
|
|
|
|
*
|
|
|
|
* For the (!ignored) case, since we only need to avoid
|
|
|
|
* deleting ignored files, we can set
|
|
|
|
* DIR_SHOW_IGNORED_TOO_MODE_MATCHING in order to avoid
|
|
|
|
* recursing into a directory which is itself ignored.
|
|
|
|
*/
|
|
|
|
dir.flags |= DIR_SHOW_IGNORED_TOO;
|
|
|
|
if (!ignored)
|
|
|
|
dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Let the fill_directory() machinery know that we aren't
|
|
|
|
* just recursing to collect the ignored files; we want all
|
|
|
|
* the untracked ones so that we can delete them. (Note:
|
|
|
|
* we could also set DIR_KEEP_UNTRACKED_CONTENTS when
|
|
|
|
* ignored_only is true, since DIR_KEEP_UNTRACKED_CONTENTS
|
|
|
|
* only has effect in combination with DIR_SHOW_IGNORED_TOO. It makes
|
|
|
|
* the code clearer to exclude it, though.
|
|
|
|
*/
|
|
|
|
dir.flags |= DIR_KEEP_UNTRACKED_CONTENTS;
|
|
|
|
}
|
clean: teach clean -d to preserve ignored paths
There is an implicit assumption that a directory containing only
untracked and ignored paths should itself be considered untracked. This
makes sense in use cases where we're asking if a directory should be
added to the git database, but not when we're asking if a directory can
be safely removed from the working tree; as a result, clean -d would
assume that an "untracked" directory containing ignored paths could be
deleted, even though doing so would also remove the ignored paths.
To get around this, we teach clean -d to collect ignored paths and skip
an untracked directory if it contained an ignored path, instead just
removing the untracked contents thereof. To achieve this, cmd_clean()
has to collect all untracked contents of untracked directories, in
addition to all ignored paths, to determine which untracked dirs must be
skipped (because they contain ignored paths) and which ones should *not*
be skipped.
For this purpose, correct_untracked_entries() is introduced to prune a
given dir_struct of untracked entries containing ignored paths and those
untracked entries encompassed by the untracked entries which are not
pruned away.
A memory leak is also fixed in cmd_clean().
This also fixes the known breakage in t7300, since clean -d now skips
untracked directories containing ignored paths.
Signed-off-by: Samuel Lijin <sxlijin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-05-23 10:09:37 +00:00
|
|
|
|
2022-01-11 18:05:00 +00:00
|
|
|
prepare_repo_settings(the_repository);
|
|
|
|
the_repository->settings.command_requires_full_index = 0;
|
|
|
|
|
2022-11-19 13:07:38 +00:00
|
|
|
if (repo_read_index(the_repository) < 0)
|
2011-02-22 23:42:21 +00:00
|
|
|
die(_("index file corrupt"));
|
2009-08-20 13:47:01 +00:00
|
|
|
|
2019-09-03 18:04:57 +00:00
|
|
|
pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
|
2010-07-20 19:35:56 +00:00
|
|
|
for (i = 0; i < exclude_list.nr; i++)
|
2019-09-03 18:04:57 +00:00
|
|
|
add_pattern(exclude_list.items[i].string, "", 0, pl, -(i+1));
|
2010-07-20 19:35:56 +00:00
|
|
|
|
2013-07-14 08:35:37 +00:00
|
|
|
parse_pathspec(&pathspec, 0,
|
|
|
|
PATHSPEC_PREFER_CWD,
|
|
|
|
prefix, argv);
|
2007-11-12 01:48:47 +00:00
|
|
|
|
2017-05-05 19:53:34 +00:00
|
|
|
fill_directory(&dir, &the_index, &pathspec);
|
clean: teach clean -d to preserve ignored paths
There is an implicit assumption that a directory containing only
untracked and ignored paths should itself be considered untracked. This
makes sense in use cases where we're asking if a directory should be
added to the git database, but not when we're asking if a directory can
be safely removed from the working tree; as a result, clean -d would
assume that an "untracked" directory containing ignored paths could be
deleted, even though doing so would also remove the ignored paths.
To get around this, we teach clean -d to collect ignored paths and skip
an untracked directory if it contained an ignored path, instead just
removing the untracked contents thereof. To achieve this, cmd_clean()
has to collect all untracked contents of untracked directories, in
addition to all ignored paths, to determine which untracked dirs must be
skipped (because they contain ignored paths) and which ones should *not*
be skipped.
For this purpose, correct_untracked_entries() is introduced to prune a
given dir_struct of untracked entries containing ignored paths and those
untracked entries encompassed by the untracked entries which are not
pruned away.
A memory leak is also fixed in cmd_clean().
This also fixes the known breakage in t7300, since clean -d now skips
untracked directories containing ignored paths.
Signed-off-by: Samuel Lijin <sxlijin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-05-23 10:09:37 +00:00
|
|
|
correct_untracked_entries(&dir);
|
2007-12-05 07:55:41 +00:00
|
|
|
|
|
|
|
for (i = 0; i < dir.nr; i++) {
|
|
|
|
struct dir_entry *ent = dir.entries[i];
|
2007-11-12 01:48:47 +00:00
|
|
|
struct stat st;
|
2013-06-25 15:53:47 +00:00
|
|
|
const char *rel;
|
2007-11-12 01:48:47 +00:00
|
|
|
|
2022-11-19 13:07:30 +00:00
|
|
|
if (!index_name_is_other(&the_index, ent->name, ent->len))
|
2014-01-24 13:40:35 +00:00
|
|
|
continue;
|
2007-11-12 01:48:47 +00:00
|
|
|
|
2015-05-18 18:08:46 +00:00
|
|
|
if (lstat(ent->name, &st))
|
|
|
|
die_errno("Cannot lstat '%s'", ent->name);
|
|
|
|
|
2021-05-06 19:33:15 +00:00
|
|
|
if (S_ISDIR(st.st_mode) && !remove_directories)
|
2014-03-10 17:24:47 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
rel = relative_path(ent->name, prefix, &buf);
|
|
|
|
string_list_append(&del_list, rel);
|
2013-06-25 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
dir: fix problematic API to avoid memory leaks
The dir structure seemed to have a number of leaks and problems around
it. First I noticed that parent_hashmap and recursive_hashmap were
being leaked (though Peff noticed and submitted fixes before me). Then
I noticed in the previous commit that clear_directory() was only taking
responsibility for a subset of fields within dir_struct, despite the
fact that entries[] and ignored[] we allocated internally to dir.c.
That, of course, resulted in many callers either leaking or haphazardly
trying to free these arrays and their contents.
Digging further, I found that despite the pretty clear documentation
near the top of dir.h that folks were supposed to call clear_directory()
when the user no longer needed the dir_struct, there were four callers
that didn't bother doing that at all. However, two of them clearly
thought about leaks since they had an UNLEAK(dir) directive, which to me
suggests that the method to free the data was too unclear. I suspect
the non-obviousness of the API and its holes led folks to avoid it,
which then snowballed into further problems with the entries[],
ignored[], parent_hashmap, and recursive_hashmap problems.
Rename clear_directory() to dir_clear() to be more in line with other
data structures in git, and introduce a dir_init() to handle the
suggested memsetting of dir_struct to all zeroes. I hope that a name
like "dir_clear()" is more clear, and that the presence of dir_init()
will provide a hint to those looking at the code that they need to look
for either a dir_clear() or a dir_free() and lead them to find
dir_clear().
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-08-18 22:58:26 +00:00
|
|
|
dir_clear(&dir);
|
clean: teach clean -d to preserve ignored paths
There is an implicit assumption that a directory containing only
untracked and ignored paths should itself be considered untracked. This
makes sense in use cases where we're asking if a directory should be
added to the git database, but not when we're asking if a directory can
be safely removed from the working tree; as a result, clean -d would
assume that an "untracked" directory containing ignored paths could be
deleted, even though doing so would also remove the ignored paths.
To get around this, we teach clean -d to collect ignored paths and skip
an untracked directory if it contained an ignored path, instead just
removing the untracked contents thereof. To achieve this, cmd_clean()
has to collect all untracked contents of untracked directories, in
addition to all ignored paths, to determine which untracked dirs must be
skipped (because they contain ignored paths) and which ones should *not*
be skipped.
For this purpose, correct_untracked_entries() is introduced to prune a
given dir_struct of untracked entries containing ignored paths and those
untracked entries encompassed by the untracked entries which are not
pruned away.
A memory leak is also fixed in cmd_clean().
This also fixes the known breakage in t7300, since clean -d now skips
untracked directories containing ignored paths.
Signed-off-by: Samuel Lijin <sxlijin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-05-23 10:09:37 +00:00
|
|
|
|
2013-06-25 15:53:48 +00:00
|
|
|
if (interactive && del_list.nr > 0)
|
|
|
|
interactive_main_loop();
|
2013-06-25 15:53:47 +00:00
|
|
|
|
|
|
|
for_each_string_list_item(item, &del_list) {
|
|
|
|
struct stat st;
|
|
|
|
|
2019-09-17 16:35:04 +00:00
|
|
|
strbuf_reset(&abs_path);
|
2013-06-25 15:53:47 +00:00
|
|
|
if (prefix)
|
|
|
|
strbuf_addstr(&abs_path, prefix);
|
|
|
|
|
|
|
|
strbuf_addstr(&abs_path, item->string);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we might have removed this as part of earlier
|
|
|
|
* recursive directory removal, so lstat() here could
|
|
|
|
* fail with ENOENT.
|
|
|
|
*/
|
|
|
|
if (lstat(abs_path.buf, &st))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
if (remove_dirs(&abs_path, prefix, rm_flags, dry_run, quiet, &gone))
|
|
|
|
errors++;
|
|
|
|
if (gone && !quiet) {
|
2020-09-10 17:01:54 +00:00
|
|
|
qname = quote_path(item->string, NULL, &buf, 0);
|
2013-06-25 15:53:47 +00:00
|
|
|
printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res = dry_run ? 0 : unlink(abs_path.buf);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
if (res) {
|
2017-02-14 09:54:49 +00:00
|
|
|
int saved_errno = errno;
|
2020-09-10 17:01:54 +00:00
|
|
|
qname = quote_path(item->string, NULL, &buf, 0);
|
2017-02-14 09:54:49 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
warning_errno(_(msg_warn_remove_failed), qname);
|
2008-02-21 01:44:46 +00:00
|
|
|
errors++;
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
} else if (!quiet) {
|
2020-09-10 17:01:54 +00:00
|
|
|
qname = quote_path(item->string, NULL, &buf, 0);
|
git-clean: Display more accurate delete messages
(1) Only print out the names of the files and directories that got
actually deleted. Also do not mention that we are not removing
directories when the user did not ask us to do so with '-d'.
(2) Show ignore message for skipped untracked git repositories.
Consider the following repo layout:
test.git/
|-- tracked_dir/
| |-- some_tracked_file
| |-- some_untracked_file
|-- tracked_file
|-- untracked_file
|-- untracked_foo/
| |-- bar/
| | |-- bar.txt
| |-- emptydir/
| |-- frotz.git/
| |-- frotz.tx
|-- untracked_some.git/
|-- some.txt
Suppose the user issues 'git clean -fd' from the test.git directory.
When -d option is used and untracked directory 'foo' contains a
subdirectory 'frotz.git' that is managed by a different git repository
therefore it will not be removed.
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Removing untracked_foo/
Removing untracked_some.git/
The message displayed to the user is slightly misleading. The foo/
directory has not been removed because of foo/frotz.git still exists.
On the other hand the subdirectories 'bar' and 'emptydir' have been
deleted but they're not mentioned anywhere. Also, untracked_some.git
has not been removed either.
This behaviour is the result of the way the deletion of untracked
directories are reported. In the current implementation they are
deleted recursively but only the name of the top most directory is
printed out. The calling function does not know about any
subdirectories that could not be removed during the recursion.
Improve the way the deleted directories are reported back to
the user:
(1) Create a recursive delete function 'remove_dirs' in builtin/clean.c
to run in both dry_run and delete modes with the delete logic as
follows:
(a) Check if the current directory to be deleted is an untracked
git repository. If it is and --force --force option is not set
do not touch this directory, print ignore message, set dir_gone
flag to false for the caller and return.
(b) Otherwise for each item in current directory:
(i) If current directory cannot be accessed, print warning,
set dir_gone flag to false and return.
(ii) If the item is a subdirectory recurse into it,
check for the returned value of the dir_gone flag.
If the subdirectory is gone, add the name of the deleted
directory to a list of successfully removed items 'dels'.
Else set the dir_gone flag as the current directory
cannot be removed because we have at least one subdirectory
hanging around.
(iii) If it is a file try to remove it. If success add the
file name to the 'dels' list, else print error and set
dir_gone flag to false.
(c) After we finished deleting all items in the current directory and
the dir_gone flag is still true, remove the directory itself.
If failed set the dir_gone flag to false.
(d) If the current directory cannot be deleted because the dir_gone flag
has been set to false, print out all the successfully deleted items
for this directory from the 'dels' list.
(e) We're done with the current directory, return.
(2) Modify the cmd_clean() function to:
(a) call the recursive delete function 'remove_dirs()' for each
topmost directory it wants to remove
(b) check for the returned value of dir_gone flag. If it's true
print the name of the directory as being removed.
Consider the output of the improved version:
$ git clean -fd
Removing tracked_dir/some_untracked_file
Removing untracked_file
Skipping repository untracked_foo/frotz.git
Removing untracked_foo/bar
Removing untracked_foo/emptydir
Skipping repository untracked_some.git/
Now it displays only the file and directory names that got actually
deleted and shows the name of the untracked git repositories it ignored.
Reported-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
Signed-off-by: Zoltan Klinger <zoltan.klinger@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-01-10 22:53:46 +00:00
|
|
|
printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
|
2008-02-21 01:44:46 +00:00
|
|
|
}
|
2007-11-12 01:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:47 +00:00
|
|
|
strbuf_release(&abs_path);
|
|
|
|
strbuf_release(&buf);
|
|
|
|
string_list_clear(&del_list, 0);
|
2010-07-20 19:35:56 +00:00
|
|
|
string_list_clear(&exclude_list, 0);
|
2023-02-06 23:07:40 +00:00
|
|
|
clear_pathspec(&pathspec);
|
2008-02-21 01:44:46 +00:00
|
|
|
return (errors != 0);
|
2007-11-12 01:48:47 +00:00
|
|
|
}
|