refs: move object peeling into "object.c"
Peeling an object has nothing to do with refs, but we still have the code in "refs.c". Move it over into "object.c", which is a more natural place to put it. Ideally, we'd also move `peel_iterated_oid()` over into "object.c". But this function is tied to the refs interfaces because it uses a global ref iterator variable to optimize peeling when the iterator already has the peeled object ID readily available. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
May 17, 2024 at 10:18 UTC
19c76e8235747a61a703fe3bf7a5e40caf6fa3fa
4 files changed
+55
-56
object.c
+21
@@ -207,6 +207,27 @@ struct object *lookup_object_by_type(struct repository *r,
207
}
208
}
209
210
+enum peel_status peel_object(const struct object_id *name, struct object_id *oid)
211
+{
212
+ struct object *o = lookup_unknown_object(the_repository, name);
213
+
214
+ if (o->type == OBJ_NONE) {
215
+ int type = oid_object_info(the_repository, name, NULL);
216
+ if (type < 0 || !object_as_type(o, type, 0))
217
+ return PEEL_INVALID;
218
+ }
219
+
220
+ if (o->type != OBJ_TAG)
221
+ return PEEL_NON_TAG;
222
+
223
+ o = deref_tag_noverify(o);
224
+ if (!o)
225
+ return PEEL_INVALID;
226
+
227
+ oidcpy(oid, &o->oid);
228
+ return PEEL_PEELED;
229
+}
230
+
231
struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
232
{
233
struct object *obj;
object.h
+34
@@ -256,6 +256,40 @@ struct object *lookup_unknown_object(struct repository *r, const struct object_i
256
struct object *lookup_object_by_type(struct repository *r, const struct object_id *oid,
257
enum object_type type);
258
259
+enum peel_status {
260
+ /* object was peeled successfully: */
261
+ PEEL_PEELED = 0,
262
+
263
+ /*
264
+ * object cannot be peeled because the named object (or an
265
+ * object referred to by a tag in the peel chain), does not
266
+ * exist.
267
+ */
268
+ PEEL_INVALID = -1,
269
+
270
+ /* object cannot be peeled because it is not a tag: */
271
+ PEEL_NON_TAG = -2,
272
+
273
+ /* ref_entry contains no peeled value because it is a symref: */
274
+ PEEL_IS_SYMREF = -3,
275
+
276
+ /*
277
+ * ref_entry cannot be peeled because it is broken (i.e., the
278
+ * symbolic reference cannot even be resolved to an object
279
+ * name):
280
+ */
281
+ PEEL_BROKEN = -4
282
+};
283
+
284
+/*
285
+ * Peel the named object; i.e., if the object is a tag, resolve the
286
+ * tag recursively until a non-tag is found. If successful, store the
287
+ * result to oid and return PEEL_PEELED. If the object is not a tag
288
+ * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively,
289
+ * and leave oid unchanged.
290
+ */
291
+enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
292
+
293
struct object_list *object_list_insert(struct object *item,
294
struct object_list **list_p);
295
refs.c
-22
@@ -19,7 +19,6 @@
19
#include "object-store-ll.h"
20
#include "object.h"
21
#include "path.h"
22
-#include "tag.h"
22
#include "submodule.h"
23
#include "worktree.h"
24
#include "strvec.h"
@@ -425,27 +424,6 @@ static int for_each_filter_refs(const char *refname,
424
return filter->fn(refname, oid, flags, filter->cb_data);
425
}
426
428
-enum peel_status peel_object(const struct object_id *name, struct object_id *oid)
429
-{
430
- struct object *o = lookup_unknown_object(the_repository, name);
431
-
432
- if (o->type == OBJ_NONE) {
433
- int type = oid_object_info(the_repository, name, NULL);
434
- if (type < 0 || !object_as_type(o, type, 0))
435
- return PEEL_INVALID;
436
- }
437
-
438
- if (o->type != OBJ_TAG)
439
- return PEEL_NON_TAG;
440
-
441
- o = deref_tag_noverify(o);
442
- if (!o)
443
- return PEEL_INVALID;
444
-
445
- oidcpy(oid, &o->oid);
446
- return PEEL_PEELED;
447
-}
448
-
427
struct warn_if_dangling_data {
428
struct ref_store *refs;
429
FILE *fp;
refs/refs-internal.h
-34
@@ -69,40 +69,6 @@ int ref_resolves_to_object(const char *refname,
69
const struct object_id *oid,
70
unsigned int flags);
71
72
-enum peel_status {
73
- /* object was peeled successfully: */
74
- PEEL_PEELED = 0,
75
-
76
- /*
77
- * object cannot be peeled because the named object (or an
78
- * object referred to by a tag in the peel chain), does not
79
- * exist.
80
- */
81
- PEEL_INVALID = -1,
82
-
83
- /* object cannot be peeled because it is not a tag: */
84
- PEEL_NON_TAG = -2,
85
-
86
- /* ref_entry contains no peeled value because it is a symref: */
87
- PEEL_IS_SYMREF = -3,
88
-
89
- /*
90
- * ref_entry cannot be peeled because it is broken (i.e., the
91
- * symbolic reference cannot even be resolved to an object
92
- * name):
93
- */
94
- PEEL_BROKEN = -4
95
-};
96
-
97
-/*
98
- * Peel the named object; i.e., if the object is a tag, resolve the
99
- * tag recursively until a non-tag is found. If successful, store the
100
- * result to oid and return PEEL_PEELED. If the object is not a tag
101
- * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively,
102
- * and leave oid unchanged.
103
- */
104
-enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
105
-
72
/**
73
* Information needed for a single ref update. Set new_oid to the new
74
* value or to null_oid to delete the ref. To check the old value