Skip to content

Commit 0d9c527

Browse files
committed
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
2 parents f9db0c0 + 4f03666 commit 0d9c527

File tree

11 files changed

+66
-46
lines changed

11 files changed

+66
-46
lines changed

attr.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,11 @@ static void bootstrap_attr_stack(void)
531531
debug_push(elem);
532532
}
533533

534-
elem = read_attr_from_file(git_path_info_attributes(), 1);
534+
if (startup_info->have_repository)
535+
elem = read_attr_from_file(git_path_info_attributes(), 1);
536+
else
537+
elem = NULL;
538+
535539
if (!elem)
536540
elem = xcalloc(1, sizeof(*elem));
537541
elem->origin = NULL;

builtin/merge.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,12 +1374,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13741374
struct commit *commit;
13751375

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

builtin/receive-pack.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,10 +1163,6 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
11631163
struct string_list_item *item;
11641164
struct command *dst_cmd;
11651165
unsigned char sha1[GIT_SHA1_RAWSZ];
1166-
char cmd_oldh[GIT_SHA1_HEXSZ + 1],
1167-
cmd_newh[GIT_SHA1_HEXSZ + 1],
1168-
dst_oldh[GIT_SHA1_HEXSZ + 1],
1169-
dst_newh[GIT_SHA1_HEXSZ + 1];
11701166
int flag;
11711167

11721168
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)
11971193

11981194
dst_cmd->skip_update = 1;
11991195

1200-
find_unique_abbrev_r(cmd_oldh, cmd->old_sha1, DEFAULT_ABBREV);
1201-
find_unique_abbrev_r(cmd_newh, cmd->new_sha1, DEFAULT_ABBREV);
1202-
find_unique_abbrev_r(dst_oldh, dst_cmd->old_sha1, DEFAULT_ABBREV);
1203-
find_unique_abbrev_r(dst_newh, dst_cmd->new_sha1, DEFAULT_ABBREV);
12041196
rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
12051197
" its target '%s' (%s..%s)",
1206-
cmd->ref_name, cmd_oldh, cmd_newh,
1207-
dst_cmd->ref_name, dst_oldh, dst_newh);
1198+
cmd->ref_name,
1199+
find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV),
1200+
find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV),
1201+
dst_cmd->ref_name,
1202+
find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV),
1203+
find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
12081204

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

cache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ extern char *sha1_pack_index_name(const unsigned char *sha1);
903903
* The result will be at least `len` characters long, and will be NUL
904904
* terminated.
905905
*
906-
* The non-`_r` version returns a static buffer which will be overwritten by
907-
* subsequent calls.
906+
* The non-`_r` version returns a static buffer which remains valid until 4
907+
* more calls to find_unique_abbrev are made.
908908
*
909909
* The `_r` variant writes to a buffer supplied by the caller, which must be at
910910
* least `GIT_SHA1_HEXSZ + 1` bytes. The return value is the number of bytes

combine-diff.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,9 +1203,9 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re
12031203

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

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

diff.c

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,6 +3096,21 @@ static int similarity_index(struct diff_filepair *p)
30963096
return p->score * 100 / MAX_SCORE;
30973097
}
30983098

3099+
static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
3100+
{
3101+
if (startup_info->have_repository)
3102+
return find_unique_abbrev(oid->hash, abbrev);
3103+
else {
3104+
char *hex = oid_to_hex(oid);
3105+
if (abbrev < 0)
3106+
abbrev = FALLBACK_DEFAULT_ABBREV;
3107+
if (abbrev > GIT_SHA1_HEXSZ)
3108+
die("BUG: oid abbreviation out of range: %d", abbrev);
3109+
hex[abbrev] = '\0';
3110+
return hex;
3111+
}
3112+
}
3113+
30993114
static void fill_metainfo(struct strbuf *msg,
31003115
const char *name,
31013116
const char *other,
@@ -3154,9 +3169,9 @@ static void fill_metainfo(struct strbuf *msg,
31543169
(!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
31553170
abbrev = 40;
31563171
}
3157-
strbuf_addf(msg, "%s%sindex %s..", line_prefix, set,
3158-
find_unique_abbrev(one->oid.hash, abbrev));
3159-
strbuf_add_unique_abbrev(msg, two->oid.hash, abbrev);
3172+
strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
3173+
diff_abbrev_oid(&one->oid, abbrev),
3174+
diff_abbrev_oid(&two->oid, abbrev));
31603175
if (one->mode == two->mode)
31613176
strbuf_addf(msg, " %06o", one->mode);
31623177
strbuf_addf(msg, "%s\n", reset);
@@ -4157,18 +4172,15 @@ void diff_free_filepair(struct diff_filepair *p)
41574172
free(p);
41584173
}
41594174

