]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/ls-files.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / builtin / ls-files.c
index 29a26ad8ae483b6055268ca2a238b8b7e9c09b74..ce083cb8efddd0db49f546b5ac0ab60a3fb4ec4a 100644 (file)
@@ -8,9 +8,13 @@
 #include "cache.h"
 #include "repository.h"
 #include "config.h"
+#include "convert.h"
 #include "quote.h"
 #include "dir.h"
 #include "builtin.h"
+#include "gettext.h"
+#include "object-name.h"
+#include "strbuf.h"
 #include "tree.h"
 #include "cache-tree.h"
 #include "parse-options.h"
@@ -18,6 +22,7 @@
 #include "string-list.h"
 #include "pathspec.h"
 #include "run-command.h"
+#include "setup.h"
 #include "submodule.h"
 #include "submodule-config.h"
 
@@ -37,6 +42,7 @@ static int debug_mode;
 static int show_eol;
 static int recurse_submodules;
 static int skipping_duplicates;
+static int show_sparse_dirs;
 
 static const char *prefix;
 static int max_prefix_len;
@@ -47,6 +53,7 @@ static char *ps_matched;
 static const char *with_tree;
 static int exc_given;
 static int exclude_args;
+static const char *format;
 
 static const char *tag_cached = "";
 static const char *tag_unmerged = "";
@@ -84,6 +91,16 @@ static void write_name(const char *name)
                                   stdout, line_terminator);
 }
 
+static void write_name_to_buf(struct strbuf *sb, const char *name)
+{
+       const char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);
+
+       if (line_terminator)
+               quote_c_style(rel, sb, NULL, 0);
+       else
+               strbuf_addstr(sb, rel);
+}
+
 static const char *get_tag(const struct cache_entry *ce, const char *tag)
 {
        static char alttag[4];
@@ -209,10 +226,8 @@ static void show_submodule(struct repository *superproject,
                           struct dir_struct *dir, const char *path)
 {
        struct repository subrepo;
-       const struct submodule *sub = submodule_from_path(superproject,
-                                                         null_oid(), path);
 
-       if (repo_submodule_init(&subrepo, superproject, sub))
+       if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
                return;
 
        if (repo_read_index(&subrepo) < 0)
@@ -223,6 +238,73 @@ static void show_submodule(struct repository *superproject,
        repo_clear(&subrepo);
 }
 
+struct show_index_data {
+       const char *pathname;
+       struct index_state *istate;
+       const struct cache_entry *ce;
+};
+
+static size_t expand_show_index(struct strbuf *sb, const char *start,
+                               void *context)
+{
+       struct show_index_data *data = context;
+       const char *end;
+       const char *p;
+       size_t len = strbuf_expand_literal_cb(sb, start, NULL);
+       struct stat st;
+
+       if (len)
+               return len;
+       if (*start != '(')
+               die(_("bad ls-files format: element '%s' "
+                     "does not start with '('"), start);
+
+       end = strchr(start + 1, ')');
+       if (!end)
+               die(_("bad ls-files format: element '%s' "
+                     "does not end in ')'"), start);
+
+       len = end - start + 1;
+       if (skip_prefix(start, "(objectmode)", &p))
+               strbuf_addf(sb, "%06o", data->ce->ce_mode);
+       else if (skip_prefix(start, "(objectname)", &p))
+               strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);
+       else if (skip_prefix(start, "(stage)", &p))
+               strbuf_addf(sb, "%d", ce_stage(data->ce));
+       else if (skip_prefix(start, "(eolinfo:index)", &p))
+               strbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?
+                             get_cached_convert_stats_ascii(data->istate,
+                             data->ce->name) : "");
+       else if (skip_prefix(start, "(eolinfo:worktree)", &p))
+               strbuf_addstr(sb, !lstat(data->pathname, &st) &&
+                             S_ISREG(st.st_mode) ?
+                             get_wt_convert_stats_ascii(data->pathname) : "");
+       else if (skip_prefix(start, "(eolattr)", &p))
+               strbuf_addstr(sb, get_convert_attr_ascii(data->istate,
+                             data->pathname));
+       else if (skip_prefix(start, "(path)", &p))
+               write_name_to_buf(sb, data->pathname);
+       else
+               die(_("bad ls-files format: %%%.*s"), (int)len, start);
+
+       return len;
+}
+
+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
+                       const char *format, const char *fullname) {
+       struct show_index_data data = {
+               .pathname = fullname,
+               .istate = repo->index,
+               .ce = ce,
+       };
+       struct strbuf sb = STRBUF_INIT;
+
+       strbuf_expand(&sb, format, expand_show_index, &data);
+       strbuf_addch(&sb, line_terminator);
+       fwrite(sb.buf, sb.len, 1, stdout);
+       strbuf_release(&sb);
+}
+
 static void show_ce(struct repository *repo, struct dir_struct *dir,
                    const struct cache_entry *ce, const char *fullname,
                    const char *tag)
