Allow the test suite to pass in a directory whose name contains spaces
[gitweb.git] / builtin / clean.c
index df53def63f29e295e09261b9bd56960cf27582b1..937eb17b66ca8984a040e32b575eca43cfccb0e2 100644 (file)
@@ -147,31 +147,6 @@ static int exclude_cb(const struct option *opt, const char *arg, int unset)
        return 0;
 }
 
-/*
- * Return 1 if the given path is the root of a git repository or
- * submodule else 0. Will not return 1 for bare repositories with the
- * exception of creating a bare repository in "foo/.git" and calling
- * is_git_repository("foo").
- */
-static int is_git_repository(struct strbuf *path)
-{
-       int ret = 0;
-       int gitfile_error;
-       size_t orig_path_len = path->len;
-       assert(orig_path_len != 0);
-       if (path->buf[orig_path_len - 1] != '/')
-               strbuf_addch(path, '/');
-       strbuf_addstr(path, ".git");
-       if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf))
-               ret = 1;
-       if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED ||
-           gitfile_error == READ_GITFILE_ERR_READ_FAILED)
-               ret = 1;  /* This could be a real .git file, take the
-                          * safe option and avoid cleaning */
-       strbuf_setlen(path, orig_path_len);
-       return ret;
-}
-
 static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                int dry_run, int quiet, int *dir_gone)
 {
@@ -183,7 +158,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
 
        *dir_gone = 1;
 
-       if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && is_git_repository(path)) {
+       if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && is_nonbare_repository_dir(path)) {
                if (!quiet) {
                        quote_path_relative(path->buf, prefix, &quoted);
                        printf(dry_run ?  _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
@@ -199,15 +174,16 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                /* 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, &quoted);
-                       warning(_(msg_warn_remove_failed), quoted.buf);
+                       errno = saved_errno;
+                       warning_errno(_(msg_warn_remove_failed), quoted.buf);
                        *dir_gone = 0;
                }
                return res;
        }
 
-       if (path->buf[original_len - 1] != '/')
-               strbuf_addch(path, '/');
+       strbuf_complete(path, '/');
 
        len = path->len;
        while ((e = readdir(dir)) != NULL) {
@@ -234,8 +210,10 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                                quote_path_relative(path->buf, prefix, &quoted);
                                string_list_append(&dels, quoted.buf);
                        } else {
+                               int saved_errno = errno;
                                quote_path_relative(path->buf, prefix, &quoted);
-                               warning(_(msg_warn_remove_failed), quoted.buf);
+                               errno = saved_errno;
+                               warning_errno(_(msg_warn_remove_failed), quoted.buf);
                                *dir_gone = 0;
                                ret = 1;
                        }
@@ -256,8 +234,10 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                if (!res)
                        *dir_gone = 1;
                else {
+                       int saved_errno = errno;
                        quote_path_relative(path->buf, prefix, &quoted);
-                       warning(_(msg_warn_remove_failed), quoted.buf);
+                       errno = saved_errno;
+                       warning_errno(_(msg_warn_remove_failed), quoted.buf);
                        *dir_gone = 0;
                        ret = 1;
                }
@@ -313,11 +293,11 @@ static void pretty_print_menus(struct string_list *menu_list)
 static void prompt_help_cmd(int singleton)
 {
        clean_print_color(CLEAN_COLOR_HELP);
-       printf_ln(singleton ?
+       printf(singleton ?
                  _("Prompt help:\n"
                    "1          - select a numbered item\n"
                    "foo        - select item based on unique prefix\n"
-                   "           - (empty) select nothing") :
+                   "           - (empty) select nothing\n") :
                  _("Prompt help:\n"
                    "1          - select a single item\n"
                    "3-5        - select a range of items\n"
@@ -325,7 +305,7 @@ static void prompt_help_cmd(int singleton)
                    "foo        - select item based on unique prefix\n"
                    "-...       - unselect specified items\n"
                    "*          - choose all items\n"
-                   "           - (empty) finish selecting"));
+                   "           - (empty) finish selecting\n"));
        clean_print_color(CLEAN_COLOR_RESET);
 }
 
@@ -534,7 +514,7 @@ static int parse_choice(struct menu_stuff *menu_stuff,
                if (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top ||
                    (is_single && bottom != top)) {
                        clean_print_color(CLEAN_COLOR_ERROR);
-                       printf_ln(_("Huh (%s)?"), (*ptr)->buf);
+                       printf(_("Huh (%s)?\n"), (*ptr)->buf);
                        clean_print_color(CLEAN_COLOR_RESET);
                        continue;
                }
@@ -569,7 +549,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
        int eof = 0;
        int i;
 
-       chosen = xmalloc(sizeof(int) * stuff->nr);
+       ALLOC_ARRAY(chosen, stuff->nr);
        /* set chosen as uninitialized */
        for (i = 0; i < stuff->nr; i++)
                chosen[i] = -1;
@@ -596,7 +576,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
                               clean_get_color(CLEAN_COLOR_RESET));
                }
 
-               if (strbuf_getline(&choice, stdin, '\n') != EOF) {
+               if (strbuf_getline_lf(&choice, stdin) != EOF) {
                        strbuf_trim(&choice);
                } else {
                        eof = 1;
@@ -641,7 +621,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
                                nr += chosen[i];
                }
 
-               result = xcalloc(nr + 1, sizeof(int));
+               result = xcalloc(st_add(nr, 1), sizeof(int));
                for (i = 0; i < stuff->nr && j < nr; i++) {
                        if (chosen[i])
                                result[j++] = i;
@@ -678,7 +658,7 @@ static int filter_by_patterns_cmd(void)
                clean_print_color(CLEAN_COLOR_PROMPT);
                printf(_("Input ignore patterns>> "));
                clean_print_color(CLEAN_COLOR_RESET);
-               if (strbuf_getline(&confirm, stdin, '\n') != EOF)
+               if (strbuf_getline_lf(&confirm, stdin) != EOF)
                        strbuf_trim(&confirm);
                else
                        putchar('\n');
@@ -776,7 +756,7 @@ static int ask_each_cmd(void)
                        qname = quote_path_relative(item->string, NULL, &buf);
                        /* TRANSLATORS: Make sure to keep [y/N] as is */
                        printf(_("Remove %s [y/N]? "), qname);
-                       if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
+                       if (strbuf_getline_lf(&confirm, stdin) != EOF) {
                                strbuf_trim(&confirm);
                        } else {
                                putchar('\n');
@@ -800,7 +780,7 @@ static int ask_each_cmd(void)
 static int quit_cmd(void)
 {
        string_list_clear(&del_list, 0);
-       printf_ln(_("Bye."));
+       printf(_("Bye.\n"));
        return MENU_RETURN_NO_LOOP;
 }
 
@@ -877,6 +857,38 @@ static void interactive_main_loop(void)
        }
 }
 
+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;
+}
+
 int cmd_clean(int argc, const char **argv, const char *prefix)
 {
        int i, res;
@@ -936,6 +948,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 
        dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
 
+       if (remove_directories)
+               dir.flags |= DIR_SHOW_IGNORED_TOO | DIR_KEEP_UNTRACKED_CONTENTS;
+
        if (read_cache() < 0)
                die(_("index file corrupt"));
 
@@ -951,6 +966,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                       prefix, argv);
 
        fill_directory(&dir, &pathspec);
+       correct_untracked_entries(&dir);
 
        for (i = 0; i < dir.nr; i++) {
                struct dir_entry *ent = dir.entries[i];
@@ -978,6 +994,12 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                string_list_append(&del_list, rel);
        }
 
+       for (i = 0; i < dir.nr; i++)
+               free(dir.entries[i]);
+
+       for (i = 0; i < dir.ignored_nr; i++)
+               free(dir.ignored[i]);
+
        if (interactive && del_list.nr > 0)
                interactive_main_loop();
 
@@ -1007,8 +1029,10 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                } else {
                        res = dry_run ? 0 : unlink(abs_path.buf);
                        if (res) {
+                               int saved_errno = errno;
                                qname = quote_path_relative(item->string, NULL, &buf);
-                               warning(_(msg_warn_remove_failed), qname);
+                               errno = saved_errno;
+                               warning_errno(_(msg_warn_remove_failed), qname);
                                errors++;
                        } else if (!quiet) {
                                qname = quote_path_relative(item->string, NULL, &buf);