builtin/replace: convert to struct object_id
Convert various uses of unsigned char [20] to struct object_id. Rename replace_object_sha1 to replace_object_oid. Finally, specify a constant in terms of GIT_SHA1_HEXSZ. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 21, 2017 at 23:47 UTC
cea4332e5414f5506e6d0803811ba0b095d08518
1 file changed
+56
-56
builtin/replace.c
+56
-56
@@ -88,78 +88,78 @@ static int list_replace_refs(const char *pattern, const char *format)
88
}
89
90
typedef int (*each_replace_name_fn)(const char *name, const char *ref,
91
- const unsigned char *sha1);
91
+ const struct object_id *oid);
92
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];
97
int had_error = 0;
98
- unsigned char sha1[20];
98
+ struct object_id oid;
99
100
for (p = argv; *p; p++) {
101
- if (get_sha1(*p, sha1)) {
101
+ if (get_oid(*p, &oid)) {
102
error("Failed to resolve '%s' as a valid ref.", *p);
103
had_error = 1;
104
continue;
105
}
106
- full_hex = sha1_to_hex(sha1);
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, sha1)) {
110
+ if (read_ref(ref, oid.hash)) {
111
error("replace ref '%s' not found.", full_hex);
112
had_error = 1;
113
continue;
114
}
115
- if (fn(full_hex, ref, sha1))
115
+ if (fn(full_hex, ref, &oid))
116
had_error = 1;
117
}
118
return had_error;
119
}
120
121
static int delete_replace_ref(const char *name, const char *ref,
122
- const unsigned char *sha1)
122
+ const struct object_id *oid)
123
{
124
- if (delete_ref(ref, sha1, 0))
124
+ if (delete_ref(ref, oid->hash, 0))
125
return 1;
126
printf("Deleted replace ref '%s'\n", name);
127
return 0;
128
}
129
130
-static void check_ref_valid(unsigned char object[20],
131
- unsigned char prev[20],
130
+static void check_ref_valid(struct object_id *object,
131
+ struct object_id *prev,
132
char *ref,
133
int ref_size,
134
int force)
135
{
136
if (snprintf(ref, ref_size,
137
"%s%s", git_replace_ref_base,
138
- sha1_to_hex(object)) > ref_size - 1)
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))
144
- hashclr(prev);
143
+ if (read_ref(ref, prev->hash))
144
+ oidclr(prev);
145
else if (!force)
146
die("replace ref '%s' already exists", ref);
147
}
148
149
-static int replace_object_sha1(const char *object_ref,
150
- unsigned char object[20],
149
+static int replace_object_oid(const char *object_ref,
150
+ struct object_id *object,
151
const char *replace_ref,
152
- unsigned char repl[20],
152
+ struct object_id *repl,
153
int force)
154
{
155
- unsigned char prev[20];
155
+ struct object_id prev;
156
enum object_type obj_type, repl_type;
157
char ref[PATH_MAX];
158
struct ref_transaction *transaction;
159
struct strbuf err = STRBUF_INIT;
160
161
- obj_type = sha1_object_info(object, NULL);
162
- repl_type = sha1_object_info(repl, NULL);
161
+ obj_type = sha1_object_info(object->hash, NULL);
162
+ repl_type = sha1_object_info(repl->hash, NULL);
163
if (!force && obj_type != repl_type)
164
die("Objects must be of the same type.\n"
165
"'%s' points to a replaced object of type '%s'\n"
@@ -167,11 +167,11 @@ static int replace_object_sha1(const char *object_ref,
167
object_ref, typename(obj_type),
168
replace_ref, typename(repl_type));
169
170
- check_ref_valid(object, prev, ref, sizeof(ref), force);
170
+ check_ref_valid(object, &prev, ref, sizeof(ref), force);
171
172
transaction = ref_transaction_begin(&err);
173
if (!transaction ||
174
- ref_transaction_update(transaction, ref, repl, prev,
174
+ ref_transaction_update(transaction, ref, repl->hash, prev.hash,
175
0, NULL, &err) ||
176
ref_transaction_commit(transaction, &err))
177
die("%s", err.buf);
@@ -182,14 +182,14 @@ static int replace_object_sha1(const char *object_ref,
182
183
static int replace_object(const char *object_ref, const char *replace_ref, int force)
184
{
185
- unsigned char object[20], repl[20];
185
+ struct object_id object, repl;
186
187
- if (get_sha1(object_ref, object))
187
+ if (get_oid(object_ref, &object))
188
die("Failed to resolve '%s' as a valid ref.", object_ref);
189
- if (get_sha1(replace_ref, repl))
189
+ if (get_oid(replace_ref, &repl))
190
die("Failed to resolve '%s' as a valid ref.", replace_ref);
191
192
- return replace_object_sha1(object_ref, object, replace_ref, repl, force);
192
+ return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
193
}
194
195
/*
@@ -197,7 +197,7 @@ static int replace_object(const char *object_ref, const char *replace_ref, int f
197
* If "raw" is true, then the object's raw contents are printed according to
198
* "type". Otherwise, we pretty-print the contents for human editing.
199
*/
200
-static void export_object(const unsigned char *sha1, enum object_type type,
200
+static void export_object(const struct object_id *oid, enum object_type type,
201
int raw, const char *filename)
202
{
203
struct child_process cmd = CHILD_PROCESS_INIT;
@@ -213,7 +213,7 @@ static void export_object(const unsigned char *sha1, enum object_type type,
213
argv_array_push(&cmd.args, typename(type));
214
else
215
argv_array_push(&cmd.args, "-p");
216
- argv_array_push(&cmd.args, sha1_to_hex(sha1));
216
+ argv_array_push(&cmd.args, oid_to_hex(oid));
217
cmd.git_cmd = 1;
218
cmd.out = fd;
219
@@ -226,7 +226,7 @@ static void export_object(const unsigned char *sha1, enum object_type type,
226
* interpreting it as "type", and writing the result to the object database.
227
* The sha1 of the written object is returned via sha1.
228
*/
229
-static void import_object(unsigned char *sha1, enum object_type type,
229
+static void import_object(struct object_id *oid, enum object_type type,
230
int raw, const char *filename)
231
{
232
int fd;
@@ -254,7 +254,7 @@ static void import_object(unsigned char *sha1, enum object_type type,
254
255
if (finish_command(&cmd))
256
die("mktree reported failure");
257
- if (get_sha1_hex(result.buf, sha1) < 0)
257
+ if (get_oid_hex(result.buf, oid) < 0)
258
die("mktree did not return an object name");
259
260
strbuf_release(&result);
@@ -264,7 +264,7 @@ static void import_object(unsigned char *sha1, enum object_type type,
264
265
if (fstat(fd, &st) < 0)
266
die_errno("unable to fstat %s", filename);
267
- if (index_fd(sha1, fd, &st, type, NULL, flags) < 0)
267
+ if (index_fd(oid->hash, fd, &st, type, NULL, flags) < 0)
268
die("unable to write object to database");
269
/* index_fd close()s fd for us */
270
}
@@ -279,29 +279,29 @@ static int edit_and_replace(const char *object_ref, int force, int raw)
279
{
280
char *tmpfile = git_pathdup("REPLACE_EDITOBJ");
281
enum object_type type;
282
- unsigned char old[20], new[20], prev[20];
282
+ struct object_id old, new, prev;
283
char ref[PATH_MAX];
284
285
- if (get_sha1(object_ref, old) < 0)
285
+ if (get_oid(object_ref, &old) < 0)
286
die("Not a valid object name: '%s'", object_ref);
287
288
- type = sha1_object_info(old, NULL);
288
+ type = sha1_object_info(old.hash, NULL);
289
if (type < 0)
290
- die("unable to get object type for %s", sha1_to_hex(old));
290
+ die("unable to get object type for %s", oid_to_hex(&old));
291
292
- check_ref_valid(old, prev, ref, sizeof(ref), force);
292
+ check_ref_valid(&old, &prev, ref, sizeof(ref), force);
293
294
- export_object(old, type, raw, tmpfile);
294
+ export_object(&old, type, raw, tmpfile);
295
if (launch_editor(tmpfile, NULL, NULL) < 0)
296
die("editing object file failed");
297
- import_object(new, type, raw, tmpfile);
297
+ import_object(&new, type, raw, tmpfile);
298
299
free(tmpfile);
300
301
- if (!hashcmp(old, new))
302
- return error("new object is the same as the old one: '%s'", sha1_to_hex(old));
301
+ if (!oidcmp(&old, &new))
302
+ return error("new object is the same as the old one: '%s'", oid_to_hex(&old));
303
304
- return replace_object_sha1(object_ref, old, "replacement", new, force);
304
+ return replace_object_oid(object_ref, &old, "replacement", &new, force);
305
}
306
307
static void replace_parents(struct strbuf *buf, int argc, const char **argv)
@@ -312,7 +312,7 @@ static void replace_parents(struct strbuf *buf, int argc, const char **argv)
312
313
/* find existing parents */
314
parent_start = buf->buf;
315
- parent_start += 46; /* "tree " + "hex sha1" + "\n" */
315
+ parent_start += GIT_SHA1_HEXSZ + 6; /* "tree " + "hex sha1" + "\n" */
316
parent_end = parent_start;
317
318
while (starts_with(parent_end, "parent "))
@@ -320,11 +320,11 @@ static void replace_parents(struct strbuf *buf, int argc, const char **argv)
320
321
/* prepare new parents */
322
for (i = 0; i < argc; i++) {
323
- unsigned char sha1[20];
324
- if (get_sha1(argv[i], sha1) < 0)
323
+ struct object_id oid;
324
+ if (get_oid(argv[i], &oid) < 0)
325
die(_("Not a valid object name: '%s'"), argv[i]);
326
- lookup_commit_or_die(sha1, argv[i]);
327
- strbuf_addf(&new_parents, "parent %s\n", sha1_to_hex(sha1));
326
+ lookup_commit_or_die(oid.hash, argv[i]);
327
+ strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&oid));
328
}
329
330
/* replace existing parents with new ones */
@@ -345,12 +345,12 @@ static void check_one_mergetag(struct commit *commit,
345
{
346
struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
347
const char *ref = mergetag_data->argv[0];
348
- unsigned char tag_sha1[20];
348
+ struct object_id tag_oid;
349
struct tag *tag;
350
int i;
351
352
- hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), tag_sha1);
353
- tag = lookup_tag(tag_sha1);
352
+ hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), tag_oid.hash);
353
+ tag = lookup_tag(tag_oid.hash);
354
if (!tag)
355
die(_("bad mergetag in commit '%s'"), ref);
356
if (parse_tag_buffer(tag, extra->value, extra->len))
@@ -366,7 +366,7 @@ static void check_one_mergetag(struct commit *commit,
366
}
367
368
die(_("original commit '%s' contains mergetag '%s' that is discarded; "
369
- "use --edit instead of --graft"), ref, sha1_to_hex(tag_sha1));
369
+ "use --edit instead of --graft"), ref, oid_to_hex(&tag_oid));
370
}
371
372
static void check_mergetags(struct commit *commit, int argc, const char **argv)
@@ -380,16 +380,16 @@ static void check_mergetags(struct commit *commit, int argc, const char **argv)
380
381
static int create_graft(int argc, const char **argv, int force)
382
{
383
- unsigned char old[20], new[20];
383
+ struct object_id old, new;
384
const char *old_ref = argv[0];
385
struct commit *commit;
386
struct strbuf buf = STRBUF_INIT;
387
const char *buffer;
388
unsigned long size;
389
390
- if (get_sha1(old_ref, old) < 0)
390
+ if (get_oid(old_ref, &old) < 0)
391
die(_("Not a valid object name: '%s'"), old_ref);
392
- commit = lookup_commit_or_die(old, old_ref);
392
+ commit = lookup_commit_or_die(old.hash, old_ref);
393
394
buffer = get_commit_buffer(commit, &size);
395
strbuf_add(&buf, buffer, size);
@@ -404,15 +404,15 @@ static int create_graft(int argc, const char **argv, int force)
404
405
check_mergetags(commit, argc, argv);
406
407
- if (write_sha1_file(buf.buf, buf.len, commit_type, new))
407
+ if (write_sha1_file(buf.buf, buf.len, commit_type, new.hash))
408
die(_("could not write replacement commit for: '%s'"), old_ref);
409
410
strbuf_release(&buf);
411
412
- if (!hashcmp(old, new))
413
- return error("new commit is the same as the old one: '%s'", sha1_to_hex(old));
412
+ if (!oidcmp(&old, &new))
413
+ return error("new commit is the same as the old one: '%s'", oid_to_hex(&old));
414
415
- return replace_object_sha1(old_ref, old, "replacement", new, force);
415
+ return replace_object_oid(old_ref, &old, "replacement", &new, force);
416
}
417
418
int cmd_replace(int argc, const char **argv, const char *prefix)