@@ -237,6 +319,12 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,
                                  max_prefix_len, ps_matched,
                                  S_ISDIR(ce->ce_mode) ||
                                  S_ISGITLINK(ce->ce_mode))) {
+               if (format) {
+                       show_ce_fmt(repo, ce, format, fullname);
+                       print_debug(ce);
+                       return;
+               }
+
                tag = get_tag(ce, tag);
 
                if (!show_stage) {
@@ -245,7 +333,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,
                        printf("%s%06o %s %d\t",
                               tag,
                               ce->ce_mode,
-                              find_unique_abbrev(&ce->oid, abbrev),
+                              repo_find_unique_abbrev(repo, &ce->oid, abbrev),
                               ce_stage(ce));
                }
                write_eolinfo(repo->index, ce, fullname);
@@ -276,7 +364,7 @@ static void show_ru_info(struct index_state *istate)
                        if (!ui->mode[i])
                                continue;
                        printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
-                              find_unique_abbrev(&ui->oid[i], abbrev),
+                              repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev),
                               i + 1);
                        write_name(path);
                }
@@ -317,8 +405,10 @@ static void show_files(struct repository *repo, struct dir_struct *dir)
 
        if (!(show_cached || show_stage || show_deleted || show_modified))
                return;
-       /* TODO: audit for interaction with sparse-index. */
-       ensure_full_index(repo->index);
+
+       if (!show_sparse_dirs)
+               ensure_full_index(repo->index);
+
        for (i = 0; i < repo->index->cache_nr; i++) {
                const struct cache_entry *ce = repo->index->cache[i];
                struct stat st;
@@ -489,7 +579,7 @@ void overlay_tree_on_index(struct index_state *istate,
        read_tree_fn_t fn = NULL;
        int err;
 
-       if (get_oid(tree_name, &oid))
+       if (repo_get_oid(the_repository, tree_name, &oid))
                die("tree-ish %s not found.", tree_name);
        tree = parse_tree_indirect(&oid);
        if (!tree)
@@ -527,6 +617,7 @@ void overlay_tree_on_index(struct index_state *istate,
        if (!fn)
                fn = read_one_entry_quick;
        err = read_tree(the_repository, tree, &pathspec, fn, istate);
+       clear_pathspec(&pathspec);
        if (err)
                die("unable to read tree entries %s", tree_name);
 
@@ -614,7 +705,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
        struct option builtin_ls_files_options[] = {
                /* Think twice before adding "--nul" synonym to this */
                OPT_SET_INT('z', NULL, &line_terminator,
-                       N_("paths are separated with NUL character"), '\0'),
+                       N_("separate paths with the NUL character"), '\0'),
                OPT_BOOL('t', NULL, &show_tag,
                        N_("identify the file status with tags")),
                OPT_BOOL('v', NULL, &show_valid_bit,
@@ -651,7 +742,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
                        N_("skip files matching pattern"),
                        PARSE_OPT_NONEG, option_parse_exclude),
                OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
-                       N_("exclude patterns are read from <file>"),
+                       N_("read exclude patterns from <file>"),
                        PARSE_OPT_NONEG, option_parse_exclude_from),
                OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
                        N_("read additional per-directory exclude patterns in <file>")),
@@ -672,12 +763,21 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
                OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
                OPT_BOOL(0, "deduplicate", &skipping_duplicates,
                         N_("suppress duplicate entries")),
+               OPT_BOOL(0, "sparse", &show_sparse_dirs,
+                        N_("show sparse directories in the presence of a sparse index")),
+               OPT_STRING_F(0, "format", &format, N_("format"),
+                            N_("format to use for the output"),
+                            PARSE_OPT_NONEG),
                OPT_END()
        };
+       int ret = 0;
 
        if (argc == 2 && !strcmp(argv[1], "-h"))
                usage_with_options(ls_files_usage, builtin_ls_files_options);
 
+       prepare_repo_settings(the_repository);
+       the_repository->settings.command_requires_full_index = 0;
+
        prefix = cmd_prefix;
        if (prefix)
                prefix_len = strlen(prefix);
@@ -692,6 +792,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
        for (i = 0; i < exclude_list.nr; i++) {
                add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
        }
+
+       if (format && (show_stage || show_others || show_killed ||
+               show_resolve_undo || skipping_duplicates || show_eol || show_tag))
+                       usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, "
+                                     "--resolve-undo, --deduplicate, --eol"),
+                                     ls_files_usage, builtin_ls_files_options);
+
        if (show_tag || show_valid_bit || show_fsmonitor_bit) {
                tag_cached = "H ";
                tag_unmerged = "M ";
@@ -719,7 +826,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
                setup_work_tree();
 
        if (recurse_submodules &&
-           (show_stage || show_deleted || show_others || show_unmerged ||
+           (show_deleted || show_others || show_unmerged ||
             show_killed || show_modified || show_resolve_undo || with_tree))
                die("ls-files --recurse-submodules unsupported mode");
 
@@ -768,7 +875,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
                 * would not make any sense with this option.
                 */
                if (show_stage || show_unmerged)
-                       die("ls-files --with-tree is incompatible with -s or -u");
+                       die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
                overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
        }
 
@@ -777,16 +884,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
        if (show_resolve_undo)
                show_ru_info(the_repository->index);
 
-       if (ps_matched) {
-               int bad;
-               bad = report_path_error(ps_matched, &pathspec);
-               if (bad)
-                       fprintf(stderr, "Did you forget to 'git add'?\n");
-
-               return bad ? 1 : 0;
+       if (ps_matched && report_path_error(ps_matched, &pathspec)) {
+               fprintf(stderr, "Did you forget to 'git add'?\n");
+               ret = 1;
        }
 
+       string_list_clear(&exclude_list, 0);
        dir_clear(&dir);
        free(max_prefix);
-       return 0;
+       return ret;
 }