submodule--helper: prefer strip_suffix() to ends_with()

Using strip_suffix() lets us avoid repeating ourselves. It also makes the handling of "/" a bit less subtle (we strip one less character than we matched in order to leave it in place, but we can just as easily include the "/" when we add more path components). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 12, 2018 at 09:47 UTC b2ac148faecc95243ed8da0b75f63966d828c802
1 file changed +4 -3
builtin/submodule--helper.c
+4 -3
@@ -1268,16 +1268,17 @@ static int add_possible_reference_from_superproject(
1268 struct alternate_object_database *alt, void *sas_cb)
1269 {
1270 struct submodule_alternate_setup *sas = sas_cb;
1271 + size_t len;
1272
1273 /*
1274 * If the alternate object store is another repository, try the
1275 * standard layout with .git/(modules/<name>)+/objects
1276 */
1276 - if (ends_with(alt->path, "/objects")) {
1277 + if (strip_suffix(alt->path, "/objects", &len)) {
1278 char *sm_alternate;
1279 struct strbuf sb = STRBUF_INIT;
1280 struct strbuf err = STRBUF_INIT;
1280 - strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
1281 + strbuf_add(&sb, alt->path, len);
1282
1283 /*
1284 * We need to end the new path with '/' to mark it as a dir,
@@ -1285,7 +1286,7 @@ static int add_possible_reference_from_superproject(
1286 * as the last part of a missing submodule reference would
1287 * be taken as a file name.
1288 */
1288 - strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
1289 + strbuf_addf(&sb, "/modules/%s/", sas->submodule_name);
1290
1291 sm_alternate = compute_alternate_path(sb.buf, &err);
1292 if (sm_alternate) {