git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] builtin-apply: check for empty files when detecting creation patch

From
Imre Deak <imre.deak@gmail.com>
Date
May 8, 2008, 14:39 UTC
Message-ID
<1210257579-30975-1-git-send-email-imre.deak@gmail.com>

When we can only guess if we have a creation patch, we do this by treating the patch as such if there weren't any old lines. Zero length files can be patched without old lines though, so do an extra check for file size.

Signed-off-by: Imre Deak <imre.deak@gmail.com>
---
 builtin-apply.c |   34 +++++++++++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 1 deletions(-)
diff --git a/builtin-apply.c b/builtin-apply.c
index caa3f2a..80d0779 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -1096,6 +1096,33 @@ static int parse_fragment(char *line, unsigned long size,
 	return offset;
 }
 
+static size_t existing_file_size(const char *file)
+{
+	size_t st_size = -1;
+
+	if (file == NULL)
+		return -1;
+
+	if (cached) {
+		struct cache_entry *ce;
+		int pos;
+
+		pos = cache_name_pos(file, strlen(file));
+		if (pos < 0)
+			return -1;
+		ce = active_cache[pos];
+		st_size = ntohl(ce->ce_size);
+	} else {
+		struct stat st;
+
+		if (lstat(file, &st) < 0)
+			return -1;
+		st_size = st.st_size;
+	}
+
+	return st_size;
+}
+
 static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
 {
 	unsigned long offset = 0;
@@ -1143,13 +1170,18 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
 	if (patch->is_delete < 0 &&
 	    (newlines || (patch->fragments && patch->fragments->next)))
 		patch->is_delete = 0;
+	/* FIXME: How can be there context if it's a creation / deletion? */
 	if (!unidiff_zero || context) {
 		/* If the user says the patch is not generated with
 		 * --unified=0, or if we have seen context lines,
 		 * then not having oldlines means the patch is creation,
 		 * and not having newlines means the patch is deletion.
+		 *
+		 * It's also possible that a zero length file is added
+		 * to.
 		 */
-		if (patch->is_new < 0 && !oldlines) {
+		if (patch->is_new < 0 && !oldlines &&
+		    existing_file_size(patch->old_name) != 0) {
 			patch->is_new = 1;
 			patch->old_name = NULL;
 		}
-- 
1.5.4.2.dirty
Next: Junio C Hamano
Message 1 of 12 in “builtin-apply: check for empty files when detecting creation patch”
  1. builtin-apply: check for empty files when detecting creation patchImre Deak, May 8, 2008
  2. Junio C HamanoMay 11, 2008
  3. Imre DeakMay 13, 2008
  4. Junio C HamanoMay 13, 2008
  5. Linus TorvaldsMay 13, 2008
  6. Junio C HamanoMay 13, 2008
  7. Linus TorvaldsMay 13, 2008
  8. Junio C HamanoMay 14, 2008
  9. Linus TorvaldsMay 14, 2008
  10. Junio C HamanoMay 17, 2008
  11. 1/2 builtin-apply: accept patch to an empty fileJunio C Hamano, May 17, 2008
  12. 2/2 builtin-apply: do not declare patch is creation when we do not know itJunio C Hamano, May 17, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.