stash: refactor code that prepares commit messages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
diff --git a/src/stash.c b/src/stash.c
index d619045..4a13d05 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -437,36 +437,33 @@ cleanup:
return error;
}
-static int prepare_worktree_commit_message(
- git_buf* msg,
- const char *user_message)
+static int prepare_worktree_commit_message(git_buf *out, const char *user_message)
{
git_buf buf = GIT_BUF_INIT;
- int error;
-
- if ((error = git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg))) < 0)
- return error;
-
- git_buf_clear(msg);
+ int error = 0;
- if (!user_message)
- git_buf_printf(msg, "WIP on %s", git_buf_cstr(&buf));
- else {
+ if (!user_message) {
+ git_buf_printf(&buf, "WIP on %s", git_buf_cstr(out));
+ } else {
const char *colon;
- if ((colon = strchr(git_buf_cstr(&buf), ':')) == NULL)
+ if ((colon = strchr(git_buf_cstr(out), ':')) == NULL)
goto cleanup;
- git_buf_puts(msg, "On ");
- git_buf_put(msg, git_buf_cstr(&buf), colon - buf.ptr);
- git_buf_printf(msg, ": %s\n", user_message);
+ git_buf_puts(&buf, "On ");
+ git_buf_put(&buf, git_buf_cstr(out), colon - out->ptr);
+ git_buf_printf(&buf, ": %s\n", user_message);
}
- error = (git_buf_oom(msg) || git_buf_oom(&buf)) ? -1 : 0;
+ if (git_buf_oom(&buf)) {
+ error = -1;
+ goto cleanup;
+ }
+
+ git_buf_swap(out, &buf);
cleanup:
git_buf_dispose(&buf);
-
return error;
}