Merge branch 'bw/ls-files-sans-the-index'

Code clean-up.

* bw/ls-files-sans-the-index:
  ls-files: factor out tag calculation
  ls-files: factor out debug info into a function
  ls-files: convert show_files to take an index
  ls-files: convert show_ce_entry to take an index
  ls-files: convert prune_cache to take an index
  ls-files: convert ce_excluded to take an index
  ls-files: convert show_ru_info to take an index
  ls-files: convert show_other_files to take an index
  ls-files: convert show_killed_files to take an index
  ls-files: convert write_eolinfo to take an index
  ls-files: convert overlay_tree_on_cache to take an index
  tree: convert read_tree to take an index parameter
  convert: convert renormalize_buffer to take an index
  convert: convert convert_to_git to take an index
  convert: convert convert_to_git_filter_fd to take an index
  convert: convert crlf_to_git to take an index
  convert: convert get_cached_convert_stats_ascii to take an index
This commit is contained in:
Junio C Hamano 2017-06-24 14:28:40 -07:00
Родитель 1c3d87cf55 a84f3e59eb
Коммит 5812b3f73b
15 изменённых файлов: 168 добавлений и 123 удалений

Просмотреть файл

@ -2256,7 +2256,7 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
case S_IFREG: case S_IFREG:
if (strbuf_read_file(buf, path, st->st_size) != st->st_size) if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
return error(_("unable to open or read %s"), path); return error(_("unable to open or read %s"), path);
convert_to_git(path, buf->buf, buf->len, buf, 0); convert_to_git(&the_index, path, buf->buf, buf->len, buf, 0);
return 0; return 0;
default: default:
return -1; return -1;

Просмотреть файл

@ -229,7 +229,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
if (strbuf_read(&buf, 0, 0) < 0) if (strbuf_read(&buf, 0, 0) < 0)
die_errno("failed to read from stdin"); die_errno("failed to read from stdin");
} }
convert_to_git(path, buf.buf, buf.len, &buf, 0); convert_to_git(&the_index, path, buf.buf, buf.len, &buf, 0);
origin->file.ptr = buf.buf; origin->file.ptr = buf.buf;
origin->file.size = buf.len; origin->file.size = buf.len;
pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash); pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash);

Просмотреть файл

@ -253,7 +253,8 @@ static int list_paths(struct string_list *list, const char *with_tree,
if (with_tree) { if (with_tree) {
char *max_prefix = common_prefix(pattern); char *max_prefix = common_prefix(pattern);
overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix); overlay_tree_on_index(&the_index, with_tree,
max_prefix ? max_prefix : prefix);
free(max_prefix); free(max_prefix);
} }

Просмотреть файл

