# [PATCH] Make git-mailinfo strip whitespace from the start of the mail file.

7 messages from 2007-11-01 to 2007-11-04. Participants: Simon Sasburg, Junio C Hamano.
Thread: https://gitlist.dev/t/10572

## Simon Sasburg, 2007-11-01 21:05

Subject: [PATCH] Make git-mailinfo strip whitespace from the start of the mail file.
Message-ID: <1193951139-2312-1-git-send-email-Simon.Sasburg@gmail.com>
URL: https://gitlist.dev/e/1193951139-2312-1-git-send-email-Simon.Sasburg%40gmail.com

```
This allows you to use files gotten through gmail's web interface via its 'Show original' option.

Signed-off-by: Simon Sasburg <Simon.Sasburg@gmail.com>
---
Note that this doesn't exactly follow RFC 2822 as far as i can see, but i don't know if git prefers to be strict or tolerant in these cases, so i'm sending the patch anyway.

It certaily helps me, even if just a little bit.

 builtin-mailinfo.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
index fb12248..5d4b6bf 100644
--- a/builtin-mailinfo.c
+++ b/builtin-mailinfo.c
@@ -915,6 +915,7 @@ static void handle_info(void)
 static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
 		    const char *msg, const char *patch)
 {
+	int peek;
 	keep_subject = ks;
 	metainfo_charset = encoding;
 	fin = in;
@@ -935,6 +936,11 @@ static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
 	p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(char *));
 	s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(char *));
 
+	do {
+		peek = fgetc(in);
+	} while (peek == ' ' || peek == '\r' || peek == '\n');
+	ungetc(peek, in);
+
 	/* process the email header */
 	while (read_one_header_line(line, sizeof(line), fin))
 		check_header(line, sizeof(line), p_hdr_data, 1);
-- 
1.5.3.4.502.g37c97

```

## Junio C Hamano, 2007-11-01 21:33

Subject: Re: [PATCH] Make git-mailinfo strip whitespace from the start of the mail file.
Message-ID: <7vd4utabx0.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vd4utabx0.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1193951139-2312-1-git-send-email-Simon.Sasburg@gmail.com>

```
Just to help me understand why this change is needed...

Are you using mailinfo directly without splitting with mailsplit
first?

```

## Simon Sasburg, 2007-11-01 21:41

Subject: Re: [PATCH] Make git-mailinfo strip whitespace from the start of the mail file.
Message-ID: <981e6de60711011441n5bef772cuda381c539c0a2603@mail.gmail.com>
URL: https://gitlist.dev/e/981e6de60711011441n5bef772cuda381c539c0a2603%40mail.gmail.com
In-Reply-To: <7vd4utabx0.fsf@gitster.siamese.dyndns.org>

```
> Just to help me understand why this change is needed...
>
> Are you using mailinfo directly without splitting with mailsplit
> first?

Well, when using gmail's web interface, when reading a mail, there is
this option to show the raw mail text (headers+body) with the 'show
original' option.

If you do 'save as..' in your browser to save what you get with that,
and try to do git-am on that file, it fails because it starts with
some whitespace.

With this patch git-am works on these files.

So, i'm not using mailsplit or any other mail tools at all, just my
browser and git.

```

## Junio C Hamano, 2007-11-01 22:26

Subject: Re: [PATCH] Make git-mailinfo strip whitespace from the start of the mail file.
Message-ID: <7vr6j98uw5.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vr6j98uw5.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <981e6de60711011441n5bef772cuda381c539c0a2603@mail.gmail.com>

```
"Simon Sasburg" <simon.sasburg@gmail.com> writes:

>> Just to help me understand why this change is needed...
>>
>> Are you using mailinfo directly without splitting with mailsplit
>> first?
>
> Well, when using gmail's web interface, when reading a mail, there is
> this option to show the raw mail text (headers+body) with the 'show
> original' option.
>
> If you do 'save as..' in your browser to save what you get with that,
> and try to do git-am on that file, it fails because it starts with
> some whitespace.
>
> With this patch git-am works on these files.
>
> So, i'm not using mailsplit or any other mail tools at all, just my
> browser and git.

Ah, I meant "git-mailsplit", which is the command internally run
by "git-am" to preprocess the file and to split it into
individual mail pieces to be fed to "git-mailinfo".

That may suggest the change is better done in git-mailsplit not
git-mailinfo.

Or perhaps both.

```

