/*
* "git clean" builtin command
*
* Copyright (C) 2007 Shawn Bohrer
*
* Based on git-clean.sh by Pavel Roskin
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h"
#include "cache.h"
#include "config.h"
#include "dir.h"
#include "parse-options.h"
#include "string-list.h"
#include "quote.h"
#include "column.h"
#include "color.h"
#include "pathspec.h"
#include "help.h"
static int force = -1; /* unset */
static int interactive;
static struct string_list del_list = STRING_LIST_INIT_DUP;
static unsigned int colopts;
static const char *const builtin_clean_usage[] = {
N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
NULL
};
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");
static const char *msg_warn_lstat_failed = N_("could not lstat %s\n");
enum color_clean {
CLEAN_COLOR_RESET = 0,
CLEAN_COLOR_PLAIN = 1,
CLEAN_COLOR_PROMPT = 2,
CLEAN_COLOR_HEADER = 3,
CLEAN_COLOR_HELP = 4,
CLEAN_COLOR_ERROR = 5
};
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",
};
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,
};
#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;
int (*fn)(void);
};
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;
};
define_list_config_array(color_interactive_slots);
static int git_clean_config(const char *var, const char *value, void *cb)
{
const char *slot_name;
if (starts_with(var, "column."))
return git_column_config(var, value, "clean", &colopts);
/* 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;
}
if (skip_prefix(var, "color.interactive.", &slot_name)) {
int slot = LOOKUP_CONFIG(color_interactive_slots, slot_name);
if (slot < 0)
return 0;
if (!value)
return config_error_nonbool(var);
return color_parse(value, clean_colors[slot]);
}
if (!strcmp(var, "clean.requireforce")) {
force = !git_config_bool(var, value);
return 0;
}
/* inspect the color.ui config variable and others */
return git_color_default_config(var, value, cb);
}
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));
}
static int exclude_cb(const struct option *opt, const char *arg, int unset)
{
struct string_list *exclude_list = opt->value;
BUG_ON_OPT_NEG(unset);
string_list_append(exclude_list, arg);
return 0;
}
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;
struct dirent *e;
int res = 0, ret = 0, gone = 1, original_len = path->len, len;
struct string_list dels = STRING_LIST_INIT_DUP;
*dir_gone = 1;
if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && is_nonbare_repository_dir(path)) {
if (!quiet) {
quote_path_relative(path->buf, prefix, "ed);
printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
quoted.buf);
}
*dir_gone = 0;
goto out;
}
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) {
int saved_errno = errno;
quote_path_relative(path->buf, prefix, "ed);
errno = saved_errno;
warning_errno(_(msg_warn_remove_failed), quoted.buf);
*dir_gone = 0;
}
ret = res;
goto out;
}
|