interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / replace-object.c

109 lines · 2774 bytes Raw Blame History
1#include "git-compat-util.h"
2#include "gettext.h"
3#include "hex.h"
4#include "oidmap.h"
5#include "odb.h"
6#include "replace-object.h"
7#include "refs.h"
8#include "repository.h"
9#include "commit.h"
10
11static int register_replace_ref(const struct reference *ref, void *cb_data)
12{
13 struct repository *r = cb_data;
14
15 /* Get sha1 from refname */
16 const char *slash = strrchr(ref->name, '/');
17 const char *hash = slash ? slash + 1 : ref->name;
18 struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
19
20 if (get_oid_hex_algop(hash, &repl_obj->original.oid, r->hash_algo)) {
21 free(repl_obj);
22 warning(_("bad replace ref name: %s"), ref->name);
23 return 0;
24 }
25
26 /* Copy sha1 from the read ref */
27 oidcpy(&repl_obj->replacement, ref->oid);
28
29 /* Register new object */
30 if (oidmap_put(&r->objects->replace_map, repl_obj))
31 die(_("duplicate replace ref: %s"), ref->name);
32
33 return 0;
34}
35
36void prepare_replace_object(struct repository *r)
37{
38 if (r->objects->replace_map_initialized)
39 return;
40
41 pthread_mutex_lock(&r->objects->replace_mutex);
42 if (r->objects->replace_map_initialized) {
43 pthread_mutex_unlock(&r->objects->replace_mutex);
44 return;
45 }
46
47 oidmap_init(&r->objects->replace_map, 0);
48
49 refs_for_each_replace_ref(get_main_ref_store(r),
50 register_replace_ref, r);
51 r->objects->replace_map_initialized = 1;
52
53 pthread_mutex_unlock(&r->objects->replace_mutex);
54}
55
56/* We allow "recursive" replacement. Only within reason, though */
57#define MAXREPLACEDEPTH 5
58
59/*
60 * If a replacement for object oid has been set up, return the
61 * replacement object's name (replaced recursively, if necessary).
62 * The return value is either oid or a pointer to a
63 * permanently-allocated value. This function always respects replace
64 * references, regardless of the value of r->settings.read_replace_refs.
65 */
66const struct object_id *do_lookup_replace_object(struct repository *r,
67 const struct object_id *oid)
68{
69 int depth = MAXREPLACEDEPTH;
70 const struct object_id *cur = oid;
71
72 prepare_replace_object(r);
73
74 /* Try to recursively replace the object */
75 while (depth-- > 0) {
76 struct replace_object *repl_obj =
77 oidmap_get(&r->objects->replace_map, cur);
78 if (!repl_obj)
79 return cur;
80 cur = &repl_obj->replacement;
81 }
82 die(_("replace depth too high for object %s"), oid_to_hex(oid));
83}
84
85/*
86 * This indicator determines whether replace references should be
87 * respected process-wide, regardless of which repository is being
88 * using at the time.
89 */
90static int read_replace_refs = 1;
91
92void disable_replace_refs(void)
93{
94 read_replace_refs = 0;
95}
96
97int replace_refs_enabled(struct repository *r)
98{
99 if (!read_replace_refs)
100 return 0;
101
102 if (r->gitdir) {
103 prepare_repo_settings(r);
104 return r->settings.read_replace_refs;
105 }
106
107 /* repository has no objects or refs. */
108 return 0;
109}