builtin/receive-pack: convert portions to struct object_id
Convert some hardcoded constants into uses of parse_oid_hex. Additionally, convert all uses of struct command, and miscellaneous other functions necessary for that. This work is necessary to be able to convert sha1_array_append later on. To avoid needing to specify a constant, reject shallow lines with the wrong length instead of simply ignoring them. Note that in queue_command we are guaranteed to have a NUL-terminated buffer or at least one byte of overflow that we can safely read, so the linelen check can be elided. We would die in such a case, but not read invalid memory. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Mar 26, 2017 at 16:01 UTC
9c44ea440323e35e4a234d0d27d511e9b4d8dc21
1 file changed
+49
-49
builtin/receive-pack.c
+49
-49
@@ -309,8 +309,8 @@ struct command {
309
unsigned int skip_update:1,
310
did_not_exist:1;
311
int index;
312
- unsigned char old_sha1[20];
313
- unsigned char new_sha1[20];
312
+ struct object_id old_oid;
313
+ struct object_id new_oid;
314
char ref_name[FLEX_ARRAY]; /* more */
315
};
316
@@ -723,7 +723,7 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
723
return -1; /* EOF */
724
strbuf_reset(&state->buf);
725
strbuf_addf(&state->buf, "%s %s %s\n",
726
- sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
726
+ oid_to_hex(&cmd->old_oid), oid_to_hex(&cmd->new_oid),
727
cmd->ref_name);
728
state->cmd = cmd->next;
729
if (bufp) {
@@ -764,8 +764,8 @@ static int run_update_hook(struct command *cmd)
764
return 0;
765
766
argv[1] = cmd->ref_name;
767
- argv[2] = sha1_to_hex(cmd->old_sha1);
768
- argv[3] = sha1_to_hex(cmd->new_sha1);
767
+ argv[2] = oid_to_hex(&cmd->old_oid);
768
+ argv[3] = oid_to_hex(&cmd->new_oid);
769
argv[4] = NULL;
770
771
proc.no_stdin = 1;
@@ -988,8 +988,8 @@ static const char *update(struct command *cmd, struct shallow_info *si)
988
const char *name = cmd->ref_name;
989
struct strbuf namespaced_name_buf = STRBUF_INIT;
990
const char *namespaced_name, *ret;
991
- unsigned char *old_sha1 = cmd->old_sha1;
992
- unsigned char *new_sha1 = cmd->new_sha1;
991
+ struct object_id *old_oid = &cmd->old_oid;
992
+ struct object_id *new_oid = &cmd->new_oid;
993
994
/* only refs/... are allowed */
995
if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
@@ -1014,20 +1014,20 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1014
refuse_unconfigured_deny();
1015
return "branch is currently checked out";
1016
case DENY_UPDATE_INSTEAD:
1017
- ret = update_worktree(new_sha1);
1017
+ ret = update_worktree(new_oid->hash);
1018
if (ret)
1019
return ret;
1020
break;
1021
}
1022
}
1023
1024
- if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
1024
+ if (!is_null_oid(new_oid) && !has_object_file(new_oid)) {
1025
error("unpack should have generated %s, "
1026
- "but I can't find it!", sha1_to_hex(new_sha1));
1026
+ "but I can't find it!", oid_to_hex(new_oid));
1027
return "bad pack";
1028
}
1029
1030
- if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
1030
+ if (!is_null_oid(old_oid) && is_null_oid(new_oid)) {
1031
if (deny_deletes && starts_with(name, "refs/heads/")) {
1032
rp_error("denying ref deletion for %s", name);
1033
return "deletion prohibited";
@@ -1053,14 +1053,14 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1053
}
1054
}
1055
1056
- if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
1057
- !is_null_sha1(old_sha1) &&
1056
+ if (deny_non_fast_forwards && !is_null_oid(new_oid) &&
1057
+ !is_null_oid(old_oid) &&
1058
starts_with(name, "refs/heads/")) {
1059
struct object *old_object, *new_object;
1060
struct commit *old_commit, *new_commit;
1061
1062
- old_object = parse_object(old_sha1);
1063
- new_object = parse_object(new_sha1);
1062
+ old_object = parse_object(old_oid->hash);
1063
+ new_object = parse_object(new_oid->hash);
1064
1065
if (!old_object || !new_object ||
1066
old_object->type != OBJ_COMMIT ||
@@ -1081,10 +1081,10 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1081
return "hook declined";
1082
}
1083
1084
- if (is_null_sha1(new_sha1)) {
1084
+ if (is_null_oid(new_oid)) {
1085
struct strbuf err = STRBUF_INIT;
1086
- if (!parse_object(old_sha1)) {
1087
- old_sha1 = NULL;
1086
+ if (!parse_object(old_oid->hash)) {
1087
+ old_oid = NULL;
1088
if (ref_exists(name)) {
1089
rp_warning("Allowing deletion of corrupt ref.");
1090
} else {
@@ -1094,7 +1094,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1094
}
1095
if (ref_transaction_delete(transaction,
1096
namespaced_name,
1097
- old_sha1,
1097
+ old_oid->hash,
1098
0, "push", &err)) {
1099
rp_error("%s", err.buf);
1100
strbuf_release(&err);
@@ -1111,7 +1111,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1111
1112
if (ref_transaction_update(transaction,
1113
namespaced_name,
1114
- new_sha1, old_sha1,
1114
+ new_oid->hash, old_oid->hash,
1115
0, "push",
1116
&err)) {
1117
rp_error("%s", err.buf);
@@ -1187,8 +1187,8 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
1187
1188
dst_cmd = (struct command *) item->util;
1189
1190
- if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
1191
- !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
1190
+ if (!oidcmp(&cmd->old_oid, &dst_cmd->old_oid) &&
1191
+ !oidcmp(&cmd->new_oid, &dst_cmd->new_oid))
1192
return;
1193
1194
dst_cmd->skip_update = 1;
@@ -1196,11 +1196,11 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
1196
rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
1197
" its target '%s' (%s..%s)",
1198
cmd->ref_name,
1199
- find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV),
1200
- find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV),
1199
+ find_unique_abbrev(cmd->old_oid.hash, DEFAULT_ABBREV),
1200
+ find_unique_abbrev(cmd->new_oid.hash, DEFAULT_ABBREV),
1201
dst_cmd->ref_name,
1202
- find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV),
1203
- find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
1202
+ find_unique_abbrev(dst_cmd->old_oid.hash, DEFAULT_ABBREV),
1203
+ find_unique_abbrev(dst_cmd->new_oid.hash, DEFAULT_ABBREV));
1204
1205
cmd->error_string = dst_cmd->error_string =
1206
"inconsistent aliased update";
@@ -1231,10 +1231,10 @@ static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
1231
struct command **cmd_list = cb_data;
1232
struct command *cmd = *cmd_list;
1233
1234
- if (!cmd || is_null_sha1(cmd->new_sha1))
1234
+ if (!cmd || is_null_oid(&cmd->new_oid))
1235
return -1; /* end of list */
1236
*cmd_list = NULL; /* this returns only one */
1237
- hashcpy(sha1, cmd->new_sha1);
1237
+ hashcpy(sha1, cmd->new_oid.hash);
1238
return 0;
1239
}
1240
@@ -1275,8 +1275,8 @@ static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
1275
if (shallow_update && data->si->shallow_ref[cmd->index])
1276
/* to be checked in update_shallow_ref() */
1277
continue;
1278
- if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
1279
- hashcpy(sha1, cmd->new_sha1);
1278
+ if (!is_null_oid(&cmd->new_oid) && !cmd->skip_update) {
1279
+ hashcpy(sha1, cmd->new_oid.hash);
1280
*cmd_list = cmd->next;
1281
return 0;
1282
}
@@ -1303,7 +1303,7 @@ static void reject_updates_to_hidden(struct command *commands)
1303
1304
if (!ref_is_hidden(cmd->ref_name, refname_full.buf))
1305
continue;
1306
- if (is_null_sha1(cmd->new_sha1))
1306
+ if (is_null_oid(&cmd->new_oid))
1307
cmd->error_string = "deny deleting a hidden ref";
1308
else
1309
cmd->error_string = "deny updating a hidden ref";
@@ -1486,23 +1486,23 @@ static struct command **queue_command(struct command **tail,
1486
const char *line,
1487
int linelen)
1488
{
1489
- unsigned char old_sha1[20], new_sha1[20];
1489
+ struct object_id old_oid, new_oid;
1490
struct command *cmd;
1491
const char *refname;
1492
int reflen;
1493
+ const char *p;
1494
1494
- if (linelen < 83 ||
1495
- line[40] != ' ' ||
1496
- line[81] != ' ' ||
1497
- get_sha1_hex(line, old_sha1) ||
1498
- get_sha1_hex(line + 41, new_sha1))
1495
+ if (parse_oid_hex(line, &old_oid, &p) ||
1496
+ *p++ != ' ' ||
1497
+ parse_oid_hex(p, &new_oid, &p) ||
1498
+ *p++ != ' ')
1499
die("protocol error: expected old/new/ref, got '%s'", line);
1500
1501
- refname = line + 82;
1502
- reflen = linelen - 82;
1501
+ refname = p;
1502
+ reflen = linelen - (p - line);
1503
FLEX_ALLOC_MEM(cmd, ref_name, refname, reflen);
1504
- hashcpy(cmd->old_sha1, old_sha1);
1505
- hashcpy(cmd->new_sha1, new_sha1);
1504
+ oidcpy(&cmd->old_oid, &old_oid);
1505
+ oidcpy(&cmd->new_oid, &new_oid);
1506
*tail = cmd;
1507
return &cmd->next;
1508
}
@@ -1541,12 +1541,12 @@ static struct command *read_head_info(struct sha1_array *shallow)
1541
if (!line)
1542
break;
1543
1544
- if (len == 48 && starts_with(line, "shallow ")) {
1545
- unsigned char sha1[20];
1546
- if (get_sha1_hex(line + 8, sha1))
1544
+ if (len > 8 && starts_with(line, "shallow ")) {
1545
+ struct object_id oid;
1546
+ if (get_oid_hex(line + 8, &oid))
1547
die("protocol error: expected shallow sha, got '%s'",
1548
line + 8);
1549
- sha1_array_append(shallow, sha1);
1549
+ sha1_array_append(shallow, oid.hash);
1550
continue;
1551
}
1552
@@ -1815,9 +1815,9 @@ static void update_shallow_info(struct command *commands,
1815
}
1816
1817
for (cmd = commands; cmd; cmd = cmd->next) {
1818
- if (is_null_sha1(cmd->new_sha1))
1818
+ if (is_null_oid(&cmd->new_oid))
1819
continue;
1820
- sha1_array_append(ref, cmd->new_sha1);
1820
+ sha1_array_append(ref, cmd->new_oid.hash);
1821
cmd->index = ref->nr - 1;
1822
}
1823
si->ref = ref;
@@ -1830,7 +1830,7 @@ static void update_shallow_info(struct command *commands,
1830
ALLOC_ARRAY(ref_status, ref->nr);
1831
assign_shallow_commits_to_refs(si, NULL, ref_status);
1832
for (cmd = commands; cmd; cmd = cmd->next) {
1833
- if (is_null_sha1(cmd->new_sha1))
1833
+ if (is_null_oid(&cmd->new_oid))
1834
continue;
1835
if (ref_status[cmd->index]) {
1836
cmd->error_string = "shallow update not allowed";
@@ -1868,7 +1868,7 @@ static int delete_only(struct command *commands)
1868
{
1869
struct command *cmd;
1870
for (cmd = commands; cmd; cmd = cmd->next) {
1871
- if (!is_null_sha1(cmd->new_sha1))
1871
+ if (!is_null_oid(&cmd->new_oid))
1872
return 0;
1873
}
1874
return 1;