@ -53,17 +53,17 @@ static const char *tag_modified = "";
static const char *tag_skip_worktree = ""; static const char *tag_skip_worktree = "";
static const char *tag_resolve_undo = ""; static const char *tag_resolve_undo = "";
static void write_eolinfo(const struct cache_entry *ce, const char *path) static void write_eolinfo(const struct index_state *istate,
const struct cache_entry *ce, const char *path)
{ {
if (!show_eol) if (show_eol) {
return;
else {
struct stat st; struct stat st;
const char *i_txt = ""; const char *i_txt = "";
const char *w_txt = ""; const char *w_txt = "";
const char *a_txt = get_convert_attr_ascii(path); const char *a_txt = get_convert_attr_ascii(path);
if (ce && S_ISREG(ce->ce_mode)) if (ce && S_ISREG(ce->ce_mode))
i_txt = get_cached_convert_stats_ascii(ce->name); i_txt = get_cached_convert_stats_ascii(istate,
ce->name);
if (!lstat(path, &st) && S_ISREG(st.st_mode)) if (!lstat(path, &st) && S_ISREG(st.st_mode))
w_txt = get_wt_convert_stats_ascii(path); w_txt = get_wt_convert_stats_ascii(path);
printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt); printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
@ -93,6 +93,43 @@ static void write_name(const char *name)
strbuf_release(&full_name); strbuf_release(&full_name);
} }
static const char *get_tag(const struct cache_entry *ce, const char *tag)
{
static char alttag[4];
if (tag && *tag && show_valid_bit && (ce->ce_flags & CE_VALID)) {
memcpy(alttag, tag, 3);
if (isalpha(tag[0])) {
alttag[0] = tolower(tag[0]);
} else if (tag[0] == '?') {
alttag[0] = '!';
} else {
alttag[0] = 'v';
alttag[1] = tag[0];
alttag[2] = ' ';
alttag[3] = 0;
}
tag = alttag;
}
return tag;
}
static void print_debug(const struct cache_entry *ce)
{
if (debug_mode) {
const struct stat_data *sd = &ce->ce_stat_data;
printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
}
}
static void show_dir_entry(const char *tag, struct dir_entry *ent) static void show_dir_entry(const char *tag, struct dir_entry *ent)
{ {
int len = max_prefix_len; int len = max_prefix_len;
@ -104,23 +141,25 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
return; return;
fputs(tag, stdout); fputs(tag, stdout);
write_eolinfo(NULL, ent->name); write_eolinfo(NULL, NULL, ent->name);
write_name(ent->name); write_name(ent->name);
} }
static void show_other_files(struct dir_struct *dir) static void show_other_files(const struct index_state *istate,
const struct dir_struct *dir)
{ {
int i; int i;
for (i = 0; i < dir->nr; i++) { for (i = 0; i < dir->nr; i++) {
struct dir_entry *ent = dir->entries[i]; struct dir_entry *ent = dir->entries[i];
if (!cache_name_is_other(ent->name, ent->len)) if (!index_name_is_other(istate, ent->name, ent->len))
continue; continue;
show_dir_entry(tag_other, ent); show_dir_entry(tag_other, ent);
} }
} }
static void show_killed_files(struct dir_struct *dir) static void show_killed_files(const struct index_state *istate,
const struct dir_struct *dir)
{ {
int i; int i;
for (i = 0; i < dir->nr; i++) { for (i = 0; i < dir->nr; i++) {
@ -134,29 +173,29 @@ static void show_killed_files(struct dir_struct *dir)
/* If ent->name is prefix of an entry in the /* If ent->name is prefix of an entry in the
* cache, it will be killed. * cache, it will be killed.
*/ */
pos = cache_name_pos(ent->name, ent->len); pos = index_name_pos(istate, ent->name, ent->len);
if (0 <= pos) if (0 <= pos)
die("BUG: killed-file %.*s not found", die("BUG: killed-file %.*s not found",
ent->len, ent->name); ent->len, ent->name);
pos = -pos - 1; pos = -pos - 1;
while (pos < active_nr && while (pos < istate->cache_nr &&
ce_stage(active_cache[pos])) ce_stage(istate->cache[pos]))
pos++; /* skip unmerged */ pos++; /* skip unmerged */
if (active_nr <= pos) if (istate->cache_nr <= pos)
break; break;
/* pos points at a name immediately after /* pos points at a name immediately after
* ent->name in the cache. Does it expect * ent->name in the cache. Does it expect
* ent->name to be a directory? * ent->name to be a directory?
*/ */
len = ce_namelen(active_cache[pos]); len = ce_namelen(istate->cache[pos]);
if ((ent->len < len) && if ((ent->len < len) &&
!strncmp(active_cache[pos]->name, !strncmp(istate->cache[pos]->name,
ent->name, ent->len) && ent->name, ent->len) &&
active_cache[pos]->name[ent->len] == '/') istate->cache[pos]->name[ent->len] == '/')
killed = 1; killed = 1;
break; break;
} }
if (0 <= cache_name_pos(ent->name, sp - ent->name)) { if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
/* If any of the leading directories in /* If any of the leading directories in
* ent->name is registered in the cache, * ent->name is registered in the cache,
* ent->name will be killed. * ent->name will be killed.
@ -230,7 +269,8 @@ static void show_gitlink(const struct cache_entry *ce)
exit(status); exit(status);
} }
static void show_ce_entry(const char *tag, const struct cache_entry *ce) static void show_ce_entry(const struct index_state *istate,
const char *tag, const struct cache_entry *ce)
{ {
struct strbuf name = STRBUF_INIT; struct strbuf name = STRBUF_INIT;
int len = max_prefix_len; int len = max_prefix_len;
@ -248,22 +288,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
len, ps_matched, len, ps_matched,
S_ISDIR(ce->ce_mode) || S_ISDIR(ce->ce_mode) ||
S_ISGITLINK(ce->ce_mode))) { S_ISGITLINK(ce->ce_mode))) {
if (tag && *tag && show_valid_bit && tag = get_tag(ce, tag);
(ce->ce_flags & CE_VALID)) {
static char alttag[4];
memcpy(alttag, tag, 3);
if (isalpha(tag[0]))
alttag[0] = tolower(tag[0]);
else if (tag[0] == '?')
alttag[0] = '!';
else {
alttag[0] = 'v';
alttag[1] = tag[0];
alttag[2] = ' ';
alttag[3] = 0;
}
tag = alttag;
}
if (!show_stage) { if (!show_stage) {
fputs(tag, stdout); fputs(tag, stdout);
@ -274,30 +299,22 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
find_unique_abbrev(ce->oid.hash, abbrev), find_unique_abbrev(ce->oid.hash, abbrev),
ce_stage(ce)); ce_stage(ce));
} }
write_eolinfo(ce, ce->name); write_eolinfo(istate, ce, ce->name);
write_name(ce->name); write_name(ce->name);
if (debug_mode) { print_debug(ce);
const struct stat_data *sd = &ce->ce_stat_data;
printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
}
} }
strbuf_release(&name); strbuf_release(&name);
} }
static void show_ru_info(void) static void show_ru_info(const struct index_state *istate)
{ {
struct string_list_item *item; struct string_list_item *item;
if (!the_index.resolve_undo) if (!istate->resolve_undo)
return; return;
for_each_string_list_item(item, the_index.resolve_undo) { for_each_string_list_item(item, istate->resolve_undo) {
const char *path = item->string; const char *path = item->string;
struct resolve_undo_info *ui = item->util; struct resolve_undo_info *ui = item->util;
int i, len; int i, len;
@ -319,13 +336,14 @@ static void show_ru_info(void)
} }
} }
static int ce_excluded(struct dir_struct *dir, const struct cache_entry *ce) static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
const struct cache_entry *ce)
{ {
int dtype = ce_to_dtype(ce); int dtype = ce_to_dtype(ce);
return is_excluded(dir, &the_index, ce->name, &dtype); return is_excluded(dir, istate, ce->name, &dtype);
} }
static void show_files(struct dir_struct *dir) static void show_files(struct index_state *istate, struct dir_struct *dir)
{ {
int i; int i;
@ -333,33 +351,33 @@ static void show_files(struct dir_struct *dir)
if (show_others || show_killed) { if (show_others || show_killed) {
if (!show_others) if (!show_others)
dir->flags |= DIR_COLLECT_KILLED_ONLY; dir->flags |= DIR_COLLECT_KILLED_ONLY;
fill_directory(dir, &the_index, &pathspec); fill_directory(dir, istate, &pathspec);
if (show_others) if (show_others)
show_other_files(dir); show_other_files(istate, dir);
if (show_killed) if (show_killed)
show_killed_files(dir); show_killed_files(istate, dir);
} }
if (show_cached || show_stage) { if (show_cached || show_stage) {
for (i = 0; i < active_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
const struct cache_entry *ce = active_cache[i]; const struct cache_entry *ce = istate->cache[i];
if ((dir->flags & DIR_SHOW_IGNORED) && if ((dir->flags & DIR_SHOW_IGNORED) &&
!ce_excluded(dir, ce)) !ce_excluded(dir, istate, ce))
continue; continue;
if (show_unmerged && !ce_stage(ce)) if (show_unmerged && !ce_stage(ce))
continue; continue;
if (ce->ce_flags & CE_UPDATE) if (ce->ce_flags & CE_UPDATE)
continue; continue;
show_ce_entry(ce_stage(ce) ? tag_unmerged : show_ce_entry(istate, 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);
} }
} }
if (show_deleted || show_modified) { if (show_deleted || show_modified) {
for (i = 0; i < active_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
const struct cache_entry *ce = active_cache[i]; const struct cache_entry *ce = istate->cache[i];
struct stat st; struct stat st;
int err; int err;
if ((dir->flags & DIR_SHOW_IGNORED) && if ((dir->flags & DIR_SHOW_IGNORED) &&
!ce_excluded(dir, ce)) !ce_excluded(dir, istate, ce))
continue; continue;
if (ce->ce_flags & CE_UPDATE) if (ce->ce_flags & CE_UPDATE)
continue; continue;
@ -367,9 +385,9 @@ static void show_files(struct dir_struct *dir)
continue; continue;
err = lstat(ce->name, &st); err = lstat(ce->name, &st);
if (show_deleted && err) if (show_deleted && err)
show_ce_entry(tag_removed, ce); show_ce_entry(istate, tag_removed, ce);
if (show_modified && ce_modified(ce, &st, 0)) if (show_modified && ie_modified(istate, ce, &st, 0))
show_ce_entry(tag_modified, ce); show_ce_entry(istate, tag_modified, ce);
} }
} }
} }
@ -377,30 +395,31 @@ static void show_files(struct dir_struct *dir)
/* /*
* Prune the index to only contain stuff starting with "prefix" * Prune the index to only contain stuff starting with "prefix"
*/ */
static void prune_cache(const char *prefix, size_t prefixlen) static void prune_index(struct index_state *istate,
const char *prefix, size_t prefixlen)
{ {
int pos; int pos;
unsigned int first, last; unsigned int first, last;
if (!prefix) if (!prefix)
return; return;
pos = cache_name_pos(prefix, prefixlen); pos = index_name_pos(istate, prefix, prefixlen);
if (pos < 0) if (pos < 0)
pos = -pos-1; pos = -pos-1;
first = pos; first = pos;
last = active_nr; last = istate->cache_nr;
while (last > first) { while (last > first) {
int next = (last + first) >> 1; int next = (last + first) >> 1;
const struct cache_entry *ce = active_cache[next]; const struct cache_entry *ce = istate->cache[next];
if (!strncmp(ce->name, prefix, prefixlen)) { if (!strncmp(ce->name, prefix, prefixlen)) {
first = next+1; first = next+1;
continue; continue;
} }
last = next; last = next;
} }
memmove(active_cache, active_cache + pos, memmove(istate->cache, istate->cache + pos,
(last - pos) * sizeof(struct cache_entry *)); (last - pos) * sizeof(struct cache_entry *));
active_nr = last - pos; istate->cache_nr = last - pos;
} }
static int get_common_prefix_len(const char *common_prefix) static int get_common_prefix_len(const char *common_prefix)
@ -430,7 +449,8 @@ static int get_common_prefix_len(const char *common_prefix)
* that were given from the command line. We are not * that were given from the command line. We are not
* going to write this index out. * going to write this index out.
*/ */
void overlay_tree_on_cache(const char *tree_name, const char *prefix) void overlay_tree_on_index(struct index_state *istate,
const char *tree_name, const char *prefix)
{ {
struct tree *tree; struct tree *tree;
struct object_id oid; struct object_id oid;
@ -445,8 +465,8 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
die("bad tree-ish %s", tree_name); die("bad tree-ish %s", tree_name);
/* Hoist the unmerged entries up to stage #3 to make room */ /* Hoist the unmerged entries up to stage #3 to make room */
for (i = 0; i < active_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
struct cache_entry *ce = active_cache[i]; struct cache_entry *ce = istate->cache[i];
if (!ce_stage(ce)) if (!ce_stage(ce))
continue; continue;
ce->ce_flags |= CE_STAGEMASK; ce->ce_flags |= CE_STAGEMASK;
@ -459,11 +479,11 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
PATHSPEC_PREFER_CWD, prefix, matchbuf); PATHSPEC_PREFER_CWD, prefix, matchbuf);
} else } else
memset(&pathspec, 0, sizeof(pathspec)); memset(&pathspec, 0, sizeof(pathspec));
if (read_tree(tree, 1, &pathspec)) if (read_tree(tree, 1, &pathspec, istate))
die("unable to read tree entries %s", tree_name); die("unable to read tree entries %s", tree_name);
for (i = 0; i < active_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
struct cache_entry *ce = active_cache[i]; struct cache_entry *ce = istate->cache[i];
switch (ce_stage(ce)) { switch (ce_stage(ce)) {
case 0: case 0:
last_stage0 = ce; last_stage0 = ce;
@ -657,7 +677,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
max_prefix = common_prefix(&pathspec); max_prefix = common_prefix(&pathspec);
max_prefix_len = get_common_prefix_len(max_prefix); max_prefix_len = get_common_prefix_len(max_prefix);
prune_cache(max_prefix, max_prefix_len); prune_index(&the_index, max_prefix, max_prefix_len);
/* Treat unmatching pathspec elements as errors */ /* Treat unmatching pathspec elements as errors */
if (pathspec.nr && error_unmatch) if (pathspec.nr && error_unmatch)
@ -678,11 +698,11 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
*/ */
if (show_stage || show_unmerged) if (show_stage || show_unmerged)
die("ls-files --with-tree is incompatible with -s or -u"); die("ls-files --with-tree is incompatible with -s or -u");
overlay_tree_on_cache(with_tree, max_prefix); overlay_tree_on_index(&the_index, with_tree, max_prefix);
} }
show_files(&dir); show_files(&the_index, &dir);
if (show_resolve_undo) if (show_resolve_undo)
show_ru_info(); show_ru_info(&the_index);
if (ps_matched) { if (ps_matched) {
int bad; int bad;

Просмотреть файл

@ -2193,7 +2193,8 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
#define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK) #define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
/* ls-files */ /* ls-files */
void overlay_tree_on_cache(const char *tree_name, const char *prefix); void overlay_tree_on_index(struct index_state *istate,
const char *tree_name, const char *prefix);
char *alias_lookup(const char *alias); char *alias_lookup(const char *alias);
int split_cmdline(char *cmdline, const char ***argv); int split_cmdline(char *cmdline, const char ***argv);

Просмотреть файл

@ -1053,7 +1053,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
if (is_file) { if (is_file) {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) { if (convert_to_git(&the_index, elem->path, result, len, &buf, safe_crlf)) {
free(result); free(result);
result = strbuf_detach(&buf, &len); result = strbuf_detach(&buf, &len);
result_size = len; result_size = len;

Просмотреть файл

@ -1,3 +1,4 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "attr.h" #include "attr.h"
#include "run-command.h" #include "run-command.h"
@ -134,11 +135,12 @@ static const char *gather_convert_stats_ascii(const char *data, unsigned long si
} }
} }
const char *get_cached_convert_stats_ascii(const char *path) const char *get_cached_convert_stats_ascii(const struct index_state *istate,
const char *path)
{ {
const char *ret; const char *ret;
unsigned long sz; unsigned long sz;
void *data = read_blob_data_from_cache(path, &sz); void *data = read_blob_data_from_index(istate, path, &sz);
ret = gather_convert_stats_ascii(data, sz); ret = gather_convert_stats_ascii(data, sz);
free(data); free(data);
return ret; return ret;
@ -217,13 +219,13 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
} }
} }
static int has_cr_in_index(const char *path) static int has_cr_in_index(const struct index_state *istate, const char *path)
{ {
unsigned long sz; unsigned long sz;
void *data; void *data;
int has_cr; int has_cr;
data = read_blob_data_from_cache(path, &sz); data = read_blob_data_from_index(istate, path, &sz);
if (!data) if (!data)
return 0; return 0;
has_cr = memchr(data, '\r', sz) != NULL; has_cr = memchr(data, '\r', sz) != NULL;
@ -253,7 +255,8 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
} }
static int crlf_to_git(const char *path, const char *src, size_t len, static int crlf_to_git(const struct index_state *istate,
const char *path, const char *src, size_t len,
struct strbuf *buf, struct strbuf *buf,
enum crlf_action crlf_action, enum safe_crlf checksafe) enum crlf_action crlf_action, enum safe_crlf checksafe)
{ {
@ -285,7 +288,8 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
* unless we want to renormalize in a merge or * unless we want to renormalize in a merge or
* cherry-pick. * cherry-pick.
*/ */
if ((checksafe != SAFE_CRLF_RENORMALIZE) && has_cr_in_index(path)) if ((checksafe != SAFE_CRLF_RENORMALIZE) &&
has_cr_in_index(istate, path))
convert_crlf_into_lf = 0; convert_crlf_into_lf = 0;
} }
if ((checksafe == SAFE_CRLF_WARN || if ((checksafe == SAFE_CRLF_WARN ||
@ -1081,7 +1085,8 @@ const char *get_convert_attr_ascii(const char *path)
return ""; return "";
} }
int convert_to_git(const char *path, const char *src, size_t len, int convert_to_git(const struct index_state *istate,
const char *path, const char *src, size_t len,
struct strbuf *dst, enum safe_crlf checksafe) struct strbuf *dst, enum safe_crlf checksafe)
{ {
int ret = 0; int ret = 0;
@ -1097,7 +1102,7 @@ int convert_to_git(const char *path, const char *src, size_t len,
src = dst->buf; src = dst->buf;
len = dst->len; len = dst->len;
} }
ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe); ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe);
if (ret && dst) { if (ret && dst) {
src = dst->buf; src = dst->buf;
len = dst->len; len = dst->len;
@ -1105,7 +1110,8 @@ int convert_to_git(const char *path, const char *src, size_t len,
return ret | ident_to_git(path, src, len, dst, ca.ident); return ret | ident_to_git(path, src, len, dst, ca.ident);
} }
void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst, void convert_to_git_filter_fd(const struct index_state *istate,
const char *path, int fd, struct strbuf *dst,
enum safe_crlf checksafe) enum safe_crlf checksafe)
{ {
struct conv_attrs ca; struct conv_attrs ca;
@ -1117,7 +1123,7 @@ void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN)) if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))
die("%s: clean filter '%s' failed", path, ca.drv->name); die("%s: clean filter '%s' failed", path, ca.drv->name);
crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe); crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
ident_to_git(path, dst->buf, dst->len, dst, ca.ident); ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
} }
@ -1160,14 +1166,15 @@ int convert_to_working_tree(const char *path, const char *src, size_t len, struc
return convert_to_working_tree_internal(path, src, len, dst, 0); return convert_to_working_tree_internal(path, src, len, dst, 0);
} }
int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst) int renormalize_buffer(const struct index_state *istate, const char *path,
const char *src, size_t len, struct strbuf *dst)
{ {
int ret = convert_to_working_tree_internal(path, src, len, dst, 1); int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
if (ret) { if (ret) {
src = dst->buf; src = dst->buf;
len = dst->len; len = dst->len;
} }
return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_RENORMALIZE); return ret | convert_to_git(istate, path, src, len, dst, SAFE_CRLF_RENORMALIZE);
} }
/***************************************************************** /*****************************************************************

Просмотреть файл

@ -4,6 +4,8 @@
#ifndef CONVERT_H #ifndef CONVERT_H
#define CONVERT_H #define CONVERT_H
struct index_state;
enum safe_crlf { enum safe_crlf {
SAFE_CRLF_FALSE = 0, SAFE_CRLF_FALSE = 0,
SAFE_CRLF_FAIL = 1, SAFE_CRLF_FAIL = 1,
@ -33,23 +35,28 @@ enum eol {
}; };
extern enum eol core_eol; extern enum eol core_eol;
extern const char *get_cached_convert_stats_ascii(const char *path); extern const char *get_cached_convert_stats_ascii(const struct index_state *istate,
const char *path);
extern const char *get_wt_convert_stats_ascii(const char *path); extern const char *get_wt_convert_stats_ascii(const char *path);
extern const char *get_convert_attr_ascii(const char *path); extern const char *get_convert_attr_ascii(const char *path);
/* returns 1 if *dst was used */ /* returns 1 if *dst was used */
extern int convert_to_git(const char *path, const char *src, size_t len, extern int convert_to_git(const struct index_state *istate,
const char *path, const char *src, size_t len,
struct strbuf *dst, enum safe_crlf checksafe); struct strbuf *dst, enum safe_crlf checksafe);
extern int convert_to_working_tree(const char *path, const char *src, extern int convert_to_working_tree(const char *path, const char *src,
size_t len, struct strbuf *dst); size_t len, struct strbuf *dst);
extern int renormalize_buffer(const char *path, const char *src, size_t len, extern int renormalize_buffer(const struct index_state *istate,
const char *path, const char *src, size_t len,
struct strbuf *dst); struct strbuf *dst);
static inline int would_convert_to_git(const char *path) static inline int would_convert_to_git(const struct index_state *istate,
const char *path)
{ {
return convert_to_git(path, NULL, 0, NULL, 0); return convert_to_git(istate, path, NULL, 0, NULL, 0);
} }
/* Precondition: would_convert_to_git_filter_fd(path) == true */ /* Precondition: would_convert_to_git_filter_fd(path) == true */
extern void convert_to_git_filter_fd(const char *path, int fd, extern void convert_to_git_filter_fd(const struct index_state *istate,
const char *path, int fd,
struct strbuf *dst, struct strbuf *dst,
enum safe_crlf checksafe); enum safe_crlf checksafe);
extern int would_convert_to_git_filter_fd(const char *path); extern int would_convert_to_git_filter_fd(const char *path);

