Merge branch 'jk/no-looking-at-dotgit-outside-repo'

Update "git diff --no-index" codepath not to try to peek into .git/
directory that happens to be under the current directory, when we
know we are operating outside any repository.

* jk/no-looking-at-dotgit-outside-repo:
  diff: handle sha1 abbreviations outside of repository
  diff_aligned_abbrev: use "struct oid"
  diff_unique_abbrev: rename to diff_aligned_abbrev
  find_unique_abbrev: use 4-buffer ring
  test-*-cache-tree: setup git dir
  read info/{attributes,exclude} only when in repository
maint
Junio C Hamano 2016-10-27 14:58:48 -07:00
commit 0d9c527d59
11 changed files with 66 additions and 46 deletions

4
attr.c
View File

@ -531,7 +531,11 @@ static void bootstrap_attr_stack(void)
debug_push(elem); debug_push(elem);
} }


if (startup_info->have_repository)
elem = read_attr_from_file(git_path_info_attributes(), 1); elem = read_attr_from_file(git_path_info_attributes(), 1);
else
elem = NULL;

if (!elem) if (!elem)
elem = xcalloc(1, sizeof(*elem)); elem = xcalloc(1, sizeof(*elem));
elem->origin = NULL; elem->origin = NULL;

View File

@ -1374,12 +1374,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
struct commit *commit; struct commit *commit;


if (verbosity >= 0) { if (verbosity >= 0) {
char from[GIT_SHA1_HEXSZ + 1], to[GIT_SHA1_HEXSZ + 1]; printf(_("Updating %s..%s\n"),
find_unique_abbrev_r(from, head_commit->object.oid.hash, find_unique_abbrev(head_commit->object.oid.hash,
DEFAULT_ABBREV); DEFAULT_ABBREV),
find_unique_abbrev_r(to, remoteheads->item->object.oid.hash, find_unique_abbrev(remoteheads->item->object.oid.hash,
DEFAULT_ABBREV); DEFAULT_ABBREV));
printf(_("Updating %s..%s\n"), from, to);
} }
strbuf_addstr(&msg, "Fast-forward"); strbuf_addstr(&msg, "Fast-forward");
if (have_message) if (have_message)

View File

@ -1163,10 +1163,6 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
struct string_list_item *item; struct string_list_item *item;
struct command *dst_cmd; struct command *dst_cmd;
unsigned char sha1[GIT_SHA1_RAWSZ]; unsigned char sha1[GIT_SHA1_RAWSZ];
char cmd_oldh[GIT_SHA1_HEXSZ + 1],
cmd_newh[GIT_SHA1_HEXSZ + 1],
dst_oldh[GIT_SHA1_HEXSZ + 1],
dst_newh[GIT_SHA1_HEXSZ + 1];
int flag; int flag;


strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name); strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
@ -1197,14 +1193,14 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)


dst_cmd->skip_update = 1; dst_cmd->skip_update = 1;


find_unique_abbrev_r(cmd_oldh, cmd->old_sha1, DEFAULT_ABBREV);
find_unique_abbrev_r(cmd_newh, cmd->new_sha1, DEFAULT_ABBREV);
find_unique_abbrev_r(dst_oldh, dst_cmd->old_sha1, DEFAULT_ABBREV);
find_unique_abbrev_r(dst_newh, dst_cmd->new_sha1, DEFAULT_ABBREV);
rp_error("refusing inconsistent update between symref '%s' (%s..%s) and" rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
" its target '%s' (%s..%s)", " its target '%s' (%s..%s)",
cmd->ref_name, cmd_oldh, cmd_newh, cmd->ref_name,
dst_cmd->ref_name, dst_oldh, dst_newh); find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV),
find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV),
dst_cmd->ref_name,
find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV),
find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));


cmd->error_string = dst_cmd->error_string = cmd->error_string = dst_cmd->error_string =
"inconsistent aliased update"; "inconsistent aliased update";

View File

