Skip to content

Commit 2848160

Browse files
committed
stash: refactor code that prepares commit messages
1 parent ca2d34a commit 2848160

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/stash.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -437,36 +437,33 @@ static int commit_worktree(
437437
return error;
438438
}
439439

440-
static int prepare_worktree_commit_message(
441-
git_buf* msg,
442-
const char *user_message)
440+
static int prepare_worktree_commit_message(git_buf *out, const char *user_message)
443441
{
444442
git_buf buf = GIT_BUF_INIT;
445-
int error;
446-
447-
if ((error = git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg))) < 0)
448-
return error;
449-
450-
git_buf_clear(msg);
443+
int error = 0;
451444

452-
if (!user_message)
453-
git_buf_printf(msg, "WIP on %s", git_buf_cstr(&buf));
454-
else {
445+
if (!user_message) {
446+
git_buf_printf(&buf, "WIP on %s", git_buf_cstr(out));
447+
} else {
455448
const char *colon;
456449

457-
if ((colon = strchr(git_buf_cstr(&buf), ':')) == NULL)
450+
if ((colon = strchr(git_buf_cstr(out), ':')) == NULL)
458451
goto cleanup;
459452

460-
git_buf_puts(msg, "On ");
461-
git_buf_put(msg, git_buf_cstr(&buf), colon - buf.ptr);
462-
git_buf_printf(msg, ": %s\n", user_message);
453+
git_buf_puts(&buf, "On ");
454+
git_buf_put(&buf, git_buf_cstr(out), colon - out->ptr);
455+
git_buf_printf(&buf, ": %s\n", user_message);
463456
}
464457

465-
error = (git_buf_oom(msg) || git_buf_oom(&buf)) ? -1 : 0;
458+
if (git_buf_oom(&buf)) {
459+
error = -1;
460+
goto cleanup;
461+
}
462+
463+
git_buf_swap(out, &buf);
466464

467465
cleanup:
468466
git_buf_dispose(&buf);
469-
470467
return error;
471468
}
472469

0 commit comments

Comments
 (0)