6
diff.c
Просмотреть файл

@ -2755,7 +2755,7 @@ static int reuse_worktree_file(const char *name, const struct object_id *oid, in
* Similarly, if we'd have to convert the file contents anyway, that * Similarly, if we'd have to convert the file contents anyway, that
* makes the optimization not worthwhile. * makes the optimization not worthwhile.
*/ */
if (!want_file && would_convert_to_git(name)) if (!want_file && would_convert_to_git(&the_index, name))
return 0; return 0;
len = strlen(name); len = strlen(name);
@ -2877,7 +2877,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
* point if the path requires us to run the content * point if the path requires us to run the content
* conversion. * conversion.
*/ */
if (size_only && !would_convert_to_git(s->path)) if (size_only && !would_convert_to_git(&the_index, s->path))
return 0; return 0;
/* /*
@ -2904,7 +2904,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
/* /*
* Convert from working tree format to canonical git format * Convert from working tree format to canonical git format
*/ */
if (convert_to_git(s->path, s->data, s->size, &buf, crlf_warn)) { if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, crlf_warn)) {
size_t size = 0; size_t size = 0;
munmap(s->data, s->size); munmap(s->data, s->size);
s->should_munmap = 0; s->should_munmap = 0;

2
dir.c
Просмотреть файл

@ -804,7 +804,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,
(pos = index_name_pos(istate, fname, strlen(fname))) >= 0 && (pos = index_name_pos(istate, fname, strlen(fname))) >= 0 &&
!ce_stage(istate->cache[pos]) && !ce_stage(istate->cache[pos]) &&
ce_uptodate(istate->cache[pos]) && ce_uptodate(istate->cache[pos]) &&
!would_convert_to_git(fname)) !would_convert_to_git(istate, fname))
hashcpy(sha1_stat->sha1, hashcpy(sha1_stat->sha1,
istate->cache[pos]->oid.hash); istate->cache[pos]->oid.hash);
else else

