Skip to content

Commit 2f8acdb

Browse files
author
Junio C Hamano
committed
core.warnambiguousrefs: warns when "name" is used and both "name" branch and tag exists.
Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent ac74905 commit 2f8acdb

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ extern void rollback_index_file(struct cache_file *);
165165
extern int trust_executable_bit;
166166
extern int assume_unchanged;
167167
extern int only_use_symrefs;
168+
extern int warn_ambiguous_refs;
168169
extern int diff_rename_limit_default;
169170
extern int shared_repository;
170171
extern const char *apply_default_whitespace;

config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ int git_default_config(const char *var, const char *value)
232232
return 0;
233233
}
234234

235+
if (!strcmp(var, "core.warnambiguousrefs")) {
236+
warn_ambiguous_refs = git_config_bool(var, value);
237+
return 0;
238+
}
239+
235240
if (!strcmp(var, "user.name")) {
236241
strncpy(git_default_name, value, sizeof(git_default_name));
237242
return 0;

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ char git_default_name[MAX_GITNAME];
1414
int trust_executable_bit = 1;
1515
int assume_unchanged = 0;
1616
int only_use_symrefs = 0;
17+
int warn_ambiguous_refs = 0;
1718
int repository_format_version = 0;
1819
char git_commit_encoding[MAX_ENCODING_LENGTH] = "utf-8";
1920
int shared_repository = 0;

sha1_name.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,13 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
240240
"refs",
241241
"refs/tags",
242242
"refs/heads",
243+
"refs/remotes",
243244
NULL
244245
};
245246
const char **p;
247+
const char *warning = "warning: refname '%.*s' is ambiguous.\n";
248+
char *pathname;
249+
int already_found = 0;
246250

247251
if (len == 40 && !get_sha1_hex(str, sha1))
248252
return 0;
@@ -252,10 +256,23 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
252256
return -1;
253257

254258
for (p = prefix; *p; p++) {
255-
char *pathname = git_path("%s/%.*s", *p, len, str);
256-
if (!read_ref(pathname, sha1))
257-
return 0;
259+
unsigned char sha1_from_ref[20];
260+
unsigned char *this_result =
261+
already_found ? sha1_from_ref : sha1;
262+
pathname = git_path("%s/%.*s", *p, len, str);
263+
if (!read_ref(pathname, this_result)) {
264+
if (warn_ambiguous_refs) {
265+
if (already_found &&
266+
!memcmp(sha1, sha1_from_ref, 20))
267+
fprintf(stderr, warning, len, str);
268+
already_found++;
269+
}
270+
else
271+
return 0;
272+
}
258273
}
274+
if (already_found)
275+
return 0;
259276
return -1;
260277
}
261278

0 commit comments

Comments
 (0)