dwim_ref(): Separate name-to-ref DWIM code out.
I'll be using this in another function to figure out what to pass to resolve_ref(). Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 19, 2007 at 01:15 UTC
e86eb6668ee0c574ba0b6c2c94564d4159a6fcc7
2 files changed
+26
-14
cache.h
+2
@@ -299,6 +299,8 @@ extern int get_sha1_hex(const char *hex, unsigned char *sha1);
299
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
300
extern int read_ref(const char *filename, unsigned char *sha1);
301
extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);
302
+extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
303
+
304
extern int create_symref(const char *ref, const char *refs_heads_master);
305
extern int validate_headref(const char *ref);
306
sha1_name.c
+24
-14
@@ -235,7 +235,7 @@ static int ambiguous_path(const char *path, int len)
235
return slash;
236
}
237
238
-static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
238
+int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
239
{
240
static const char *fmt[] = {
241
"%.*s",
@@ -246,13 +246,32 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
246
"refs/remotes/%.*s/HEAD",
247
NULL
248
};
249
+ const char **p, *r;
250
+ int refs_found = 0;
251
+
252
+ *ref = NULL;
253
+ for (p = fmt; *p; p++) {
254
+ unsigned char sha1_from_ref[20];
255
+ unsigned char *this_result;
256
+
257
+ this_result = refs_found ? sha1_from_ref : sha1;
258
+ r = resolve_ref(mkpath(*p, len, str), this_result, 1, NULL);
259
+ if (r) {
260
+ if (!refs_found++)
261
+ *ref = xstrdup(r);
262
+ if (!warn_ambiguous_refs)
263
+ break;
264
+ }
265
+ }
266
+ return refs_found;
267
+}
268
+
269
+static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
270
+{
271
static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
250
- const char **p, *ref;
272
char *real_ref = NULL;
273
int refs_found = 0;
274
int at, reflog_len;
254
- unsigned char *this_result;
255
- unsigned char sha1_from_ref[20];
275
276
if (len == 40 && !get_sha1_hex(str, sha1))
277
return 0;
@@ -273,16 +292,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
292
if (ambiguous_path(str, len))
293
return -1;
294
276
- for (p = fmt; *p; p++) {
277
- this_result = refs_found ? sha1_from_ref : sha1;
278
- ref = resolve_ref(mkpath(*p, len, str), this_result, 1, NULL);
279
- if (ref) {
280
- if (!refs_found++)
281
- real_ref = xstrdup(ref);
282
- if (!warn_ambiguous_refs)
283
- break;
284
- }
285
- }
295
+ refs_found = dwim_ref(str, len, sha1, &real_ref);
296
297
if (!refs_found)
298
return -1;