1022
return -1;
1023
}
1024
1025
-static const char *stop_at_slash(const char *line, int llen)
1025
+/*
1026
+ * Skip p_value leading components from "line"; as we do not accept
1027
+ * absolute paths, return NULL in that case.
1028
+ */
1029
+static const char *skip_tree_prefix(const char *line, int llen)
1030
{
1027
- int nslash = p_value;
1031
+ int nslash;
1032
int i;
1033
1034
+ if (!p_value)
1035
+ return (llen && line[0] == '/') ? NULL : line;
1036
+
1037
+ nslash = p_value;
1038
for (i = 0; i < llen; i++) {
1039
int ch = line[i];
1040
if (ch == '/' && --nslash <= 0)
1033
- return &line[i];
1041
+ return (i == 0) ? NULL : &line[i + 1];
1042
}
1043
return NULL;
1044
}
1068
if (unquote_c_style(&first, line, &second))
1069
goto free_and_fail1;
1070
1063
- /* advance to the first slash */
1064
- cp = stop_at_slash(first.buf, first.len);
1065
- /* we do not accept absolute paths */
1066
- if (!cp || cp == first.buf)
1071
+ /* strip the a/b prefix including trailing slash */
1072
+ cp = skip_tree_prefix(first.buf, first.len);
1073
+ if (!cp)
1074
goto free_and_fail1;
1068
- strbuf_remove(&first, 0, cp + 1 - first.buf);
1075
+ strbuf_remove(&first, 0, cp - first.buf);
1076
1077
/*
1078
* second points at one past closing dq of name.
1086
if (*second == '"') {
1087
if (unquote_c_style(&sp, second, NULL))
1088
goto free_and_fail1;
1082
- cp = stop_at_slash(sp.buf, sp.len);
1083
- if (!cp || cp == sp.buf)
1089
+ cp = skip_tree_prefix(sp.buf, sp.len);
1090
+ if (!cp)
1091
goto free_and_fail1;
1092
/* They must match, otherwise ignore */
1086
- if (strcmp(cp + 1, first.buf))
1093
+ if (strcmp(cp, first.buf))
1094
goto free_and_fail1;
1095
strbuf_release(&sp);
1096
return strbuf_detach(&first, NULL);
1097
}
1098
1099
/* unquoted second */
1093
- cp = stop_at_slash(second, line + llen - second);
1094
- if (!cp || cp == second)
1100
+ cp = skip_tree_prefix(second, line + llen - second);
1101
+ if (!cp)
1102
goto free_and_fail1;
1096
- cp++;
1097
- if (line + llen - cp != first.len + 1 ||
1103
+ if (line + llen - cp != first.len ||
1104
memcmp(first.buf, cp, first.len))
1105
goto free_and_fail1;
1106
return strbuf_detach(&first, NULL);
1112
}
1113
1114
/* unquoted first name */
1109
- name = stop_at_slash(line, llen);
1110
- if (!name || name == line)
1115
+ name = skip_tree_prefix(line, llen);
1116
+ if (!name)
1117
return NULL;
1112
- name++;
1118
1119
/*
1120
* since the first name is unquoted, a dq if exists must be
1128
if (unquote_c_style(&sp, second, NULL))
1129
goto free_and_fail2;
1130
1126
- np = stop_at_slash(sp.buf, sp.len);
1127
- if (!np || np == sp.buf)
1131
+ np = skip_tree_prefix(sp.buf, sp.len);
1132
+ if (!np)
1133
goto free_and_fail2;
1129
- np++;
1134
1135
len = sp.buf + sp.len - np;
1136
if (len < second - name &&
1162
case '\n':
1163
return NULL;
1164
case '\t': case ' ':
1161
- second = stop_at_slash(name + len, line_len - len);
1165
+ /*
1166
+ * Is this the separator between the preimage
1167
+ * and the postimage pathname? Again, we are
1168
+ * only interested in the case where there is
1169
+ * no rename, as this is only to set def_name
1170
+ * and a rename patch has the names elsewhere
1171
+ * in an unambiguous form.
1172
+ */
1173
+ if (!name[len + 1])
1174
+ return NULL; /* no postimage name */
1175
+ second = skip_tree_prefix(name + len + 1,
1176
+ line_len - (len + 1));
1177
if (!second)
1178
return NULL;
1164
- second++;
1165
- if (second[len] == '\n' && !strncmp(name, second, len)) {
1179
+ /*
1180
+ * Does len bytes starting at "name" and "second"
1181
+ * (that are separated by one HT or SP we just
1182
+ * found) exactly match?
1183
+ */
1184
+ if (second[len] == '\n' && !strncmp(name, second, len))
1185
return xmemdupz(name, len);
1167
- }
1186
}
1187
}
1188
}