93
static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
94
{
95
const char **p, *full_hex;
96
- char ref[PATH_MAX];
96
+ struct strbuf ref = STRBUF_INIT;
97
+ size_t base_len;
98
int had_error = 0;
99
struct object_id oid;
100
101
+ strbuf_addstr(&ref, git_replace_ref_base);
102
+ base_len = ref.len;
103
+
104
for (p = argv; *p; p++) {
105
if (get_oid(*p, &oid)) {
106
error("Failed to resolve '%s' as a valid ref.", *p);
107
had_error = 1;
108
continue;
109
}
106
- full_hex = oid_to_hex(&oid);
107
- snprintf(ref, sizeof(ref), "%s%s", git_replace_ref_base, full_hex);
108
- /* read_ref() may reuse the buffer */
109
- full_hex = ref + strlen(git_replace_ref_base);
110
- if (read_ref(ref, oid.hash)) {
110
+
111
+ strbuf_setlen(&ref, base_len);
112
+ strbuf_addstr(&ref, oid_to_hex(&oid));
113
+ full_hex = ref.buf + base_len;
114
+
115
+ if (read_ref(ref.buf, oid.hash)) {
116
error("replace ref '%s' not found.", full_hex);
117
had_error = 1;
118
continue;
119
}
115
- if (fn(full_hex, ref, &oid))
120
+ if (fn(full_hex, ref.buf, &oid))
121
had_error = 1;
122
}
123
return had_error;
134
135
static void check_ref_valid(struct object_id *object,
136
struct object_id *prev,
132
- char *ref,
133
- int ref_size,
137
+ struct strbuf *ref,
138
int force)
139
{
136
- if (snprintf(ref, ref_size,
137
- "%s%s", git_replace_ref_base,
138
- oid_to_hex(object)) > ref_size - 1)
139
- die("replace ref name too long: %.*s...", 50, ref);
140
- if (check_refname_format(ref, 0))
141
- die("'%s' is not a valid ref name.", ref);
142
-
143
- if (read_ref(ref, prev->hash))
140
+ strbuf_reset(ref);
141
+ strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
142
+ if (check_refname_format(ref->buf, 0))
143
+ die("'%s' is not a valid ref name.", ref->buf);
144
+
145
+ if (read_ref(ref->buf, prev->hash))
146
oidclr(prev);
147
else if (!force)
146
- die("replace ref '%s' already exists", ref);
148
+ die("replace ref '%s' already exists", ref->buf);
149
}
150
151
static int replace_object_oid(const char *object_ref,
156
{
157
struct object_id prev;
158
enum object_type obj_type, repl_type;
157
- char ref[PATH_MAX];
159
+ struct strbuf ref = STRBUF_INIT;
160
struct ref_transaction *transaction;
161
struct strbuf err = STRBUF_INIT;
162
169
object_ref, typename(obj_type),
170
replace_ref, typename(repl_type));
171
170
- check_ref_valid(object, &prev, ref, sizeof(ref), force);
172
+ check_ref_valid(object, &prev, &ref, force);
173
174
transaction = ref_transaction_begin(&err);
175
if (!transaction ||
174
- ref_transaction_update(transaction, ref, repl->hash, prev.hash,
176
+ ref_transaction_update(transaction, ref.buf, repl->hash, prev.hash,
177
0, NULL, &err) ||
178
ref_transaction_commit(transaction, &err))
179
die("%s", err.buf);
180
181
ref_transaction_free(transaction);
182
+ strbuf_release(&ref);
183
return 0;
184
}
185
283
char *tmpfile = git_pathdup("REPLACE_EDITOBJ");
284
enum object_type type;
285
struct object_id old, new, prev;
283
- char ref[PATH_MAX];
286
+ struct strbuf ref = STRBUF_INIT;
287
288
if (get_oid(object_ref, &old) < 0)
289
die("Not a valid object name: '%s'", object_ref);
292
if (type < 0)
293
die("unable to get object type for %s", oid_to_hex(&old));
294
292
- check_ref_valid(&old, &prev, ref, sizeof(ref), force);
295
+ check_ref_valid(&old, &prev, &ref, force);
296
+ strbuf_release(&ref);
297
298
export_object(&old, type, raw, tmpfile);
299
if (launch_editor(tmpfile, NULL, NULL) < 0)