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

Re: [BUG] gitattribute macro expansion oddity

From
Jeff King <peff@peff.net>
Date
Jan 10, 2012, 17:11 UTC
Message-ID
<20120110171100.GA18962@sigill.intra.peff.net>
In-Reply-To
<4F0BFE6E.6080904@alum.mit.edu>
On Tue, Jan 10, 2012 at 10:01:34AM +0100, Michael Haggerty wrote:
Show 27 quoted lines
> On 01/10/2012 08:03 AM, Jeff King wrote:
> > I'm seeing some very odd behavior with git's attribute expansion for
> > diffs. You can see it with this repository:
> > 
> >   git clone git://github.com/libgit2/libgit2sharp.git
> > 
> > Try a diff of a non-binary file: [...]
> 
> The problem has nothing with diffing; simply interrogating the attribute
> values gives different results depending on the order of the files:
> 
> $ git check-attr --all Lib/NativeBinaries/x86/git2.dll
> LibGit2Sharp/Configuration.cs
> Lib/NativeBinaries/x86/git2.dll: binary: set
> Lib/NativeBinaries/x86/git2.dll: diff: unset
> Lib/NativeBinaries/x86/git2.dll: text: unset
> LibGit2Sharp/Configuration.cs: binary: set
> LibGit2Sharp/Configuration.cs: diff: unset
> LibGit2Sharp/Configuration.cs: text: unset
> LibGit2Sharp/Configuration.cs: crlf: set
> $ git check-attr --all LibGit2Sharp/Configuration.cs
> Lib/NativeBinaries/x86/git2.dll
> LibGit2Sharp/Configuration.cs: diff: csharp
> LibGit2Sharp/Configuration.cs: crlf: set
> Lib/NativeBinaries/x86/git2.dll: binary: set
> Lib/NativeBinaries/x86/git2.dll: diff: unset
> Lib/NativeBinaries/x86/git2.dll: text: unset

Thanks. I tried to test it with check-attr but for some reason wasn't able to provoke the bug (I think I probably just screwed up the invocation).

Show 5 quoted lines
> It also doesn't depend on the fact that Lib/.gitattributes uses CRLF as
> its EOL, nor does it depend on the use of the "binary" macro.  However,
> it does depend on the fact that the directory name "Lib" matches the
> first part of the directory name "LibGit2Sharp".  Here is a simplified
> demonstration of the problem:

Ah, very helpful. That allowed me to find the problem very quickly by grepping for "strncmp". :)

The patch below seem to fix it for me. I'll do a bit more testing before posting it for real, though.

-Peff
diff --git a/attr.c b/attr.c
index 7467baf..f4beb62 100644
--- a/attr.c
+++ b/attr.c
@@ -528,7 +528,8 @@ static void prepare_attr_stack(const char *path, int dirlen)
 
 		elem = attr_stack;
 		if (namelen <= dirlen &&
-		    !strncmp(elem->origin, path, namelen))
+		    !strncmp(elem->origin, path, namelen) &&
+		    (!namelen || path[namelen] == '/' || path[namelen] == '\0'))
 			break;
 
 		debug_pop(elem);
Previous: Michael HaggertyNext: Jeff King
Message 3 of 13 in “[BUG] gitattribute macro expansion oddity”
  1. Jeff KingJan 10, 2012
  2. Michael HaggertyJan 10, 2012
  3. Jeff KingJan 10, 2012
  4. attr: don't confuse prefixes with leading directoriesJeff King, Jan 10, 2012
  5. Jeff KingJan 10, 2012
  6. Junio C HamanoJan 10, 2012
  7. Jeff KingJan 10, 2012
  8. Jeff KingJan 10, 2012
  9. Junio C HamanoJan 10, 2012
  10. Jeff KingJan 10, 2012
  11. Junio C HamanoJan 10, 2012
  12. Junio C HamanoJan 10, 2012
  13. Michael HaggertyJan 11, 2012

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.