From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Jonathan Niedier <jrnieder@gmail.com>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 14/16] ls: immitate UNIX ls output style
Date: Wed, 9 Feb 2011 19:24:42 +0700 [thread overview]
Message-ID: <1297254284-3729-15-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1297254284-3729-1-git-send-email-pclouds@gmail.com>
Two points:
- When input is a mixed of directories and files, files will be
grouped and displayed first. Directory content will follow.
- GNU ls when examining a directory will output files relative to
that directory.
Index does not have directories. There nearest thing to that is
pathspecs that expand to more than one item. So in Git the rules become:
- Multiple match pathspecs is printed with a header, the pathspec,
then the content (its expansion). If there is only one pathspec, no
header will be printed.
- The common directory prefix of all files from a pathspec will be
stripped (not yet implemented)
- One-match pathspecs will be grouped and printed out first like
the result of a virtual multiple match pathspec.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
Mixing multiple entry types (others and cached) will result in
duplicates and unsorted output. The command is a porcelain now,
perhaps we should sort/uniq output?
one_match_pathspec() needs better impl.
builtin/ls-files.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 96 insertions(+), 5 deletions(-)
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 87ee728..22e0c87 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -676,10 +676,46 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
return 0;
}
+static int one_match_pathspec(const char *pathspec)
+{
+ struct stat st;
+ int len = strlen(pathspec);
+
+ if (show_cached) {
+ char *new_path;
+ int pos = index_name_pos(&the_index, pathspec, len);
+ if (pos >= 0)
+ return 1;
+
+ new_path = xmalloc(len+2);
+ memcpy(new_path, pathspec, len);
+ new_path[len++] = '/';
+ new_path[len] = 0;
+ pos = index_name_pos(&the_index, pathspec, len);
+ if (pos >= 0)
+ die("BUG: Wait there are directories in index??");
+ pos = -pos-1;
+
+ /* dir match */
+ if (!strncmp(the_index.cache[pos]->name, new_path, len)) {
+ free(new_path);
+ return 0;
+ }
+ free(new_path);
+ }
+
+ if (!stat(pathspec, &st) && !S_ISDIR(st.st_mode))
+ return 1;
+
+ return 0;
+}
+
int cmd_ls(int argc, const char **argv, const char *cmd_prefix)
{
+ int show_pathspec;
+ const char **pathspecs, **one_matches = NULL;
struct dir_struct dir;
- int recursive = 0;
+ int recursive = 0, src, dst, len;
struct option builtin_ls_files_options[] = {
OPT_BOOLEAN('c', "cached", &show_cached,
"show cached files in the output (default)"),
@@ -720,20 +756,75 @@ int cmd_ls(int argc, const char **argv, const char *cmd_prefix)
if (dir.flags & DIR_SHOW_IGNORED || show_others)
setup_standard_excludes(&dir);
- pathspec = get_pathspec(prefix, argv);
+ pathspecs = get_pathspec(prefix, argv);
if (show_modified)
- refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL);
+ refresh_index(&the_index, REFRESH_QUIET, pathspecs, NULL, NULL);
+
+ if (!pathspecs) {
+ static const char *spec[2];
+ spec[0] = ".";
+ spec[1] = NULL;
+ pathspecs = get_pathspec(prefix, spec);
+ }
/* With no flags, we default to showing the cached files */
if (!(show_stage | show_deleted | show_others | show_unmerged |
show_killed | show_modified | show_resolve_undo))
show_cached = 1;
+ /*
+ * Group one-match pathspecs together. Shell expansion may
+ * turn foo* to fooa foob/ and fooc. List fooa and fooc, leave
+ * foob for later.
+ */
+ len = src = dst = 0;
+ while (pathspecs[src]) {
+ if (one_match_pathspec(pathspecs[src])) {
+ len++;
+ one_matches = xrealloc(one_matches, sizeof(*one_matches) * (len+1));
+ one_matches[len - 1] = pathspecs[src];
+ src++;
+ continue;
+ }
+ if (src != dst)
+ pathspecs[dst] = pathspecs[src];
+ src++;
+ dst++;
+ }
+ pathspecs[dst] = NULL;
+
if (!recursive)
depth_limit = 1;
- show_files(&dir);
- display_columns(&output, column_mode, term_columns(), 2, NULL);
+ if (len) {
+ one_matches[len] = NULL;
+ pathspec = one_matches;
+ show_files(&dir);
+ display_columns(&output, column_mode, term_columns(), 2, NULL);
+ string_list_clear(&output, 0);
+ if (dst)
+ printf("\n");
+ }
+
+ if (!pathspecs[0])
+ return 0;
+
+ show_pathspec = dst > 1 || len;
+ pathspec = xmalloc(sizeof(*pathspec)*2);
+ pathspec[1] = NULL;
+ while (*pathspecs) {
+ pathspec[0] = pathspecs[0];
+ pathspecs++;
+
+ show_files(&dir);
+ if (output.nr && show_pathspec)
+ printf("%s:\n", pathspec[0]);
+
+ display_columns(&output, column_mode, term_columns(), 2, NULL);
+ string_list_clear(&output, 0);
+ if (show_pathspec && pathspecs[0])
+ printf("\n");
+ }
return 0;
}
--
1.7.2.2
next prev parent reply other threads:[~2011-02-09 12:28 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-09 12:24 [PATCH 00/16] column output (v2) and git-ls Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 01/16] Move term_columns() to pager.c and save terminal width before pager Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 02/16] Add display_columns() to display in column layout Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 03/16] display_columns: add COL_MODE_{COLUMN,ROW} mode Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 04/16] display_columns: add COL_DENSE to do unequal column layout Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 05/16] Add test-column for testing " Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 06/16] Add core.column Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 07/16] parseopt: OPT_COLUMN to set struct column_layout.mode Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 08/16] help: reuse display_columns() for help -a Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 09/16] tag: add --column Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 10/16] branch: " Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 11/16] Add ls command Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 12/16] ls: add --column Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 13/16] ls: add --recursive and turn default to non-recursive mode Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` Nguyễn Thái Ngọc Duy [this message]
2011-02-09 12:24 ` [PATCH 15/16] ls: strip common directory prefix from output Nguyễn Thái Ngọc Duy
2011-02-09 12:24 ` [PATCH 16/16] ls: color output Nguyễn Thái Ngọc Duy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1297254284-3729-15-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.org \
--cc=jrnieder@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).