Skip to content

Commit cc8364c

Browse files
committed
Merge branch 'ep/commit-static-buf-cleanup'
Code clean-up. * ep/commit-static-buf-cleanup: builtin/commit.c: switch to strbuf, instead of snprintf() builtin/commit.c: remove the PATH_MAX limitation via dynamic allocation
2 parents a77fe4a + 4a52819 commit cc8364c

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

builtin/commit.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -960,15 +960,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
960960
return 0;
961961

962962
if (use_editor) {
963-
char index[PATH_MAX];
964-
const char *env[2] = { NULL };
965-
env[0] = index;
966-
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
967-
if (launch_editor(git_path_commit_editmsg(), NULL, env)) {
963+
struct argv_array env = ARGV_ARRAY_INIT;
964+
965+
argv_array_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
966+
if (launch_editor(git_path_commit_editmsg(), NULL, env.argv)) {
968967
fprintf(stderr,
969968
_("Please supply the message using either -m or -F option.\n"));
970969
exit(1);
971970
}
971+
argv_array_clear(&env);
972972
}
973973

974974
if (!no_verify &&
@@ -1525,12 +1525,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
15251525
static int run_rewrite_hook(const unsigned char *oldsha1,
15261526
const unsigned char *newsha1)
15271527
{
1528-
/* oldsha1 SP newsha1 LF NUL */
1529-
static char buf[2*40 + 3];
15301528
struct child_process proc = CHILD_PROCESS_INIT;
15311529
const char *argv[3];
15321530
int code;
1533-
size_t n;
1531+
struct strbuf sb = STRBUF_INIT;
15341532

15351533
argv[0] = find_hook("post-rewrite");
15361534
if (!argv[0])
@@ -1546,34 +1544,33 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
15461544
code = start_command(&proc);
15471545
if (code)
15481546
return code;
1549-
n = snprintf(buf, sizeof(buf), "%s %s\n",
1550-
sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1547+
strbuf_addf(&sb, "%s %s\n", sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
15511548
sigchain_push(SIGPIPE, SIG_IGN);
1552-
write_in_full(proc.in, buf, n);
1549+
write_in_full(proc.in, sb.buf, sb.len);
15531550
close(proc.in);
1551+
strbuf_release(&sb);
15541552
sigchain_pop(SIGPIPE);
15551553
return finish_command(&proc);
15561554
}
15571555

15581556
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
15591557
{
1560-
const char *hook_env[3] = { NULL };
1561-
char index[PATH_MAX];
1558+
struct argv_array hook_env = ARGV_ARRAY_INIT;
15621559
va_list args;
15631560
int ret;
15641561

1565-
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
1566-
hook_env[0] = index;
1562+
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
15671563

15681564
/*
15691565
* Let the hook know that no editor will be launched.
15701566
*/
15711567
if (!editor_is_used)
1572-
hook_env[1] = "GIT_EDITOR=:";
1568+
argv_array_push(&hook_env, "GIT_EDITOR=:");
15731569

15741570
va_start(args, name);
1575-
ret = run_hook_ve(hook_env, name, args);
1571+
ret = run_hook_ve(hook_env.argv,name, args);
15761572
va_end(args);
1573+
argv_array_clear(&hook_env);
15771574

15781575
return ret;
15791576
}

0 commit comments

Comments
 (0)