Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D98EC433DB for ; Mon, 4 Jan 2021 23:51:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 02CC22253D for ; Mon, 4 Jan 2021 23:51:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727878AbhADXvQ (ORCPT ); Mon, 4 Jan 2021 18:51:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727826AbhADXvQ (ORCPT ); Mon, 4 Jan 2021 18:51:16 -0500 Received: from mail-ot1-x32b.google.com (mail-ot1-x32b.google.com [IPv6:2607:f8b0:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BDBCC06179A for ; Mon, 4 Jan 2021 15:50:36 -0800 (PST) Received: by mail-ot1-x32b.google.com with SMTP id j20so27739443otq.5 for ; Mon, 04 Jan 2021 15:50:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=D3/K76RpxK95m/dgFzqFg6bo0ITTMqrb4v0r2gO+Kok=; b=fkYeQI6Cd+3tCaGuu5vsBvLgA6HX2A8fJBNIuW4kHAqefrw0ZOXiR22IEcmC/WZ9Va ZVAHZr0XM/7iurzVjWQgZ+2vD894GZV0xtOO0kU1CJsnot7VGPjn3o2LNxiVSwBqWkYO 77ddt0aK5yYpvLcmh6qt0Zr9cQiuOFelsn2ubooMYxJCen5iXUtU19zB9a5Iotp58p4Z bB9ITIHdbX7pYsjn9KVVSSxaGYEbRLhV3cgvUlGX8UkmRAzEvuBek9CCpA1JIMyiww/W U/8lqq237K3o0kQ73mYGZhespsrP15IsCPEtJ8cNJ9TIqTneTpKSgrF9YQAjPwb6i6V1 tWkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=D3/K76RpxK95m/dgFzqFg6bo0ITTMqrb4v0r2gO+Kok=; b=SZ+pcLN/VaQ/o9zMXP8AE/26lyyzZTM3FNtLvWxHppxPqNKtWeAmgxn2qdYCliZSWT VfbmZ3Cumsn00pVWsBSma9tjn/88M5yH3JNwNTsl9d2/3JBJcYaeOgBHnH/wtpKCsNxF lSygVnLL3tS2niRCnRNKkJJrZspYZICYDcoPbzVEon2Okrv37rDpBYqKZ1Unzydm622J 2k2R51b1w6b5yXwMqLz+gtzYGYWILTCREmv6x4XGybgq83O6usbs7RBkJnCkjzJBEYve +Pc506mAwmjX3VfjknscbtLdCjgpDxVHWaVeuD6l9yU2XIJ/d7koYDcvBQvBx8o51Aj/ 8voA== X-Gm-Message-State: AOAM532PA27op5HvrNozRDRcduFUpY0P36POiksiv7ztSqGYVbo+KAwm zALpOilj5wLommonCIr7lpb6fmxP/zs= X-Google-Smtp-Source: ABdhPJxTMeoyxnKPAX2NQ2M0bSBFZ+qnVG0UpjQGff7tlTlMVU8n33huBt5oxVxtgd3duEVS/cdXcg== X-Received: by 2002:a9d:508:: with SMTP id 8mr54538827otw.338.1609804235419; Mon, 04 Jan 2021 15:50:35 -0800 (PST) Received: from tiger.attlocal.net ([2600:1700:bae0:2de0::26]) by smtp.gmail.com with ESMTPSA id o135sm13359600ooo.38.2021.01.04.15.50.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jan 2021 15:50:35 -0800 (PST) From: Elijah Newren To: git@vger.kernel.org Cc: gitster@pobox.com, Elijah Newren Subject: [PATCH 06/17] merge-ort: copy get_renamed_dir_portion() from merge-recursive.c Date: Mon, 4 Jan 2021 15:49:55 -0800 Message-Id: <20210104235006.2867404-7-newren@gmail.com> X-Mailer: git-send-email 2.29.1.106.g3ff750dc32.dirty In-Reply-To: <20210104235006.2867404-1-newren@gmail.com> References: <20210104235006.2867404-1-newren@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Signed-off-by: Elijah Newren --- merge-ort.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/merge-ort.c b/merge-ort.c index 6ccc7a1a45..19c3f8d41d 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -1139,6 +1139,110 @@ static int handle_content_merge(struct merge_options *opt, /*** Function Grouping: functions related to directory rename detection ***/ +MAYBE_UNUSED +static void get_renamed_dir_portion(const char *old_path, const char *new_path, + char **old_dir, char **new_dir) +{ + char *end_of_old, *end_of_new; + + /* Default return values: NULL, meaning no rename */ + *old_dir = NULL; + *new_dir = NULL; + + /* + * For + * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c" + * the "e/foo.c" part is the same, we just want to know that + * "a/b/c/d" was renamed to "a/b/some/thing/else" + * so, for this example, this function returns "a/b/c/d" in + * *old_dir and "a/b/some/thing/else" in *new_dir. + */ + + /* + * If the basename of the file changed, we don't care. We want + * to know which portion of the directory, if any, changed. + */ + end_of_old = strrchr(old_path, '/'); + end_of_new = strrchr(new_path, '/'); + + /* + * If end_of_old is NULL, old_path wasn't in a directory, so there + * could not be a directory rename (our rule elsewhere that a + * directory which still exists is not considered to have been + * renamed means the root directory can never be renamed -- because + * the root directory always exists). + */ + if (end_of_old == NULL) + return; /* Note: *old_dir and *new_dir are still NULL */ + + /* + * If new_path contains no directory (end_of_new is NULL), then we + * have a rename of old_path's directory to the root directory. + */ + if (end_of_new == NULL) { + *old_dir = xstrndup(old_path, end_of_old - old_path); + *new_dir = xstrdup(""); + return; + } + + /* Find the first non-matching character traversing backwards */ + while (*--end_of_new == *--end_of_old && + end_of_old != old_path && + end_of_new != new_path) + ; /* Do nothing; all in the while loop */ + + /* + * If both got back to the beginning of their strings, then the + * directory didn't change at all, only the basename did. + */ + if (end_of_old == old_path && end_of_new == new_path && + *end_of_old == *end_of_new) + return; /* Note: *old_dir and *new_dir are still NULL */ + + /* + * If end_of_new got back to the beginning of its string, and + * end_of_old got back to the beginning of some subdirectory, then + * we have a rename/merge of a subdirectory into the root, which + * needs slightly special handling. + * + * Note: There is no need to consider the opposite case, with a + * rename/merge of the root directory into some subdirectory + * because as noted above the root directory always exists so it + * cannot be considered to be renamed. + */ + if (end_of_new == new_path && + end_of_old != old_path && end_of_old[-1] == '/') { + *old_dir = xstrndup(old_path, --end_of_old - old_path); + *new_dir = xstrdup(""); + return; + } + + /* + * We've found the first non-matching character in the directory + * paths. That means the current characters we were looking at + * were part of the first non-matching subdir name going back from + * the end of the strings. Get the whole name by advancing both + * end_of_old and end_of_new to the NEXT '/' character. That will + * represent the entire directory rename. + * + * The reason for the increment is cases like + * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c + * After dropping the basename and going back to the first + * non-matching character, we're now comparing: + * a/b/s and a/b/ + * and we want to be comparing: + * a/b/star/ and a/b/tar/ + * but without the pre-increment, the one on the right would stay + * a/b/. + */ + end_of_old = strchr(++end_of_old, '/'); + end_of_new = strchr(++end_of_new, '/'); + + /* Copy the old and new directories into *old_dir and *new_dir. */ + *old_dir = xstrndup(old_path, end_of_old - old_path); + *new_dir = xstrndup(new_path, end_of_new - new_path); +} + static void compute_rename_counts(struct diff_queue_struct *pairs, struct strmap *dir_rename_count, struct strset *dirs_removed) -- 2.29.1.106.g3ff750dc32.dirty