4160-
/*
4161-
* This is different from find_unique_abbrev() in that
4162-
* it stuffs the result with dots for alignment.
4163-
*/
4164-
const char *diff_unique_abbrev(const unsigned char *sha1, int len)
4175+
const char *diff_aligned_abbrev(const struct object_id *oid, int len)
41654176
{
41664177
int abblen;
41674178
const char *abbrev;
4168-
if (len == 40)
4169-
return sha1_to_hex(sha1);
41704179

4171-
abbrev = find_unique_abbrev(sha1, len);
4180+
if (len == GIT_SHA1_HEXSZ)
4181+
return oid_to_hex(oid);
4182+
4183+
abbrev = diff_abbrev_oid(oid, len);
41724184
abblen = strlen(abbrev);
41734185

41744186
/*
@@ -4190,15 +4202,16 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)
41904202
* the automatic sizing is supposed to give abblen that ensures
41914203
* uniqueness across all objects (statistically speaking).
41924204
*/
4193-
if (abblen < 37) {
4194-
static char hex[41];
4205+
if (abblen < GIT_SHA1_HEXSZ - 3) {
4206+
static char hex[GIT_SHA1_HEXSZ + 1];
41954207
if (len < abblen && abblen <= len + 2)
41964208
xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
41974209
else
41984210
xsnprintf(hex, sizeof(hex), "%s...", abbrev);
41994211
return hex;
42004212
}
4201-
return sha1_to_hex(sha1);
4213+
4214+
return oid_to_hex(oid);
42024215
}
42034216

42044217
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)
42094222
fprintf(opt->file, "%s", diff_line_prefix(opt));
42104223
if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
42114224
fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
4212-
diff_unique_abbrev(p->one->oid.hash, opt->abbrev));
4225+
diff_aligned_abbrev(&p->one->oid, opt->abbrev));
42134226
fprintf(opt->file, "%s ",
4214-
diff_unique_abbrev(p->two->oid.hash, opt->abbrev));
4227+
diff_aligned_abbrev(&p->two->oid, opt->abbrev));
42154228
}
42164229
if (p->score) {
42174230
fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),

diff.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,11 @@ extern void diff_warn_rename_limit(const char *varname, int needed, int degraded
340340
#define DIFF_STATUS_FILTER_AON '*'
341341
#define DIFF_STATUS_FILTER_BROKEN 'B'
342342

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

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

dir.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,8 +2237,6 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
22372237

22382238
void setup_standard_excludes(struct dir_struct *dir)
22392239
{
2240-
const char *path;
2241-
22422240
dir->exclude_per_dir = ".gitignore";
22432241

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

22512249
/* per repository user preference */
2252-
path = git_path_info_exclude();
2253-
if (!access_or_warn(path, R_OK, 0))
2254-
add_excludes_from_file_1(dir, path,
2255-
dir->untracked ? &dir->ss_info_exclude : NULL);
2250+
if (startup_info->have_repository) {
2251+
const char *path = git_path_info_exclude();
2252+
if (!access_or_warn(path, R_OK, 0))
2253+
add_excludes_from_file_1(dir, path,
2254+
dir->untracked ? &dir->ss_info_exclude : NULL);
2255+
}
22562256
}
22572257

22582258
int remove_path(const char *name)

sha1_name.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,9 @@ int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
508508

509509
const char *find_unique_abbrev(const unsigned char *sha1, int len)
510510
{
511-
static char hex[GIT_SHA1_HEXSZ + 1];
511+
static int bufno;
512+
static char hexbuffer[4][GIT_SHA1_HEXSZ + 1];
513+
char *hex = hexbuffer[3 & ++bufno];
512514
find_unique_abbrev_r(hex, sha1, len);
513515
return hex;
514516
}

t/helper/test-dump-cache-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int cmd_main(int ac, const char **av)
5858
{
5959
struct index_state istate;
6060
struct cache_tree *another = cache_tree();
61+
setup_git_directory();
6162
if (read_cache() < 0)
6263
die("unable to read index file");
6364
istate = the_index;

0 commit comments

Comments
 (0)