diff --git a/builtin-tar-tree.c b/builtin-tar-tree.c
index 5f740cf702..7663b9bd8e 100644
--- a/builtin-tar-tree.c
+++ b/builtin-tar-tree.c
@@ -168,8 +168,9 @@ static int get_path_prefix(const struct strbuf *path, int maxlen)
 	int i = path->len;
 	if (i > maxlen)
 		i = maxlen;
-	while (i > 0 && path->buf[i] != '/')
+	do {
 		i--;
+	} while (i > 0 && path->buf[i] != '/');
 	return i;
 }