Skip to content

Instantly share code, notes, and snippets.

@niha
Created July 1, 2010 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niha/460117 to your computer and use it in GitHub Desktop.
Save niha/460117 to your computer and use it in GitHub Desktop.
--- ls-files.c 2010-07-02 00:29:48.206993409 +0900
+++ ls-files.c.orig 2010-07-02 00:29:41.022986503 +0900
@@ -13,6 +13,7 @@
#include "parse-options.h"
#include "resolve-undo.h"
#include "string-list.h"
+#include "color.h"
static int abbrev;
static int show_deleted;
@@ -44,13 +45,22 @@
static const char *tag_skip_worktree = "";
static const char *tag_resolve_undo = "";
+static const char *color_cached = GIT_COLOR_NORMAL;
+static const char *color_unmerged = GIT_COLOR_NORMAL;
+static const char *color_removed = GIT_COLOR_NORMAL;
+static const char *color_other = GIT_COLOR_NORMAL;
+static const char *color_killed = GIT_COLOR_NORMAL;
+static const char *color_modified = GIT_COLOR_NORMAL;
+static const char *color_skip_worktree = GIT_COLOR_NORMAL;
+static const char *color_resolve_undo = GIT_COLOR_NORMAL;
+
static void write_name(const char* name, size_t len)
{
write_name_quoted_relative(name, len, prefix, prefix_len, stdout,
line_terminator);
}
-static void show_dir_entry(const char *tag, struct dir_entry *ent)
+static void show_dir_entry(const char *tag, const char *color, struct dir_entry *ent)
{
int len = max_prefix_len;
@@ -60,7 +70,7 @@
if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
return;
- fputs(tag, stdout);
+ color_fprintf(stdout, color, tag);
write_name(ent->name, ent->len);
}
@@ -72,7 +82,7 @@
struct dir_entry *ent = dir->entries[i];
if (!cache_name_is_other(ent->name, ent->len))
continue;
- show_dir_entry(tag_other, ent);
+ show_dir_entry(tag_other, color_other, ent);
}
}
@@ -121,11 +131,11 @@
}
}
if (killed)
- show_dir_entry(tag_killed, dir->entries[i]);
+ show_dir_entry(tag_killed, color_killed, dir->entries[i]);
}
}
-static void show_ce_entry(const char *tag, struct cache_entry *ce)
+static void show_ce_entry(const char *tag, const char *color, struct cache_entry *ce)
{
int len = max_prefix_len;
@@ -153,7 +163,7 @@
}
if (!show_stage) {
- fputs(tag, stdout);
+ color_fprintf(stdout, color, tag);
} else {
printf("%s%06o %s %d\t",
tag,
@@ -217,7 +227,9 @@
if (ce->ce_flags & CE_UPDATE)
continue;
show_ce_entry(ce_stage(ce) ? tag_unmerged :
- (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
+ (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached),
+ ce_stage(ce) ? color_unmerged :
+ (ce_skip_worktree(ce) ? color_skip_worktree : color_cached), ce);
}
}
if (show_deleted | show_modified) {
@@ -235,9 +247,9 @@
continue;
err = lstat(ce->name, &st);
if (show_deleted && err)
- show_ce_entry(tag_removed, ce);
+ show_ce_entry(tag_removed, color_removed, ce);
if (show_modified && ce_modified(ce, &st, 0))
- show_ce_entry(tag_modified, ce);
+ show_ce_entry(tag_modified, color_modified, ce);
}
}
}
@@ -464,7 +476,7 @@
int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
{
- int require_work_tree = 0, show_tag = 0;
+ int require_work_tree = 0, show_tag = 0, colored = 0;
const char *max_prefix;
struct dir_struct dir;
struct option builtin_ls_files_options[] = {
@@ -516,6 +528,8 @@
PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
OPT_BOOLEAN(0, "error-unmatch", &error_unmatch,
"if any <file> is not in the index, treat this as an error"),
+ OPT_BOOLEAN(0, "color", &colored,
+ "use colored output"),
OPT_STRING(0, "with-tree", &with_tree, "tree-ish",
"pretend that paths removed since <tree-ish> are still present"),
OPT__ABBREV(&abbrev),
@@ -543,6 +557,16 @@
tag_skip_worktree = "S ";
tag_resolve_undo = "U ";
}
+ if (colored) {
+ color_cached = GIT_COLOR_NORMAL;
+ color_unmerged = GIT_COLOR_BOLD_RED;
+ color_removed = GIT_COLOR_NORMAL;
+ color_modified = GIT_COLOR_BOLD_YELLOW;
+ color_other = GIT_COLOR_NORMAL;
+ color_killed = GIT_COLOR_NORMAL;
+ color_skip_worktree = GIT_COLOR_NORMAL;
+ color_resolve_undo = GIT_COLOR_NORMAL;
+ }
if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
require_work_tree = 1;
if (show_unmerged)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment