threads / patch / 12150

patch, 4 partsImprove message-id generation flow control for format-patch

Subject: [PATCH 1/4] Improve message-id generation flow control for format-patch

## tl;dr

4 messages between Feb 17, 2008 and Feb 18, 2008. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Daniel Barkalow· Feb 17, 2008, 18:35 UTC · lore
Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
---
 builtin-log.c |   29 ++++++++++++++---------------
 1 files changed, 14 insertions(+), 15 deletions(-)
Show changes to builtin-log.c +14 −15
diff --git a/builtin-log.c b/builtin-log.c
index 99d69f0..867cc13 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -575,16 +575,19 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const cha
 	o2->flags = flags2;
 }
 
-static void gen_message_id(char *dest, unsigned int length, char *base)
+static void gen_message_id(struct rev_info *info, char *base)
 {
 	const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
 	const char *email_start = strrchr(committer, '<');
 	const char *email_end = strrchr(committer, '>');
-	if(!email_start || !email_end || email_start > email_end - 1)
+	struct strbuf buf;
+	if (!email_start || !email_end || email_start > email_end - 1)
 		die("Could not extract email from committer identity.");
-	snprintf(dest, length, "%s.%lu.git.%.*s", base,
-		 (unsigned long) time(NULL),
-		 (int)(email_end - email_start - 1), email_start + 1);
+	strbuf_init(&buf, 0);
+	strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
+		    (unsigned long) time(NULL),
+		    (int)(email_end - email_start - 1), email_start + 1);
+	info->message_id = strbuf_detach(&buf, NULL);
 }
 
 static const char *clean_message_id(const char *msg_id)
@@ -625,8 +628,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	const char *in_reply_to = NULL;
 	struct patch_ids ids;
 	char *add_signoff = NULL;
-	char message_id[1024];
-	char ref_message_id[1024];
 
 	git_config(git_format_config);
 	init_revisions(&rev, prefix);
@@ -809,15 +810,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		rev.nr = total - nr + (start_number - 1);
 		/* Make the second and subsequent mails replies to the first */
 		if (thread) {
-			if (nr == (total - 2)) {
-				strncpy(ref_message_id, message_id,
-					sizeof(ref_message_id));
-				ref_message_id[sizeof(ref_message_id)-1]='\0';
-				rev.ref_message_id = ref_message_id;
+			if (rev.message_id) {
+				if (rev.ref_message_id)
+					free((char *) rev.message_id);
+				else
+					rev.ref_message_id = rev.message_id;
 			}
-			gen_message_id(message_id, sizeof(message_id),
-				       sha1_to_hex(commit->object.sha1));
-			rev.message_id = message_id;
+			gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
 		}
 		if (!use_stdout)
 			if (reopen_stdout(commit, rev.nr, keep_subject,
-- 
1.5.4.1.1350.g2b9ee
Johannes Schindelin· Feb 18, 2008, 12:41 UTC · re: Daniel Barkalow · lore

Re: [PATCH 1/4] Improve message-id generation flow control for format-patch

Hi,
On Sun, 17 Feb 2008, Daniel Barkalow wrote:
Show 26 quoted lines
> diff --git a/builtin-log.c b/builtin-log.c
> index 99d69f0..867cc13 100644
> --- a/builtin-log.c
> +++ b/builtin-log.c
> @@ -575,16 +575,19 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const cha
>  	o2->flags = flags2;
>  }
>  
> -static void gen_message_id(char *dest, unsigned int length, char *base)
> +static void gen_message_id(struct rev_info *info, char *base)
>  {
>  	const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
>  	const char *email_start = strrchr(committer, '<');
>  	const char *email_end = strrchr(committer, '>');
> -	if(!email_start || !email_end || email_start > email_end - 1)
> +	struct strbuf buf;
> +	if (!email_start || !email_end || email_start > email_end - 1)
>  		die("Could not extract email from committer identity.");
> -	snprintf(dest, length, "%s.%lu.git.%.*s", base,
> -		 (unsigned long) time(NULL),
> -		 (int)(email_end - email_start - 1), email_start + 1);
> +	strbuf_init(&buf, 0);
> +	strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
> +		    (unsigned long) time(NULL),
> +		    (int)(email_end - email_start - 1), email_start + 1);
> +	info->message_id = strbuf_detach(&buf, NULL);
With this last line, and...
Show 12 quoted lines
> @@ -809,15 +810,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
>  		rev.nr = total - nr + (start_number - 1);
>  		/* Make the second and subsequent mails replies to the first */
>  		if (thread) {
> -			if (nr == (total - 2)) {
> -				strncpy(ref_message_id, message_id,
> -					sizeof(ref_message_id));
> -				ref_message_id[sizeof(ref_message_id)-1]='\0';
> -				rev.ref_message_id = ref_message_id;
> +			if (rev.message_id) {
> +				if (rev.ref_message_id)
> +					free((char *) rev.message_id);

... this one, you should make the message_id member of struct rev_info a "char *". At least for this developer, "const char *" is a sign that the caller should clean up, and that the pointer _might_ point to a constant.

Ciao, Dscho

Daniel Barkalow· Feb 18, 2008, 17:43 UTC · re: Johannes Schindelin · lore

Re: [PATCH 1/4] Improve message-id generation flow control for format-patch

On Mon, 18 Feb 2008, Johannes Schindelin wrote:
Show 49 quoted lines
> Hi,
> 
> On Sun, 17 Feb 2008, Daniel Barkalow wrote:
> 
> > diff --git a/builtin-log.c b/builtin-log.c
> > index 99d69f0..867cc13 100644
> > --- a/builtin-log.c
> > +++ b/builtin-log.c
> > @@ -575,16 +575,19 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const cha
> >  	o2->flags = flags2;
> >  }
> >  
> > -static void gen_message_id(char *dest, unsigned int length, char *base)
> > +static void gen_message_id(struct rev_info *info, char *base)
> >  {
> >  	const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
> >  	const char *email_start = strrchr(committer, '<');
> >  	const char *email_end = strrchr(committer, '>');
> > -	if(!email_start || !email_end || email_start > email_end - 1)
> > +	struct strbuf buf;
> > +	if (!email_start || !email_end || email_start > email_end - 1)
> >  		die("Could not extract email from committer identity.");
> > -	snprintf(dest, length, "%s.%lu.git.%.*s", base,
> > -		 (unsigned long) time(NULL),
> > -		 (int)(email_end - email_start - 1), email_start + 1);
> > +	strbuf_init(&buf, 0);
> > +	strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
> > +		    (unsigned long) time(NULL),
> > +		    (int)(email_end - email_start - 1), email_start + 1);
> > +	info->message_id = strbuf_detach(&buf, NULL);
> 
> With this last line, and...
> 
> > @@ -809,15 +810,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
> >  		rev.nr = total - nr + (start_number - 1);
> >  		/* Make the second and subsequent mails replies to the first */
> >  		if (thread) {
> > -			if (nr == (total - 2)) {
> > -				strncpy(ref_message_id, message_id,
> > -					sizeof(ref_message_id));
> > -				ref_message_id[sizeof(ref_message_id)-1]='\0';
> > -				rev.ref_message_id = ref_message_id;
> > +			if (rev.message_id) {
> > +				if (rev.ref_message_id)
> > +					free((char *) rev.message_id);
> 
> ... this one, you should make the message_id member of struct rev_info a 
> "char *".  At least for this developer, "const char *" is a sign that the 
> caller should clean up, and that the pointer _might_ point to a constant.

It's sort of like that, in that this *is* the caller, and it's using gen_message_id to set it and cleaning it up, and it could put in a constant (in which case it would have to know this and not free it), but I agree that it's more suggestive of the right things as a "char *".

	-Daniel
*This .sig left intentionally blank*
Johannes Schindelin· Feb 18, 2008, 18:00 UTC · re: Daniel Barkalow · lore

Re: [PATCH 1/4] Improve message-id generation flow control for format-patch

Hi,
On Mon, 18 Feb 2008, Daniel Barkalow wrote:
> It's sort of like that, in that this *is* the caller,

Sorry, I should have been clearer. It is all too easy to forget that reasoning and use rev_info->message_id in a different way, and then mix the code paths.

Ciao, Dscho

← back to recent threads