Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blame: add blame progress callback #6285

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/git2/blame.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ typedef enum {
GIT_BLAME_IGNORE_WHITESPACE = (1<<6)
} git_blame_flag_t;

/**
* Blame progress callback.
*
* Called for each blame suspect.
*
* - 'suspect' is the id of the suspect commit.
*
* Returning a non-zero value will abort the blame.
*/
typedef int (*git_blame_progress_cb)(
const git_oid *suspect,
void *payload);

/**
* Blame options structure
*
Expand Down Expand Up @@ -120,6 +133,9 @@ typedef struct git_blame_options {
* The default is the last line of the file.
*/
size_t max_line;

git_blame_progress_cb progress_cb;
void *payload;
} git_blame_options;

#define GIT_BLAME_OPTIONS_VERSION 1
Expand Down
10 changes: 10 additions & 0 deletions src/libgit2/blame_git.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,16 @@ int git_blame__like_git(git_blame *blame, uint32_t opt)
if (!suspect)
break;

/* Callback to indicate that blame loading is still
* in progress with the ability to abort the loading */
if (blame->options.progress_cb) {
const git_oid *id = git_commit_id(suspect->commit);
if ((error = blame->options.progress_cb(id, blame->options.payload))) {
/* Abort loading */
break;
}
}

/* We'll use this suspect later in the loop, so hold on to it for now. */
origin_incref(suspect);

Expand Down
33 changes: 33 additions & 0 deletions tests/libgit2/blame/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,36 @@ def\n";
check_blame_hunk_index(g_repo, g_bufferblame, 3, 11, 5, 0, "aa06ecca", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 4, 16, 2, 0, "00000000", "b.txt");
}

int blame_progress(const git_oid *suspect, void *payload)
{
static int counter = 0;

counter ++;
cl_assert_equal_i(3.1416, *((double*)payload));

/* abort after the third suspect */
if (counter >= 3)
return -123;

return 0;
}

/*!
* \brief test_blame_buffer__abort_loading_blame
* Abort loading blame after a few suspects
*/
void test_blame_buffer__abort_loading_blame(void)
{
git_blame_options opts;
git_blame_options_init(&opts, GIT_BLAME_OPTIONS_VERSION);

double number = 3.1416;

opts.progress_cb = &blame_progress;
opts.payload = &number;

cl_assert_equal_i(-123, git_blame_file(&g_fileblame, g_repo, "c.txt", &opts));
g_bufferblame = NULL;
}