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 3/9] builtin/notes: Improve naming
Date: Sun, 9 Nov 2014 13:30:49 +0100 [thread overview]
Message-ID: <1415536255-19961-4-git-send-email-johan@herland.net> (raw)
In-Reply-To: <1415536255-19961-1-git-send-email-johan@herland.net>
In preparation for some needed refactoring, rename struct msg_arg to
struct note_data, and rename its instances from "msg" to "d" (also
removing some unnecessary parentheses). The 'msg_arg' name was
inherited from tag.c, but is not really a good name for the contents
of a note.
Also rename write_note_data() to copy_obj_to_fd(), which more aptly
describes what it actually does: Copying the contents of a git object
(given by its SHA1) into a given file descriptor.
Signed-off-by: Johan Herland <johan@herland.net>
---
builtin/notes.c | 109 ++++++++++++++++++++++++++++----------------------------
1 file changed, 54 insertions(+), 55 deletions(-)
diff --git a/builtin/notes.c b/builtin/notes.c
index 9ee6816..3cf22cb 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -92,7 +92,7 @@ static const char * const git_notes_get_ref_usage[] = {
static const char note_template[] =
"\nWrite/edit the notes for the following object:\n";
-struct msg_arg {
+struct note_data {
int given;
int use_editor;
struct strbuf buf;
@@ -106,7 +106,7 @@ static int list_each_note(const unsigned char *object_sha1,
return 0;
}
-static void write_note_data(int fd, const unsigned char *sha1)
+static void copy_obj_to_fd(int fd, const unsigned char *sha1)
{
unsigned long size;
enum object_type type;
@@ -149,13 +149,13 @@ static void write_commented_object(int fd, const unsigned char *object)
sha1_to_hex(object));
}
-static void create_note(const unsigned char *object, struct msg_arg *msg,
+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 (msg->use_editor || !msg->given) {
+ if (d->use_editor || !d->given) {
int fd;
struct strbuf buf = STRBUF_INIT;
@@ -165,10 +165,10 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
if (fd < 0)
die_errno(_("could not create file '%s'"), path);
- if (msg->given)
- write_or_die(fd, msg->buf.buf, msg->buf.len);
+ if (d->given)
+ write_or_die(fd, d->buf.buf, d->buf.len);
else if (prev && !append_only)
- write_note_data(fd, prev);
+ copy_obj_to_fd(fd, prev);
strbuf_addch(&buf, '\n');
strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
@@ -179,13 +179,12 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
close(fd);
strbuf_release(&buf);
- strbuf_reset(&(msg->buf));
+ strbuf_reset(&d->buf);
- if (launch_editor(path, &(msg->buf), NULL)) {
- die(_("Please supply the note contents using either -m" \
- " or -F option"));
+ if (launch_editor(path, &d->buf, NULL)) {
+ die(_("Please supply the note contents using either -m or -F option"));
}
- stripspace(&(msg->buf), 1);
+ stripspace(&d->buf, 1);
}
if (prev && append_only) {
@@ -194,20 +193,20 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
enum object_type type;
char *prev_buf = read_sha1_file(prev, &type, &size);
- strbuf_grow(&(msg->buf), size + 1);
- if (msg->buf.len && prev_buf && size)
- strbuf_insert(&(msg->buf), 0, "\n", 1);
+ strbuf_grow(&d->buf, size + 1);
+ if (d->buf.len && prev_buf && size)
+ strbuf_insert(&d->buf, 0, "\n", 1);
if (prev_buf && size)
- strbuf_insert(&(msg->buf), 0, prev_buf, size);
+ strbuf_insert(&d->buf, 0, prev_buf, size);
free(prev_buf);
}
- if (!msg->buf.len) {
+ if (!d->buf.len) {
fprintf(stderr, _("Removing note for object %s\n"),
sha1_to_hex(object));
hashclr(result);
} else {
- if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
+ if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) {
error(_("unable to write note object"));
if (path)
error(_("The note contents have been left in %s"),
@@ -224,45 +223,45 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
{
- struct msg_arg *msg = opt->value;
+ struct note_data *d = opt->value;
- strbuf_grow(&(msg->buf), strlen(arg) + 2);
- if (msg->buf.len)
- strbuf_addch(&(msg->buf), '\n');
- strbuf_addstr(&(msg->buf), arg);
- stripspace(&(msg->buf), 0);
+ strbuf_grow(&d->buf, strlen(arg) + 2);
+ if (d->buf.len)
+ strbuf_addch(&d->buf, '\n');
+ strbuf_addstr(&d->buf, arg);
+ stripspace(&d->buf, 0);
- msg->given = 1;
+ d->given = 1;
return 0;
}
static int parse_file_arg(const struct option *opt, const char *arg, int unset)
{
- struct msg_arg *msg = opt->value;
+ struct note_data *d = opt->value;
- if (msg->buf.len)
- strbuf_addch(&(msg->buf), '\n');
+ if (d->buf.len)
+ strbuf_addch(&d->buf, '\n');
if (!strcmp(arg, "-")) {
- if (strbuf_read(&(msg->buf), 0, 1024) < 0)
+ if (strbuf_read(&d->buf, 0, 1024) < 0)
die_errno(_("cannot read '%s'"), arg);
- } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
+ } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
die_errno(_("could not open or read '%s'"), arg);
- stripspace(&(msg->buf), 0);
+ stripspace(&d->buf, 0);
- msg->given = 1;
+ d->given = 1;
return 0;
}
static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
{
- struct msg_arg *msg = opt->value;
+ struct note_data *d = opt->value;
char *buf;
unsigned char object[20];
enum object_type type;
unsigned long len;
- if (msg->buf.len)
- strbuf_addch(&(msg->buf), '\n');
+ if (d->buf.len)
+ strbuf_addch(&d->buf, '\n');
if (get_sha1(arg, object))
die(_("Failed to resolve '%s' as a valid ref."), arg);
@@ -274,17 +273,17 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
free(buf);
die(_("Cannot read note data from non-blob object '%s'."), arg);
}
- strbuf_add(&(msg->buf), buf, len);
+ strbuf_add(&d->buf, buf, len);
free(buf);
- msg->given = 1;
+ d->given = 1;
return 0;
}
static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
{
- struct msg_arg *msg = opt->value;
- msg->use_editor = 1;
+ struct note_data *d = opt->value;
+ d->use_editor = 1;
return parse_reuse_arg(opt, arg, unset);
}
@@ -403,18 +402,18 @@ 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 msg_arg msg = { 0, 0, STRBUF_INIT };
+ struct note_data d = { 0, 0, STRBUF_INIT };
struct option options[] = {
- { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
+ { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
N_("note contents as a string"), PARSE_OPT_NONEG,
parse_msg_arg},
- { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
+ { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
N_("note contents in a file"), PARSE_OPT_NONEG,
parse_file_arg},
- { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
+ { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
parse_reedit_arg},
- { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
+ { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
N_("reuse specified note object"), PARSE_OPT_NONEG,
parse_reuse_arg},
OPT__FORCE(&force, N_("replace existing notes")),
@@ -439,7 +438,7 @@ static int add(int argc, const char **argv, const char *prefix)
if (note) {
if (!force) {
- if (!msg.given) {
+ if (!d.given) {
/*
* Redirect to "edit" subcommand.
*
@@ -460,7 +459,7 @@ static int add(int argc, const char **argv, const char *prefix)
sha1_to_hex(object));
}
- create_note(object, &msg, 0, note, new_note);
+ create_note(object, &d, 0, note, new_note);
if (is_null_sha1(new_note))
remove_note(t, object);
@@ -472,7 +471,7 @@ static int add(int argc, const char **argv, const char *prefix)
commit_notes(t, logmsg);
out:
free_notes(t);
- strbuf_release(&(msg.buf));
+ strbuf_release(&d.buf);
return retval;
}
@@ -560,18 +559,18 @@ static int append_edit(int argc, const char **argv, const char *prefix)
const unsigned char *note;
char logmsg[100];
const char * const *usage;
- struct msg_arg msg = { 0, 0, STRBUF_INIT };
+ struct note_data d = { 0, 0, STRBUF_INIT };
struct option options[] = {
- { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
+ { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
N_("note contents as a string"), PARSE_OPT_NONEG,
parse_msg_arg},
- { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
+ { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
N_("note contents in a file"), PARSE_OPT_NONEG,
parse_file_arg},
- { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
+ { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
parse_reedit_arg},
- { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
+ { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
N_("reuse specified note object"), PARSE_OPT_NONEG,
parse_reuse_arg},
OPT_END()
@@ -587,7 +586,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
usage_with_options(usage, options);
}
- if (msg.given && edit)
+ if (d.given && edit)
fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
"for the 'edit' subcommand.\n"
"Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
@@ -600,7 +599,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
t = init_notes_check(argv[0]);
note = get_note(t, object);
- create_note(object, &msg, !edit, note, new_note);
+ create_note(object, &d, !edit, note, new_note);
if (is_null_sha1(new_note))
remove_note(t, object);
@@ -611,7 +610,7 @@ 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(&(msg.buf));
+ 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 ` Johan Herland [this message]
2014-11-09 12:30 ` [PATCHv4 4/9] builtin/notes: Refactor note file path into struct note_data Johan Herland
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-4-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).