@ -903,8 +903,8 @@ extern char *sha1_pack_index_name(const unsigned char *sha1);
* The result will be at least `len` characters long, and will be NUL * The result will be at least `len` characters long, and will be NUL
* terminated. * terminated.
* *
* The non-`_r` version returns a static buffer which will be overwritten by * The non-`_r` version returns a static buffer which remains valid until 4
* subsequent calls. * more calls to find_unique_abbrev are made.
* *
* The `_r` variant writes to a buffer supplied by the caller, which must be at * The `_r` variant writes to a buffer supplied by the caller, which must be at
* least `GIT_SHA1_HEXSZ + 1` bytes. The return value is the number of bytes * least `GIT_SHA1_HEXSZ + 1` bytes. The return value is the number of bytes

View File

@ -1203,9 +1203,9 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re


/* Show sha1's */ /* Show sha1's */
for (i = 0; i < num_parent; i++) for (i = 0; i < num_parent; i++)
printf(" %s", diff_unique_abbrev(p->parent[i].oid.hash, printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
opt->abbrev)); opt->abbrev));
printf(" %s ", diff_unique_abbrev(p->oid.hash, opt->abbrev)); printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
} }


if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) { if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {

45
diff.c
View File

@ -3096,6 +3096,21 @@ static int similarity_index(struct diff_filepair *p)
return p->score * 100 / MAX_SCORE; return p->score * 100 / MAX_SCORE;
} }


static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
{
if (startup_info->have_repository)
return find_unique_abbrev(oid->hash, abbrev);
else {
char *hex = oid_to_hex(oid);
if (abbrev < 0)
abbrev = FALLBACK_DEFAULT_ABBREV;
if (abbrev > GIT_SHA1_HEXSZ)
die("BUG: oid abbreviation out of range: %d", abbrev);
hex[abbrev] = '\0';
return hex;
}
}

static void fill_metainfo(struct strbuf *msg, static void fill_metainfo(struct strbuf *msg,
const char *name, const char *name,
const char *other, const char *other,
@ -3154,9 +3169,9 @@ static void fill_metainfo(struct strbuf *msg,
(!fill_mmfile(&mf, two) && diff_filespec_is_binary(two))) (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
abbrev = 40; abbrev = 40;
} }
strbuf_addf(msg, "%s%sindex %s..", line_prefix, set, strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
find_unique_abbrev(one->oid.hash, abbrev)); diff_abbrev_oid(&one->oid, abbrev),
strbuf_add_unique_abbrev(msg, two->oid.hash, abbrev); diff_abbrev_oid(&two->oid, abbrev));
if (one->mode == two->mode) if (one->mode == two->mode)
strbuf_addf(msg, " %06o", one->mode); strbuf_addf(msg, " %06o", one->mode);
strbuf_addf(msg, "%s\n", reset); strbuf_addf(msg, "%s\n", reset);
@ -4157,18 +4172,15 @@ void diff_free_filepair(struct diff_filepair *p)
free(p); free(p);
} }


/* const char *diff_aligned_abbrev(const struct object_id *oid, int len)
* This is different from find_unique_abbrev() in that
* it stuffs the result with dots for alignment.
*/
const char *diff_unique_abbrev(const unsigned char *sha1, int len)
{ {
int abblen; int abblen;
const char *abbrev; const char *abbrev;
if (len == 40)
return sha1_to_hex(sha1);


abbrev = find_unique_abbrev(sha1, len); if (len == GIT_SHA1_HEXSZ)
return oid_to_hex(oid);

abbrev = diff_abbrev_oid(oid, len);
abblen = strlen(abbrev); abblen = strlen(abbrev);


/* /*
@ -4190,15 +4202,16 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)
* the automatic sizing is supposed to give abblen that ensures * the automatic sizing is supposed to give abblen that ensures
* uniqueness across all objects (statistically speaking). * uniqueness across all objects (statistically speaking).
*/ */
if (abblen < 37) { if (abblen < GIT_SHA1_HEXSZ - 3) {
static char hex[41]; static char hex[GIT_SHA1_HEXSZ + 1];
if (len < abblen && abblen <= len + 2) if (len < abblen && abblen <= len + 2)
xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, ".."); xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
else else
xsnprintf(hex, sizeof(hex), "%s...", abbrev); xsnprintf(hex, sizeof(hex), "%s...", abbrev);
return hex; return hex;
} }
return sha1_to_hex(sha1);
return oid_to_hex(oid);
} }