## Simon Sasburg, 2007-11-01 22:57

Subject: [PATCH] Make git-mailsplit strip whitespace from the start of the mailbox file.
Message-ID: <1193957865-3904-1-git-send-email-Simon.Sasburg@gmail.com>
URL: https://gitlist.dev/e/1193957865-3904-1-git-send-email-Simon.Sasburg%40gmail.com
In-Reply-To: <7vr6j98uw5.fsf@gitster.siamese.dyndns.org>

```
Signed-off-by: Simon Sasburg <Simon.Sasburg@gmail.com>
---

Ah, i see.

Well, this patch also fixes the problem in my case.

 builtin-mailsplit.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/builtin-mailsplit.c b/builtin-mailsplit.c
index 43fc373..3fdeb23 100644
--- a/builtin-mailsplit.c
+++ b/builtin-mailsplit.c
@@ -164,6 +164,7 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
 {
 	char name[PATH_MAX];
 	int ret = -1;
+	int peek;
 
 	FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
 	int file_done = 0;
@@ -173,6 +174,11 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
 		goto out;
 	}
 
+	do {
+		peek = fgetc(f);
+	} while (peek == ' ' || peek == '\r' || peek == '\n');
+	ungetc(peek, f);
+
 	if (fgets(buf, sizeof(buf), f) == NULL) {
 		/* empty stdin is OK */
 		if (f != stdin) {
-- 
1.5.3.4.504.gdf75-dirty

```

## Junio C Hamano, 2007-11-02 08:53

Subject: Re: [PATCH] Make git-mailinfo strip whitespace from the start of the mail file.
Message-ID: <7v8x5h58qj.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8x5h58qj.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1193951139-2312-1-git-send-email-Simon.Sasburg@gmail.com>

```
Simon Sasburg <simon.sasburg@gmail.com> writes:

> @@ -935,6 +936,11 @@ static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
>  	p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(char *));
>  	s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(char *));
>  
> +	do {
> +		peek = fgetc(in);
> +	} while (peek == ' ' || peek == '\r' || peek == '\n');
> +	ungetc(peek, in);
> +

I wonder why this is not using isspace(peek).

```

## Simon Sasburg, 2007-11-04 13:32

Subject: [PATCH] Make git-mailsplit strip whitespace from the start of the mailbox file.
Message-ID: <1194183120-3660-1-git-send-email-Simon.Sasburg@gmail.com>
URL: https://gitlist.dev/e/1194183120-3660-1-git-send-email-Simon.Sasburg%40gmail.com
In-Reply-To: <7v8x5h58qj.fsf@gitster.siamese.dyndns.org>

```
This will allow it to handle the files gotten through gmail's web interface via its 'Show original' option.
These files contain the mail headers and the mail body, but start with some whitespace.
Now you can give these files to git-am without having to remove the whitespace yourself.

Signed-off-by: Simon Sasburg <Simon.Sasburg@gmail.com>
---

On Nov 2, 2007 9:53 AM, Junio C Hamano <gitster@pobox.com> wrote:
> I wonder why this is not using isspace(peek).

Fixed.

On Nov 1, 2007 11:26 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Ah, I meant "git-mailsplit", which is the command internally run
> by "git-am" to preprocess the file and to split it into
> individual mail pieces to be fed to "git-mailinfo".
> 
> That may suggest the change is better done in git-mailsplit not
> git-mailinfo.

The files from gmail only contain 1 mail per file, but having git-mailspit
massage these into a proper file that git-mailinfo can parse seems like
a sane solution to me.

 builtin-mailsplit.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/builtin-mailsplit.c b/builtin-mailsplit.c
index 43fc373..74b0470 100644
--- a/builtin-mailsplit.c
+++ b/builtin-mailsplit.c
@@ -164,6 +164,7 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
 {
 	char name[PATH_MAX];
 	int ret = -1;
+	int peek;
 
 	FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
 	int file_done = 0;
@@ -173,6 +174,11 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
 		goto out;
 	}
 
+	do {
+		peek = fgetc(f);
+	} while (isspace(peek));
+	ungetc(peek, f);
+
 	if (fgets(buf, sizeof(buf), f) == NULL) {
 		/* empty stdin is OK */
 		if (f != stdin) {
-- 
1.5.3.4.504.gdf75-dirty

```
