move match_object_id() into the library; supersedes got_repo_resolve_commit_arg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
diff --git a/got/got.c b/got/got.c
index 5afeb71..8557a12 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1107,8 +1107,8 @@ cmd_checkout(int argc, char *argv[])
if (commit_id_str) {
struct got_object_id *commit_id;
- error = got_repo_resolve_commit_arg(&commit_id,
- commit_id_str, repo);
+ error = got_repo_match_object_id(&commit_id, NULL,
+ commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
if (error)
goto done;
error = check_linear_ancestry(commit_id,
@@ -1347,8 +1347,8 @@ cmd_update(int argc, char *argv[])
if (error != NULL)
goto done;
} else {
- error = got_repo_resolve_commit_arg(&commit_id,
- commit_id_str, repo);
+ error = got_repo_match_object_id(&commit_id, NULL,
+ commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
free(commit_id_str);
commit_id_str = NULL;
if (error)
@@ -2190,66 +2190,6 @@ done:
}
static const struct got_error *
-match_object_id(struct got_object_id **id, char **label,
- const char *id_str, int obj_type, int resolve_tags,
- struct got_repository *repo)
-{
- const struct got_error *err;
- struct got_tag_object *tag;
- struct got_reference *ref = NULL;
-
- *id = NULL;
- *label = NULL;
-
- if (resolve_tags) {
- err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
- repo);
- if (err == NULL) {
- *id = got_object_id_dup(
- got_object_tag_get_object_id(tag));
- if (*id == NULL)
- err = got_error_from_errno("got_object_id_dup");
- else if (asprintf(label, "refs/tags/%s",
- got_object_tag_get_name(tag)) == -1) {
- err = got_error_from_errno("asprintf");
- free(*id);
- *id = NULL;
- }
- got_object_tag_close(tag);
- return err;
- } else if (err->code != GOT_ERR_OBJ_TYPE &&
- err->code != GOT_ERR_NO_OBJ)
- return err;
- }
-
- err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
- if (err) {
- if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
- return err;
- err = got_ref_open(&ref, repo, id_str, 0);
- if (err != NULL)
- goto done;
- *label = strdup(got_ref_get_name(ref));
- if (*label == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- err = got_ref_resolve(id, repo, ref);
- } else {
- err = got_object_id_str(label, *id);
- if (*label == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- }
-done:
- if (ref)
- got_ref_close(ref);
- return err;
-}
-
-
-static const struct got_error *
cmd_diff(int argc, char *argv[])
{
const struct got_error *error;
@@ -2396,13 +2336,13 @@ cmd_diff(int argc, char *argv[])
goto done;
}
- error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
- repo);
+ error = got_repo_match_object_id(&id1, &label1, id_str1,
+ GOT_OBJ_TYPE_ANY, 1, repo);
if (error)
goto done;
- error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
- repo);
+ error = got_repo_match_object_id(&id2, &label2, id_str2,
+ GOT_OBJ_TYPE_ANY, 1, repo);
if (error)
goto done;
@@ -2691,8 +2631,8 @@ cmd_blame(int argc, char *argv[])
if (error != NULL)
goto done;
} else {
- error = got_repo_resolve_commit_arg(&commit_id,
- commit_id_str, repo);
+ error = got_repo_match_object_id(&commit_id, NULL,
+ commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
if (error)
goto done;
}
@@ -2996,8 +2936,8 @@ cmd_tree(int argc, char *argv[])
if (error != NULL)
goto done;
} else {
- error = got_repo_resolve_commit_arg(&commit_id,
- commit_id_str, repo);
+ error = got_repo_match_object_id(&commit_id, NULL,
+ commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
if (error)
goto done;
}
@@ -3637,8 +3577,8 @@ cmd_branch(int argc, char *argv[])
commit_id_arg = worktree ?
got_worktree_get_head_ref_name(worktree) :
GOT_REF_HEAD;
- error = got_repo_resolve_commit_arg(&commit_id,
- commit_id_arg, repo);
+ error = got_repo_match_object_id(&commit_id, NULL,
+ commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
if (error)
goto done;
error = add_branch(repo, argv[0], commit_id);
@@ -3978,7 +3918,7 @@ add_tag(struct got_repository *repo, const char *tag_name,
if (err)
return err;
- err = match_object_id(&commit_id, &label, commit_arg,
+ err = got_repo_match_object_id(&commit_id, &label, commit_arg,
GOT_OBJ_TYPE_COMMIT, 1, repo);
if (err)
goto done;
@@ -7360,7 +7300,8 @@ cmd_cat(int argc, char *argv[])
if (commit_id_str == NULL)
commit_id_str = GOT_REF_HEAD;
- error = got_repo_resolve_commit_arg(&commit_id, commit_id_str, repo);
+ error = got_repo_match_object_id(&commit_id, NULL,
+ commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
if (error)
goto done;
@@ -7371,7 +7312,7 @@ cmd_cat(int argc, char *argv[])
if (error)
break;
} else {
- error = match_object_id(&id, &label, argv[i],
+ error = got_repo_match_object_id(&id, &label, argv[i],
GOT_OBJ_TYPE_ANY, 0, repo);
if (error) {
if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
diff --git a/include/got_repository.h b/include/got_repository.h
index 4255434..f54c896 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -88,20 +88,24 @@ const struct got_error *got_repo_match_object_id_prefix(struct got_object_id **,
const char *, int, struct got_repository *);
/*
+ * Given an object ID string or reference name, attempt to find a corresponding
+ * commit object. Tags can optionally be ignored during matching.
+ * The object type may be restricted to commit, tree, blob, or tag.
+ * GOT_OBJ_TYPE_ANY will match any type of object.
+ * A human-readable label can optionally be returned, which the caller should
+ * dispose of with free(3).
+ * Return GOT_ERR_NO_OBJ if no matching commit can be found.
+ */
+const struct got_error *got_repo_match_object_id(struct got_object_id **,
+ char **, const char *, int, int, struct got_repository *);
+
+/*
* Attempt to find a tag object with a given name and target object type.
* Return GOT_ERR_NO_OBJ if no matching tag can be found.
*/
const struct got_error *got_repo_object_match_tag(struct got_tag_object **,
const char *, int, struct got_repository *);
-/*
- * Given an object ID string or reference name argument,
- * attempt to find a corresponding commit object.
- * Return GOT_ERR_NO_OBJ if no matching commit can be found.
- */
-const struct got_error *got_repo_resolve_commit_arg(struct got_object_id **,
- const char *, struct got_repository *);
-
/* A callback function which is invoked when a path is imported. */
typedef const struct got_error *(*got_repo_import_cb)(void *, const char *);
diff --git a/lib/repository.c b/lib/repository.c
index 6ab751c..694024d 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -1355,6 +1355,68 @@ done:
}
const struct got_error *
+got_repo_match_object_id(struct got_object_id **id, char **label,
+ const char *id_str, int obj_type, int resolve_tags,
+ struct got_repository *repo)
+{
+ const struct got_error *err;
+ struct got_tag_object *tag;
+ struct got_reference *ref = NULL;
+
+ *id = NULL;
+ if (label)
+ *label = NULL;
+
+ if (resolve_tags) {
+ err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
+ repo);
+ if (err == NULL) {
+ *id = got_object_id_dup(
+ got_object_tag_get_object_id(tag));
+ if (*id == NULL)
+ err = got_error_from_errno("got_object_id_dup");
+ else if (label && asprintf(label, "refs/tags/%s",
+ got_object_tag_get_name(tag)) == -1) {
+ err = got_error_from_errno("asprintf");
+ free(*id);
+ *id = NULL;
+ }
+ got_object_tag_close(tag);
+ return err;
+ } else if (err->code != GOT_ERR_OBJ_TYPE &&
+ err->code != GOT_ERR_NO_OBJ)
+ return err;
+ }
+
+ err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
+ if (err) {
+ if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
+ return err;
+ err = got_ref_open(&ref, repo, id_str, 0);
+ if (err != NULL)
+ goto done;
+ if (label) {
+ *label = strdup(got_ref_get_name(ref));
+ if (*label == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
+ }
+ err = got_ref_resolve(id, repo, ref);
+ } else if (label) {
+ err = got_object_id_str(label, *id);
+ if (*label == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
+ }
+done:
+ if (ref)
+ got_ref_close(ref);
+ return err;
+}
+
+const struct got_error *
got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
int obj_type, struct got_repository *repo)
{
@@ -1398,39 +1460,6 @@ got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
return err;
}
-const struct got_error *
-got_repo_resolve_commit_arg(struct got_object_id **commit_id,
- const char *commit_id_arg, struct got_repository *repo)
-{
- const struct got_error *err;
- struct got_reference *ref;
- struct got_tag_object *tag;
-
- err = got_repo_object_match_tag(&tag, commit_id_arg,
- GOT_OBJ_TYPE_COMMIT, repo);
- if (err == NULL) {
- *commit_id = got_object_id_dup(
- got_object_tag_get_object_id(tag));
- if (*commit_id == NULL)
- err = got_error_from_errno("got_object_id_dup");
- got_object_tag_close(tag);
- return err;
- } else if (err->code != GOT_ERR_NO_OBJ)
- return err;
-
- err = got_ref_open(&ref, repo, commit_id_arg, 0);
- if (err == NULL) {
- err = got_ref_resolve(commit_id, repo, ref);
- got_ref_close(ref);
- } else {
- if (err->code != GOT_ERR_NOT_REF)
- return err;
- err = got_repo_match_object_id_prefix(commit_id,
- commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
- }
- return err;
-}
-
static const struct got_error *
alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
const char *name, mode_t mode, struct got_object_id *blob_id)
diff --git a/tog/tog.c b/tog/tog.c
index 6a748ce..ac26de5 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2387,9 +2387,9 @@ input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
view->begin_y, view->begin_x, TOG_VIEW_LOG);
if (lv == NULL)
return got_error_from_errno("view_open");
- err = got_repo_resolve_commit_arg(&start_id,
- s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
- s->repo);
+ err = got_repo_match_object_id(&start_id, NULL,
+ s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
+ GOT_OBJ_TYPE_COMMIT, 1, s->repo);
if (err) {
view_close(lv);
return err;
@@ -2570,12 +2570,12 @@ cmd_log(int argc, char *argv[])
goto done;
if (start_commit == NULL)
- error = got_repo_resolve_commit_arg(&start_id, worktree ?
+ error = got_repo_match_object_id(&start_id, NULL, worktree ?
got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
- repo);
+ GOT_OBJ_TYPE_COMMIT, 1, repo);
else
- error = got_repo_resolve_commit_arg(&start_id, start_commit,
- repo);
+ error = got_repo_match_object_id(&start_id, NULL, start_commit,
+ GOT_OBJ_TYPE_COMMIT, 1, repo);
if (error != NULL)
goto done;
@@ -4222,8 +4222,8 @@ cmd_blame(int argc, char *argv[])
error = got_ref_resolve(&commit_id, repo, head_ref);
got_ref_close(head_ref);
} else {
- error = got_repo_resolve_commit_arg(&commit_id, commit_id_str,
- repo);
+ error = got_repo_match_object_id(&commit_id, NULL,
+ commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
}
if (error != NULL)
goto done;
@@ -5010,8 +5010,9 @@ cmd_tree(int argc, char *argv[])
if (error)
goto done;
- error = got_repo_resolve_commit_arg(&commit_id,
- commit_id_arg ? commit_id_arg : GOT_REF_HEAD, repo);
+ error = got_repo_match_object_id(&commit_id, NULL,
+ commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
+ GOT_OBJ_TYPE_COMMIT, 1, repo);
if (error != NULL)
goto done;