Skip to content

Commit 0bf6723

Browse files
authored
Merge pull request libgit2#6158 from arroz/feat/statusRenameThreshold
Add `rename_threshold` to `git_status_options`.
2 parents 82f526a + ef84889 commit 0bf6723

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

include/git2/status.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ typedef struct {
250250
* working directory and index; defaults to HEAD.
251251
*/
252252
git_tree *baseline;
253+
254+
/**
255+
* Threshold above which similar files will be considered renames.
256+
* This is equivalent to the -M option. Defaults to 50.
257+
*/
258+
uint16_t rename_threshold;
253259
} git_status_options;
254260

255261
#define GIT_STATUS_OPTIONS_VERSION 1

src/status.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ int git_status_list_new(
336336
GIT_DIFF_FIND_RENAMES_FROM_REWRITES |
337337
GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY;
338338

339+
if (opts != NULL && opts->rename_threshold != 0)
340+
findopt.rename_threshold = opts->rename_threshold;
341+
339342
if (show != GIT_STATUS_SHOW_WORKDIR_ONLY) {
340343
if ((error = git_diff_tree_to_index(
341344
&status->head2idx, repo, head, index, &diffopt)) < 0)

tests/status/renames.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,3 +718,66 @@ void test_status_renames__precomposed_unicode_toggle_is_rename(void)
718718
#endif
719719
}
720720

721+
void test_status_renames__rename_threshold(void)
722+
{
723+
git_index *index;
724+
git_status_list *statuslist;
725+
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
726+
727+
_rename_helper(g_repo, "ikeepsix.txt", "newname.txt",
728+
"Line 1\n" \
729+
"Line 2\n" \
730+
"Line 3\n" \
731+
"Line 4\n" \
732+
"Line 5\n" \
733+
"Line 6\n" \
734+
"Line 7\n" \
735+
"Line 8\n" \
736+
"Line 9\n"
737+
);
738+
739+
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
740+
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
741+
742+
cl_git_pass(git_repository_index(&index, g_repo));
743+
744+
// Default threshold
745+
{
746+
struct status_entry expected[] = {
747+
{ GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED, "ikeepsix.txt", "newname.txt" },
748+
};
749+
750+
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
751+
check_status(statuslist, expected, 1);
752+
git_status_list_free(statuslist);
753+
}
754+
755+
// Threshold set to 90
756+
{
757+
struct status_entry expected[] = {
758+
{ GIT_STATUS_WT_DELETED, "ikeepsix.txt", NULL },
759+
{ GIT_STATUS_WT_NEW, "newname.txt", NULL }
760+
};
761+
762+
opts.rename_threshold = 90;
763+
764+
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
765+
check_status(statuslist, expected, 2);
766+
git_status_list_free(statuslist);
767+
}
768+
769+
// Threshold set to 25
770+
{
771+
struct status_entry expected[] = {
772+
{ GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED, "ikeepsix.txt", "newname.txt" },
773+
};
774+
775+
opts.rename_threshold = 25;
776+
777+
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
778+
check_status(statuslist, expected, 1);
779+
git_status_list_free(statuslist);
780+
}
781+
782+
git_index_free(index);
783+
}

0 commit comments

Comments
 (0)