1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-02 05:56:11 +02:00

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
This commit is contained in:
Junio C Hamano 2016-10-27 14:58:48 -07:00
commit 0d9c527d59
11 changed files with 66 additions and 46 deletions

6
attr.c
View File

@ -531,7 +531,11 @@ static void bootstrap_attr_stack(void)
debug_push(elem);
}
elem = read_attr_from_file(git_path_info_attributes(), 1);
if (startup_info->have_repository)
elem = read_attr_from_file(git_path_info_attributes(), 1);
else
elem = NULL;
if (!elem)
elem = xcalloc(1, sizeof(*elem));
elem->origin = NULL;

View File

@ -1374,12 +1374,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
struct commit *commit;
if (verbosity >= 0) {
char from[GIT_SHA1_HEXSZ + 1], to[GIT_SHA1_HEXSZ + 1];
find_unique_abbrev_r(from, head_commit->object.oid.hash,
DEFAULT_ABBREV);
find_unique_abbrev_r(to, remoteheads->item->object.oid.hash,
DEFAULT_ABBREV);
printf(_("Updating %s..%s\n"), from, to);
printf(_("Updating %s..%s\n"),
find_unique_abbrev(head_commit->object.oid.hash,
DEFAULT_ABBREV),
find_unique_abbrev(remoteheads->item->object.oid.hash,
DEFAULT_ABBREV));
}
strbuf_addstr(&msg, "Fast-forward");
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 command *dst_cmd;
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;
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;
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"
" its target '%s' (%s..%s)",
cmd->ref_name, cmd_oldh, cmd_newh,
dst_cmd->ref_name, dst_oldh, dst_newh);
cmd->ref_name,
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 =
"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
* terminated.
*
* The non-`_r` version returns a static buffer which will be overwritten by
* subsequent calls.
* The non-`_r` version returns a static buffer which remains valid until 4
* more calls to find_unique_abbrev are made.
*
* 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

View File

@ -1203,9 +1203,9 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re
/* Show sha1's */
for (i = 0; i < num_parent; i++)
printf(" %s", diff_unique_abbrev(p->parent[i].oid.hash,
opt->abbrev));
printf(" %s ", diff_unique_abbrev(p->oid.hash, opt->abbrev));
printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
opt->abbrev));
printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
}
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;
}
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,
const char *name,
const char *other,
@ -3154,9 +3169,9 @@ static void fill_metainfo(struct strbuf *msg,
(!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
abbrev = 40;
}
strbuf_addf(msg, "%s%sindex %s..", line_prefix, set,
find_unique_abbrev(one->oid.hash, abbrev));
strbuf_add_unique_abbrev(msg, two->oid.hash, abbrev);
strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
diff_abbrev_oid(&one->oid, abbrev),
diff_abbrev_oid(&two->oid, abbrev));
if (one->mode == two->mode)
strbuf_addf(msg, " %06o", one->mode);
strbuf_addf(msg, "%s\n", reset);
@ -4157,18 +4172,15 @@ void diff_free_filepair(struct diff_filepair *p)
free(p);
}
/*
* 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)
const char *diff_aligned_abbrev(const struct object_id *oid, int len)
{
int abblen;
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);
/*
@ -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
* uniqueness across all objects (statistically speaking).
*/
if (abblen < 37) {
static char hex[41];
if (abblen < GIT_SHA1_HEXSZ - 3) {
static char hex[GIT_SHA1_HEXSZ + 1];
if (len < abblen && abblen <= len + 2)
xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
else
xsnprintf(hex, sizeof(hex), "%s...", abbrev);
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)
@ -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));
if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
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 ",
diff_unique_abbrev(p->two->oid.hash, opt->abbrev));
diff_aligned_abbrev(&p->two->oid, opt->abbrev));
}
if (p->score) {
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_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 */
#define DIFF_SILENT_ON_REMOVED 01

12
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)
{
const char *path;
dir->exclude_per_dir = ".gitignore";
/* core.excludefile defaulting to $XDG_HOME/git/ignore */
@ -2249,10 +2247,12 @@ void setup_standard_excludes(struct dir_struct *dir)
dir->untracked ? &dir->ss_excludes_file : NULL);
/* per repository user preference */
path = git_path_info_exclude();
if (!access_or_warn(path, R_OK, 0))
add_excludes_from_file_1(dir, path,
dir->untracked ? &dir->ss_info_exclude : NULL);
if (startup_info->have_repository) {
const char *path = git_path_info_exclude();
if (!access_or_warn(path, R_OK, 0))
add_excludes_from_file_1(dir, path,
dir->untracked ? &dir->ss_info_exclude : NULL);
}
}
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)
{
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);
return hex;
}

View File

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

View File

@ -7,6 +7,7 @@ static struct lock_file index_lock;
int cmd_main(int ac, const char **av)
{
setup_git_directory();
hold_locked_index(&index_lock, 1);
if (read_cache() < 0)
die("unable to read index file");