refs.c: move dwim_ref()/dwim_log() from sha1_name.c

Both dwim_ref()/dwim_log() functions are intimately related to the ref parsing rules defined in refs.c and better fits there. Move them together with substitute_branch_name(), a file scope static helper function. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 12, 2011 at 10:35 UTC ff74f7f1187cbf1b46e85ed1bde1f194991f919c
2 files changed +85 -85
refs.c
+85
@@ -1067,6 +1067,91 @@ static int is_refname_available(const char *ref, const char *oldref,
1067 return 1;
1068 }
1069
1070 +/*
1071 + * *string and *len will only be substituted, and *string returned (for
1072 + * later free()ing) if the string passed in is a magic short-hand form
1073 + * to name a branch.
1074 + */
1075 +static char *substitute_branch_name(const char **string, int *len)
1076 +{
1077 + struct strbuf buf = STRBUF_INIT;
1078 + int ret = interpret_branch_name(*string, &buf);
1079 +
1080 + if (ret == *len) {
1081 + size_t size;
1082 + *string = strbuf_detach(&buf, &size);
1083 + *len = size;
1084 + return (char *)*string;
1085 + }
1086 +
1087 + return NULL;
1088 +}
1089 +
1090 +int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
1091 +{
1092 + char *last_branch = substitute_branch_name(&str, &len);
1093 + const char **p, *r;
1094 + int refs_found = 0;
1095 +
1096 + *ref = NULL;
1097 + for (p = ref_rev_parse_rules; *p; p++) {
1098 + char fullref[PATH_MAX];
1099 + unsigned char sha1_from_ref[20];
1100 + unsigned char *this_result;
1101 + int flag;
1102 +
1103 + this_result = refs_found ? sha1_from_ref : sha1;
1104 + mksnpath(fullref, sizeof(fullref), *p, len, str);
1105 + r = resolve_ref(fullref, this_result, 1, &flag);
1106 + if (r) {
1107 + if (!refs_found++)
1108 + *ref = xstrdup(r);
1109 + if (!warn_ambiguous_refs)
1110 + break;
1111 + } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD"))
1112 + warning("ignoring dangling symref %s.", fullref);
1113 + }
1114 + free(last_branch);
1115 + return refs_found;
1116 +}
1117 +
1118 +int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
1119 +{
1120 + char *last_branch = substitute_branch_name(&str, &len);
1121 + const char **p;
1122 + int logs_found = 0;
1123 +
1124 + *log = NULL;
1125 + for (p = ref_rev_parse_rules; *p; p++) {
1126 + struct stat st;
1127 + unsigned char hash[20];
1128 + char path[PATH_MAX];
1129 + const char *ref, *it;
1130 +
1131 + mksnpath(path, sizeof(path), *p, len, str);
1132 + ref = resolve_ref(path, hash, 1, NULL);
1133 + if (!ref)
1134 + continue;
1135 + if (!stat(git_path("logs/%s", path), &st) &&
1136 + S_ISREG(st.st_mode))
1137 + it = path;
1138 + else if (strcmp(ref, path) &&
1139 + !stat(git_path("logs/%s", ref), &st) &&
1140 + S_ISREG(st.st_mode))
1141 + it = ref;
1142 + else
1143 + continue;
1144 + if (!logs_found++) {
1145 + *log = xstrdup(it);
1146 + hashcpy(sha1, hash);
1147 + }
1148 + if (!warn_ambiguous_refs)
1149 + break;
1150 + }
1151 + free(last_branch);
1152 + return logs_found;
1153 +}
1154 +
1155 static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int flags, int *type_p)
1156 {
1157 char *ref_file;
sha1_name.c
-85
@@ -241,91 +241,6 @@ static int ambiguous_path(const char *path, int len)
241 return slash;
242 }
243
244 -/*
245 - * *string and *len will only be substituted, and *string returned (for
246 - * later free()ing) if the string passed in is a magic short-hand form
247 - * to name a branch.
248 - */
249 -static char *substitute_branch_name(const char **string, int *len)
250 -{
251 - struct strbuf buf = STRBUF_INIT;
252 - int ret = interpret_branch_name(*string, &buf);
253 -
254 - if (ret == *len) {
255 - size_t size;
256 - *string = strbuf_detach(&buf, &size);
257 - *len = size;
258 - return (char *)*string;
259 - }
260 -
261 - return NULL;
262 -}
263 -
264 -int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
265 -{
266 - char *last_branch = substitute_branch_name(&str, &len);
267 - const char **p, *r;
268 - int refs_found = 0;
269 -
270 - *ref = NULL;
271 - for (p = ref_rev_parse_rules; *p; p++) {
272 - char fullref[PATH_MAX];
273 - unsigned char sha1_from_ref[20];
274 - unsigned char *this_result;
275 - int flag;
276 -
277 - this_result = refs_found ? sha1_from_ref : sha1;
278 - mksnpath(fullref, sizeof(fullref), *p, len, str);
279 - r = resolve_ref(fullref, this_result, 1, &flag);
280 - if (r) {
281 - if (!refs_found++)
282 - *ref = xstrdup(r);
283 - if (!warn_ambiguous_refs)
284 - break;
285 - } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD"))
286 - warning("ignoring dangling symref %s.", fullref);
287 - }
288 - free(last_branch);
289 - return refs_found;
290 -}
291 -
292 -int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
293 -{
294 - char *last_branch = substitute_branch_name(&str, &len);
295 - const char **p;
296 - int logs_found = 0;
297 -
298 - *log = NULL;
299 - for (p = ref_rev_parse_rules; *p; p++) {
300 - struct stat st;
301 - unsigned char hash[20];
302 - char path[PATH_MAX];
303 - const char *ref, *it;
304 -
305 - mksnpath(path, sizeof(path), *p, len, str);
306 - ref = resolve_ref(path, hash, 1, NULL);
307 - if (!ref)
308 - continue;
309 - if (!stat(git_path("logs/%s", path), &st) &&
310 - S_ISREG(st.st_mode))
311 - it = path;
312 - else if (strcmp(ref, path) &&
313 - !stat(git_path("logs/%s", ref), &st) &&
314 - S_ISREG(st.st_mode))
315 - it = ref;
316 - else
317 - continue;
318 - if (!logs_found++) {
319 - *log = xstrdup(it);
320 - hashcpy(sha1, hash);
321 - }
322 - if (!warn_ambiguous_refs)
323 - break;
324 - }
325 - free(last_branch);
326 - return logs_found;
327 -}
328 -
244 static inline int upstream_mark(const char *string, int len)
245 {
246 const char *suffix[] = { "@{upstream}", "@{u}" };