From: Johan Herland <johan@herland.net>
To: gitster@pobox.com
Cc: git@vger.kernel.org, mackyle@gmail.com, jhf@trifork.com,
sunshine@sunshineco.com, Johan Herland <johan@herland.net>
Subject: [PATCHv4 4/9] builtin/notes: Refactor note file path into struct note_data
Date: Sun, 9 Nov 2014 13:30:50 +0100 [thread overview]
Message-ID: <1415536255-19961-5-git-send-email-johan@herland.net> (raw)
In-Reply-To: <1415536255-19961-1-git-send-email-johan@herland.net>
Move the 'path' variable from create_note() and into the
note_data struct. Unify cleanup of note_data objects with
a free_note_data() function.
This might not make too much sense on its own, but it makes the
future refactoring of create_note() considerably cleaner.
Signed-off-by: Johan Herland <johan@herland.net>
---
builtin/notes.c | 38 +++++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 17 deletions(-)
diff --git a/builtin/notes.c b/builtin/notes.c
index 3cf22cb..1017472 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -95,9 +95,19 @@ static const char note_template[] =
struct note_data {
int given;
int use_editor;
+ char *edit_path;
struct strbuf buf;
};
+static void free_note_data(struct note_data *d)
+{
+ if (d->edit_path) {
+ unlink_or_warn(d->edit_path);
+ free(d->edit_path);
+ }
+ strbuf_release(&d->buf);
+}
+
static int list_each_note(const unsigned char *object_sha1,
const unsigned char *note_sha1, char *note_path,
void *cb_data)
@@ -153,17 +163,15 @@ static void create_note(const unsigned char *object, struct note_data *d,
int append_only, const unsigned char *prev,
unsigned char *result)
{
- char *path = NULL;
-
if (d->use_editor || !d->given) {
int fd;
struct strbuf buf = STRBUF_INIT;
/* write the template message before editing: */
- path = git_pathdup("NOTES_EDITMSG");
- fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
+ d->edit_path = git_pathdup("NOTES_EDITMSG");
+ fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (fd < 0)
- die_errno(_("could not create file '%s'"), path);
+ die_errno(_("could not create file '%s'"), d->edit_path);
if (d->given)
write_or_die(fd, d->buf.buf, d->buf.len);
@@ -181,7 +189,7 @@ static void create_note(const unsigned char *object, struct note_data *d,
strbuf_release(&buf);
strbuf_reset(&d->buf);
- if (launch_editor(path, &d->buf, NULL)) {
+ if (launch_editor(d->edit_path, &d->buf, NULL)) {
die(_("Please supply the note contents using either -m or -F option"));
}
stripspace(&d->buf, 1);
@@ -208,17 +216,12 @@ static void create_note(const unsigned char *object, struct note_data *d,
} else {
if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) {
error(_("unable to write note object"));
- if (path)
+ if (d->edit_path)
error(_("The note contents have been left in %s"),
- path);
+ d->edit_path);
exit(128);
}
}
-
- if (path) {
- unlink_or_warn(path);
- free(path);
- }
}
static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
@@ -402,7 +405,7 @@ static int add(int argc, const char **argv, const char *prefix)
unsigned char object[20], new_note[20];
char logmsg[100];
const unsigned char *note;
- struct note_data d = { 0, 0, STRBUF_INIT };
+ struct note_data d = { 0, 0, NULL, STRBUF_INIT };
struct option options[] = {
{ OPTION_CALLBACK, 'm', "message", &d, N_("message"),
N_("note contents as a string"), PARSE_OPT_NONEG,
@@ -447,6 +450,7 @@ static int add(int argc, const char **argv, const char *prefix)
* therefore still in argv[0-1].
*/
argv[0] = "edit";
+ free_note_data(&d);
free_notes(t);
return append_edit(argc, argv, prefix);
}
@@ -460,6 +464,7 @@ static int add(int argc, const char **argv, const char *prefix)
}
create_note(object, &d, 0, note, new_note);
+ free_note_data(&d);
if (is_null_sha1(new_note))
remove_note(t, object);
@@ -471,7 +476,6 @@ static int add(int argc, const char **argv, const char *prefix)
commit_notes(t, logmsg);
out:
free_notes(t);
- strbuf_release(&d.buf);
return retval;
}
@@ -559,7 +563,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
const unsigned char *note;
char logmsg[100];
const char * const *usage;
- struct note_data d = { 0, 0, STRBUF_INIT };
+ struct note_data d = { 0, 0, NULL, STRBUF_INIT };
struct option options[] = {
{ OPTION_CALLBACK, 'm', "message", &d, N_("message"),
N_("note contents as a string"), PARSE_OPT_NONEG,
@@ -600,6 +604,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
note = get_note(t, object);
create_note(object, &d, !edit, note, new_note);
+ free_note_data(&d);
if (is_null_sha1(new_note))
remove_note(t, object);
@@ -610,7 +615,6 @@ static int append_edit(int argc, const char **argv, const char *prefix)
is_null_sha1(new_note) ? "removed" : "added", argv[0]);
commit_notes(t, logmsg);
free_notes(t);
- strbuf_release(&d.buf);
return 0;
}
--
2.1.1.392.g062cc5d
next prev parent reply other threads:[~2014-11-09 12:31 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-09 12:30 [PATCHv4 0/9] Handling empty notes Johan Herland
2014-11-09 12:30 ` [PATCHv4 1/9] builtin/notes: Fix premature failure when trying to add the empty blob Johan Herland
2014-11-09 12:30 ` [PATCHv4 2/9] t3301: Verify that 'git notes' removes empty notes by default Johan Herland
2014-11-09 12:30 ` [PATCHv4 3/9] builtin/notes: Improve naming Johan Herland
2014-11-09 12:30 ` Johan Herland [this message]
2014-11-09 12:30 ` [PATCHv4 5/9] builtin/notes: Simplify early exit code in add() Johan Herland
2014-11-10 20:36 ` Junio C Hamano
2014-11-11 0:49 ` Johan Herland
2014-11-11 15:53 ` Junio C Hamano
2014-11-09 12:30 ` [PATCHv4 6/9] builtin/notes: Split create_note() to clarify add vs. remove logic Johan Herland
2014-11-09 12:30 ` [PATCHv4 7/9] builtin/notes: Add --allow-empty, to allow storing empty notes Johan Herland
2014-11-09 12:30 ` [PATCHv4 8/9] notes: Empty notes should be shown by 'git log' Johan Herland
2014-11-09 12:30 ` [PATCHv4 9/9] t3301: Modernize Johan Herland
2014-11-10 20:42 ` Junio C Hamano
2014-11-11 1:04 ` Johan Herland
2014-11-11 1:07 ` Eric Sunshine
2014-11-11 1:50 ` Johan Herland
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1415536255-19961-5-git-send-email-johan@herland.net \
--to=johan@herland.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jhf@trifork.com \
--cc=mackyle@gmail.com \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).