|
14 | 14 |
|
15 | 15 | #include "git2.h" |
16 | 16 |
|
17 | | -static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname) |
18 | | -{ |
19 | | - int error = 0, i; |
20 | | - bool fallbackmode = true, foundvalid = false; |
21 | | - git_reference *ref; |
22 | | - git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT; |
23 | | - |
24 | | - static const char* formatters[] = { |
25 | | - "%s", |
26 | | - GIT_REFS_DIR "%s", |
27 | | - GIT_REFS_TAGS_DIR "%s", |
28 | | - GIT_REFS_HEADS_DIR "%s", |
29 | | - GIT_REFS_REMOTES_DIR "%s", |
30 | | - GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE, |
31 | | - NULL |
32 | | - }; |
33 | | - |
34 | | - if (*refname) |
35 | | - git_buf_puts(&name, refname); |
36 | | - else { |
37 | | - git_buf_puts(&name, GIT_HEAD_FILE); |
38 | | - fallbackmode = false; |
39 | | - } |
40 | | - |
41 | | - for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) { |
42 | | - |
43 | | - git_buf_clear(&refnamebuf); |
44 | | - |
45 | | - if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0) |
46 | | - goto cleanup; |
47 | | - |
48 | | - if (!git_reference_is_valid_name(git_buf_cstr(&refnamebuf))) { |
49 | | - error = GIT_EINVALIDSPEC; |
50 | | - continue; |
51 | | - } |
52 | | - foundvalid = true; |
53 | | - |
54 | | - error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1); |
55 | | - |
56 | | - if (!error) { |
57 | | - *out = ref; |
58 | | - error = 0; |
59 | | - goto cleanup; |
60 | | - } |
61 | | - |
62 | | - if (error != GIT_ENOTFOUND) |
63 | | - goto cleanup; |
64 | | - } |
65 | | - |
66 | | -cleanup: |
67 | | - if (error && !foundvalid) { |
68 | | - /* never found a valid reference name */ |
69 | | - giterr_set(GITERR_REFERENCE, |
70 | | - "Could not use '%s' as valid reference name", git_buf_cstr(&name)); |
71 | | - } |
72 | | - |
73 | | - git_buf_free(&name); |
74 | | - git_buf_free(&refnamebuf); |
75 | | - return error; |
76 | | -} |
77 | | - |
78 | 17 | static int maybe_sha_or_abbrev(git_object** out, git_repository *repo, const char *spec, size_t speclen) |
79 | 18 | { |
80 | 19 | git_oid oid; |
@@ -157,7 +96,7 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const |
157 | 96 | if (error < 0 && error != GIT_ENOTFOUND) |
158 | 97 | return error; |
159 | 98 |
|
160 | | - error = disambiguate_refname(&ref, repo, spec); |
| 99 | + error = git_reference_dwim(&ref, repo, spec); |
161 | 100 | if (!error) { |
162 | 101 | error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJ_ANY); |
163 | 102 | git_reference_free(ref); |
@@ -242,7 +181,7 @@ static int retrieve_previously_checked_out_branch_or_revision(git_object **out, |
242 | 181 |
|
243 | 182 | git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so); |
244 | 183 |
|
245 | | - if ((error = disambiguate_refname(base_ref, repo, git_buf_cstr(&buf))) == 0) |
| 184 | + if ((error = git_reference_dwim(base_ref, repo, git_buf_cstr(&buf))) == 0) |
246 | 185 | goto cleanup; |
247 | 186 |
|
248 | 187 | if (error < 0 && error != GIT_ENOTFOUND) |
@@ -323,7 +262,7 @@ static int retrieve_revobject_from_reflog(git_object **out, git_reference **base |
323 | 262 | int error = -1; |
324 | 263 |
|
325 | 264 | if (*base_ref == NULL) { |
326 | | - if ((error = disambiguate_refname(&ref, repo, identifier)) < 0) |
| 265 | + if ((error = git_reference_dwim(&ref, repo, identifier)) < 0) |
327 | 266 | return error; |
328 | 267 | } else { |
329 | 268 | ref = *base_ref; |
@@ -351,7 +290,7 @@ static int retrieve_remote_tracking_reference(git_reference **base_ref, const ch |
351 | 290 | int error = -1; |
352 | 291 |
|
353 | 292 | if (*base_ref == NULL) { |
354 | | - if ((error = disambiguate_refname(&ref, repo, identifier)) < 0) |
| 293 | + if ((error = git_reference_dwim(&ref, repo, identifier)) < 0) |
355 | 294 | return error; |
356 | 295 | } else { |
357 | 296 | ref = *base_ref; |
|
0 commit comments