static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt) static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
@ -4209,9 +4222,9 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
fprintf(opt->file, "%s", diff_line_prefix(opt)); fprintf(opt->file, "%s", diff_line_prefix(opt));
if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) { if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode, fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
diff_unique_abbrev(p->one->oid.hash, opt->abbrev)); diff_aligned_abbrev(&p->one->oid, opt->abbrev));
fprintf(opt->file, "%s ", fprintf(opt->file, "%s ",
diff_unique_abbrev(p->two->oid.hash, opt->abbrev)); diff_aligned_abbrev(&p->two->oid, opt->abbrev));
} }
if (p->score) { if (p->score) {
fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p), fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),

6
diff.h
View File

@ -340,7 +340,11 @@ extern void diff_warn_rename_limit(const char *varname, int needed, int degraded
#define DIFF_STATUS_FILTER_AON '*' #define DIFF_STATUS_FILTER_AON '*'
#define DIFF_STATUS_FILTER_BROKEN 'B' #define DIFF_STATUS_FILTER_BROKEN 'B'


extern const char *diff_unique_abbrev(const unsigned char *, int); /*
* This is different from find_unique_abbrev() in that
* it stuffs the result with dots for alignment.
*/
extern const char *diff_aligned_abbrev(const struct object_id *sha1, int);


/* do not report anything on removed paths */ /* do not report anything on removed paths */
#define DIFF_SILENT_ON_REMOVED 01 #define DIFF_SILENT_ON_REMOVED 01

6
dir.c
View File

@ -2237,8 +2237,6 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")


void setup_standard_excludes(struct dir_struct *dir) void setup_standard_excludes(struct dir_struct *dir)
{ {
const char *path;

dir->exclude_per_dir = ".gitignore"; dir->exclude_per_dir = ".gitignore";


/* core.excludefile defaulting to $XDG_HOME/git/ignore */ /* core.excludefile defaulting to $XDG_HOME/git/ignore */
@ -2249,11 +2247,13 @@ void setup_standard_excludes(struct dir_struct *dir)
dir->untracked ? &dir->ss_excludes_file : NULL); dir->untracked ? &dir->ss_excludes_file : NULL);


/* per repository user preference */ /* per repository user preference */
path = git_path_info_exclude(); if (startup_info->have_repository) {
const char *path = git_path_info_exclude();
if (!access_or_warn(path, R_OK, 0)) if (!access_or_warn(path, R_OK, 0))
add_excludes_from_file_1(dir, path, add_excludes_from_file_1(dir, path,
dir->untracked ? &dir->ss_info_exclude : NULL); dir->untracked ? &dir->ss_info_exclude : NULL);
} }
}


int remove_path(const char *name) int remove_path(const char *name)
{ {

View File

@ -508,7 +508,9 @@ int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)


const char *find_unique_abbrev(const unsigned char *sha1, int len) const char *find_unique_abbrev(const unsigned char *sha1, int len)
{ {
static char hex[GIT_SHA1_HEXSZ + 1]; static int bufno;
static char hexbuffer[4][GIT_SHA1_HEXSZ + 1];
char *hex = hexbuffer[3 & ++bufno];
find_unique_abbrev_r(hex, sha1, len); find_unique_abbrev_r(hex, sha1, len);
return hex; return hex;
} }

View File

@ -58,6 +58,7 @@ int cmd_main(int ac, const char **av)
{ {
struct index_state istate; struct index_state istate;
struct cache_tree *another = cache_tree(); struct cache_tree *another = cache_tree();
setup_git_directory();
if (read_cache() < 0) if (read_cache() < 0)
die("unable to read index file"); die("unable to read index file");
istate = the_index; istate = the_index;

View File

@ -7,6 +7,7 @@ static struct lock_file index_lock;


int cmd_main(int ac, const char **av) int cmd_main(int ac, const char **av)
{ {
setup_git_directory();
hold_locked_index(&index_lock, 1); hold_locked_index(&index_lock, 1);
if (read_cache() < 0) if (read_cache() < 0)
die("unable to read index file"); die("unable to read index file");