Просмотреть файл

@ -339,7 +339,7 @@ static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr
static void normalize_file(mmfile_t *mm, const char *path) static void normalize_file(mmfile_t *mm, const char *path)
{ {
struct strbuf strbuf = STRBUF_INIT; struct strbuf strbuf = STRBUF_INIT;
if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) { if (renormalize_buffer(&the_index, path, mm->ptr, mm->size, &strbuf)) {
free(mm->ptr); free(mm->ptr);
mm->size = strbuf.len; mm->size = strbuf.len;
mm->ptr = strbuf_detach(&strbuf, NULL); mm->ptr = strbuf_detach(&strbuf, NULL);

Просмотреть файл

@ -1639,8 +1639,8 @@ static int blob_unchanged(struct merge_options *opt,
* performed. Comparison can be skipped if both files are * performed. Comparison can be skipped if both files are
* unchanged since their sha1s have already been compared. * unchanged since their sha1s have already been compared.
*/ */
if (renormalize_buffer(path, o.buf, o.len, &o) | if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) |
renormalize_buffer(path, a.buf, a.len, &a)) renormalize_buffer(&the_index, path, a.buf, a.len, &a))
ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len)); ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
error_return: error_return:

Просмотреть файл

@ -3546,7 +3546,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
*/ */
if ((type == OBJ_BLOB) && path) { if ((type == OBJ_BLOB) && path) {
struct strbuf nbuf = STRBUF_INIT; struct strbuf nbuf = STRBUF_INIT;
if (convert_to_git(path, buf, size, &nbuf, if (convert_to_git(&the_index, path, buf, size, &nbuf,
write_object ? safe_crlf : SAFE_CRLF_FALSE)) { write_object ? safe_crlf : SAFE_CRLF_FALSE)) {
buf = strbuf_detach(&nbuf, &size); buf = strbuf_detach(&nbuf, &size);
re_allocated = 1; re_allocated = 1;
@ -3580,7 +3580,7 @@ static int index_stream_convert_blob(unsigned char *sha1, int fd,
assert(path); assert(path);
assert(would_convert_to_git_filter_fd(path)); assert(would_convert_to_git_filter_fd(path));
convert_to_git_filter_fd(path, fd, &sbuf, convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
write_object ? safe_crlf : SAFE_CRLF_FALSE); write_object ? safe_crlf : SAFE_CRLF_FALSE);
if (write_object) if (write_object)
@ -3668,7 +3668,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st,
else if (!S_ISREG(st->st_mode)) else if (!S_ISREG(st->st_mode))
ret = index_pipe(sha1, fd, type, path, flags); ret = index_pipe(sha1, fd, type, path, flags);
else if (st->st_size <= big_file_threshold || type != OBJ_BLOB || else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
(path && would_convert_to_git(path))) (path && would_convert_to_git(&the_index, path)))
ret = index_core(sha1, fd, xsize_t(st->st_size), type, path, ret = index_core(sha1, fd, xsize_t(st->st_size), type, path,
flags); flags);
else else

28
tree.c
Просмотреть файл

@ -1,3 +1,4 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h" #include "cache.h"
#include "cache-tree.h" #include "cache-tree.h"
#include "tree.h" #include "tree.h"
@ -8,7 +9,11 @@
const char *tree_type = "tree"; const char *tree_type = "tree";
static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt) static int read_one_entry_opt(struct index_state *istate,
const unsigned char *sha1,
const char *base, int baselen,
const char *pathname,
unsigned mode, int stage, int opt)
{ {
int len; int len;
unsigned int size; unsigned int size;
@ -27,14 +32,15 @@ static int read_one_entry_opt(const unsigned char *sha1, const char *base, int b
memcpy(ce->name, base, baselen); memcpy(ce->name, base, baselen);
memcpy(ce->name + baselen, pathname, len+1); memcpy(ce->name + baselen, pathname, len+1);
hashcpy(ce->oid.hash, sha1); hashcpy(ce->oid.hash, sha1);
return add_cache_entry(ce, opt); return add_index_entry(istate, ce, opt);
} }
static int read_one_entry(const unsigned char *sha1, struct strbuf *base, static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
const char *pathname, unsigned mode, int stage, const char *pathname, unsigned mode, int stage,
void *context) void *context)
{ {
return read_one_entry_opt(sha1, base->buf, base->len, pathname, struct index_state *istate = context;
return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname,
mode, stage, mode, stage,
ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
} }
@ -47,7 +53,8 @@ static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
const char *pathname, unsigned mode, int stage, const char *pathname, unsigned mode, int stage,
void *context) void *context)
{ {
return read_one_entry_opt(sha1, base->buf, base->len, pathname, struct index_state *istate = context;
return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname,
mode, stage, mode, stage,
ADD_CACHE_JUST_APPEND); ADD_CACHE_JUST_APPEND);
} }
@ -144,7 +151,8 @@ static int cmp_cache_name_compare(const void *a_, const void *b_)
ce2->name, ce2->ce_namelen, ce_stage(ce2)); ce2->name, ce2->ce_namelen, ce_stage(ce2));
} }
int read_tree(struct tree *tree, int stage, struct pathspec *match) int read_tree(struct tree *tree, int stage, struct pathspec *match,
struct index_state *istate)
{ {
read_tree_fn_t fn = NULL; read_tree_fn_t fn = NULL;
int i, err; int i, err;
@ -164,23 +172,23 @@ int read_tree(struct tree *tree, int stage, struct pathspec *match)
* do it the original slow way, otherwise, append and then * do it the original slow way, otherwise, append and then
* sort at the end. * sort at the end.
*/ */
for (i = 0; !fn && i < active_nr; i++) { for (i = 0; !fn && i < istate->cache_nr; i++) {
const struct cache_entry *ce = active_cache[i]; const struct cache_entry *ce = istate->cache[i];
if (ce_stage(ce) == stage) if (ce_stage(ce) == stage)
fn = read_one_entry; fn = read_one_entry;
} }
if (!fn) if (!fn)
fn = read_one_entry_quick; fn = read_one_entry_quick;
err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL); err = read_tree_recursive(tree, "", 0, stage, match, fn, istate);
if (fn == read_one_entry || err) if (fn == read_one_entry || err)
return err; return err;
/* /*
* Sort the cache entry -- we need to nuke the cache tree, though. * Sort the cache entry -- we need to nuke the cache tree, though.
*/ */
cache_tree_free(&active_cache_tree); cache_tree_free(&istate->cache_tree);
QSORT(active_cache, active_nr, cmp_cache_name_compare); QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
return 0; return 0;
} }

3
tree.h
Просмотреть файл

@ -34,6 +34,7 @@ extern int read_tree_recursive(struct tree *tree,
int stage, const struct pathspec *pathspec, int stage, const struct pathspec *pathspec,
read_tree_fn_t fn, void *context); read_tree_fn_t fn, void *context);
extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec); extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec,
struct index_state *istate);
#endif /* TREE_H */ #endif /* TREE_H */