Skip to content

Commit 49697cb

Browse files
phillipwoodgitster
authored andcommitted
move run_commit_hook() to libgit and use it there
This function was declared in commit.h but was implemented in builtin/commit.c so was not part of libgit. Move it to libgit so we can use it in the sequencer. This simplifies the implementation of run_prepare_commit_msg_hook() and will be used in the next commit. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 12bb7a5 commit 49697cb

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed

builtin/commit.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,28 +1443,6 @@ static int git_commit_config(const char *k, const char *v, void *cb)
14431443
return git_status_config(k, v, s);
14441444
}
14451445

1446-
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
1447-
{
1448-
struct argv_array hook_env = ARGV_ARRAY_INIT;
1449-
va_list args;
1450-
int ret;
1451-
1452-
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
1453-
1454-
/*
1455-
* Let the hook know that no editor will be launched.
1456-
*/
1457-
if (!editor_is_used)
1458-
argv_array_push(&hook_env, "GIT_EDITOR=:");
1459-
1460-
va_start(args, name);
1461-
ret = run_hook_ve(hook_env.argv,name, args);
1462-
va_end(args);
1463-
argv_array_clear(&hook_env);
1464-
1465-
return ret;
1466-
}
1467-
14681446
int cmd_commit(int argc, const char **argv, const char *prefix)
14691447
{
14701448
const char *argv_gc_auto[] = {"gc", "--auto", NULL};

commit.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "advice.h"
2020
#include "refs.h"
2121
#include "commit-reach.h"
22+
#include "run-command.h"
2223

2324
static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
2425

@@ -1581,3 +1582,26 @@ size_t ignore_non_trailer(const char *buf, size_t len)
15811582
}
15821583
return boc ? len - boc : len - cutoff;
15831584
}
1585+
1586+
int run_commit_hook(int editor_is_used, const char *index_file,
1587+
const char *name, ...)
1588+
{
1589+
struct argv_array hook_env = ARGV_ARRAY_INIT;
1590+
va_list args;
1591+
int ret;
1592+
1593+
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
1594+
1595+
/*
1596+
* Let the hook know that no editor will be launched.
1597+
*/
1598+
if (!editor_is_used)
1599+
argv_array_push(&hook_env, "GIT_EDITOR=:");
1600+
1601+
va_start(args, name);
1602+
ret = run_hook_ve(hook_env.argv,name, args);
1603+
va_end(args);
1604+
argv_array_clear(&hook_env);
1605+
1606+
return ret;
1607+
}

sequencer.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,25 +1127,22 @@ static int run_prepare_commit_msg_hook(struct repository *r,
11271127
struct strbuf *msg,
11281128
const char *commit)
11291129
{
1130-
struct argv_array hook_env = ARGV_ARRAY_INIT;
1131-
int ret;
1132-
const char *name;
1130+
int ret = 0;
1131+
const char *name, *arg1 = NULL, *arg2 = NULL;
11331132

11341133
name = git_path_commit_editmsg();
11351134
if (write_message(msg->buf, msg->len, name, 0))
11361135
return -1;
11371136

1138-
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", r->index_file);
1139-
argv_array_push(&hook_env, "GIT_EDITOR=:");
1140-
if (commit)
1141-
ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name,
1142-
"commit", commit, NULL);
1143-
else
1144-
ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name,
1145-
"message", NULL);
1146-
if (ret)
1137+
if (commit) {
1138+
arg1 = "commit";
1139+
arg2 = commit;
1140+
} else {
1141+
arg1 = "message";
1142+
}
1143+
if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name,
1144+
arg1, arg2, NULL))
11471145
ret = error(_("'prepare-commit-msg' hook failed"));
1148-
argv_array_clear(&hook_env);
11491146

11501147
return ret;
11511148
}

0 commit comments

Comments
 (0)