8000 Hook support by tiennou · Pull Request #4620 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

Hook support #4620

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
hooks: wire up pre-rebase
  • Loading branch information
tiennou committed Sep 10, 2020
commit f4aeca4ebf3ad7e4200f9f193d9dd274bdee846b
5 changes: 5 additions & 0 deletions include/git2/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ GIT_EXTERN(int) git_hook_register_callback(
git_hook_destructor_cb destructor,
void *payload);

GIT_EXTERN(int) git_hook_call_pre_rebase(
git_repository *repo,
const git_annotated_commit *upstream,
const git_annotated_commit *rebased);

/* @} */
GIT_END_DECL
#endif
23 changes: 23 additions & 0 deletions src/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "posix.h"
#include "repository.h"
#include "annotated_commit.h 8000 "

#include "git2/hook.h"
#include "git2/sys/hook.h"
Expand Down Expand Up @@ -240,3 +241,25 @@ int git_hook_execute_io(git_buf *io, git_repository *repo, const char *hook_name

return err;
}

/** High-level hook calls */

int git_hook_call_pre_rebase(git_repository *repo, const git_annotated_commit *upstream, const git_annotated_commit *rebased)
{
int err = 0;
const char *rebased_name;

assert(repo);

if (upstream && !upstream->ref_name)
return 0;

if (rebased && !rebased->ref_name)
return 0;

rebased_name = (rebased ? rebased->ref_name : "");

err = git_hook_execute(repo, "pre-rebase", upstream->ref_name, rebased_name, NULL);

return err;
}
6 changes: 6 additions & 0 deletions src/rebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "config.h"
#include "annotated_commit.h"
#include "index.h"
#include <git2/sys/hook.h>

#include <git2/types.h>
#include <git2/annotated_commit.h>
Expand Down Expand Up @@ -726,6 +727,11 @@ int git_rebase_init(
branch = head_branch;
}

if (!inmemory && upstream) {
if ((error = git_hook_call_pre_rebase(repo, upstream, (head_ref == NULL ? branch : NULL))) < 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to check for < 0 here? Usually, return codes from exec'd programs are always positive in the range from 0 to 255, where non-zero positive values denote an error. So in case the callback simply exec's the hook and returns its value, we will not detect that error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm defaulting to the libgit2 convention. Right now the expectation is that the executor will bubble a GIT_* error, not the underlying exec return value.

goto done;
}

if (rebase_alloc(&rebase, given_opts) < 0)
return -1;

Expand Down
71 changes: 71 additions & 0 deletions tests/hook/call.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "clar_libgit2.h"
#include "git2/sys/hook.h"

static git_repository *g_repo = NULL;
static git_signature *signature;

void test_hook_call__initialize(void)
{
g_repo = cl_git_sandbox_init("rebase");

cl_git_pass(git_signature_new(&signature,
"Rebaser", "rebaser@rebaser.rb", 1405694510, 0));
}

void test_hook_call__cleanup(void)
{
cl_git_sandbox_cleanup();
git_signature_free(signature);
}

static void make_dummy_hook(const char *hook_name)
{
git_buf hook_path = GIT_BUF_INIT;

cl_git_pass(git_hook_dir(&hook_path 8000 , g_repo));

cl_must_pass(p_mkdir(git_buf_cstr(&hook_path), 0777));

git_buf_joinpath(&hook_path, hook_path.ptr, hook_name);

cl_git_mkfile(git_buf_cstr(&hook_path), NULL);
cl_must_pass(p_chmod(git_buf_cstr(&hook_path), 0776));

git_buf_dispose(&hook_path);
}

int hook_exec__pre_rebase(git_hook_env *env, void *payload)
{
GIT_UNUSED(payload);

if (strstr(env->args.strings[0], "master") != NULL) {
return -1;
}

return 0;
}

void test_hook_call__pre_rebase_hook(void)
{
git_rebase *rebase;
git_reference *branch_ref, *upstream_ref;
git_annotated_commit *branch_head, *upstream_head;

make_dummy_hook("pre-rebase");

cl_git_pass(git_hook_register_callback(g_repo, hook_exec__pre_rebase, NULL, NULL));

cl_git_pass(git_reference_lookup(&branch_ref, g_repo, "refs/heads/beef"));
cl_git_pass(git_reference_lookup(&upstream_ref, g_repo, "refs/heads/master"));

cl_git_pass(git_annotated_commit_from_ref(&branch_head, g_repo, branch_ref));
cl_git_pass(git_annotated_commit_from_ref(&upstream_head, g_repo, upstream_ref));

cl_git_fail_with(git_rebase_init(&rebase, g_repo, branch_head, upstream_head, NULL, NULL), -1);

git_annotated_commit_free(branch_head);
git_annotated_commit_free(upstream_head);
git_reference_free(branch_ref);
git_reference_free(upstream_ref);
git_rebase_free(rebase);
}
0