# [PATCH] teach ls-tree how to handle names with embedded LF

4 messages from 2005-04-13 to 2005-04-14. Participants: Junio C Hamano, Petr Baudis.
Thread: https://gitlist.dev/t/7

## Junio C Hamano, 2005-04-13 23:43

Subject: [PATCH] teach ls-tree how to handle names with embedded LF
Message-ID: <7vd5sytf94.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vd5sytf94.fsf%40assigned-by-dhcp.cox.net

```
GIT dircache can represent filenames with embedded newline.
Give ls-tree an option to terminate its output with '\0', so that
its output can be further processed with tools like "sort -z"
(and custom perl/python scripts).

Signed-off-by: Junio C Hamano <junkio@cox.net>

---

 ls-tree.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletion(-)

ls-tree.c:  3e2a6c7d183a42e41f1073dfec6794e8f8a5e75c
--- ls-tree.c
+++ ls-tree.c	2005-04-13 16:17:29.000000000 -0700
@@ -5,6 +5,8 @@
  */
 #include "cache.h"
 
+int line_termination = '\n';
+
 static int list(unsigned char *sha1)
 {
 	void *buffer;
@@ -31,7 +33,8 @@
 		 * It seems not worth it to read each file just to get this
 		 * and the file size. -- pasky@ucw.cz */
 		type = S_ISDIR(mode) ? "tree" : "blob";
-		printf("%03o\t%s\t%s\t%s\n", mode, type, sha1_to_hex(sha1), path);
+		printf("%03o\t%s\t%s\t%s%c", mode, type, sha1_to_hex(sha1),
+		       path, line_termination);
 	}
 	return 0;
 }
@@ -40,6 +43,10 @@
 {
 	unsigned char sha1[20];
 
+	if (argc == 3 && !strcmp(argv[1], "-z")) {
+	  line_termination = 0;
+	  argc--; argv++;
+	}
 	if (argc != 2)
 		usage("ls-tree <key>");
 	if (get_sha1_hex(argv[1], sha1) < 0)

---
head before this patch: f6803ddd4e97a8b0cc090c546d1ff085ed0a4316
tree after  this patch: 0aa0af6254226f0811c72282c46e7e4d9438371c
head after  this patch: 63582e5e8c40790d6255206d6bde65f4ce3820bf


```

## Petr Baudis, 2005-04-13 23:50

Subject: Re: teach ls-tree how to handle names with embedded LF
Message-ID: <20050413235018.GQ25711@pasky.ji.cz>
URL: https://gitlist.dev/e/20050413235018.GQ25711%40pasky.ji.cz
In-Reply-To: <7vd5sytf94.fsf@assigned-by-dhcp.cox.net>

```
Dear diary, on Thu, Apr 14, 2005 at 01:43:03AM CEST, I got a letter
where Junio C Hamano <junkio@cox.net> told me that...
> GIT dircache can represent filenames with embedded newline.
> Give ls-tree an option to terminate its output with '\0', so that
> its output can be further processed with tools like "sort -z"
> (and custom perl/python scripts).

Actually, what about just making it consistent with diff-tree and making
it always terminate the records with '\0'?

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor

```

## Junio C Hamano, 2005-04-14 00:15

Subject: Re: teach ls-tree how to handle names with embedded LF
Message-ID: <7v7jj6tdq9.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v7jj6tdq9.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20050413235018.GQ25711@pasky.ji.cz>

```
>>>>> "PB" == Petr Baudis <pasky@ucw.cz> writes:

PB> Actually, what about just making it consistent with diff-tree and making
PB> it always terminate the records with '\0'?

Sounds good to me.  I need '-z' for a script I am working on to
generate data for incremental updates of object database.


```

## Petr Baudis, 2005-04-14 00:20

Subject: Re: Re: teach ls-tree how to handle names with embedded LF
Message-ID: <20050414002037.GS25711@pasky.ji.cz>
URL: https://gitlist.dev/e/20050414002037.GS25711%40pasky.ji.cz
In-Reply-To: <7v7jj6tdq9.fsf@assigned-by-dhcp.cox.net>

```
Dear diary, on Thu, Apr 14, 2005 at 02:15:58AM CEST, I got a letter
where Junio C Hamano <junkio@cox.net> told me that...
> >>>>> "PB" == Petr Baudis <pasky@ucw.cz> writes:
> 
> PB> Actually, what about just making it consistent with diff-tree and making
> PB> it always terminate the records with '\0'?
> 
> Sounds good to me.  I need '-z' for a script I am working on to
> generate data for incremental updates of object database.

I actually use ls-tree only in various one-shot hacks, so it's not an
issue for me neither.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor

```
