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

Re: Git on Windows, CRLF issues

From
Jeff King <peff@peff.net>
Date
Apr 22, 2008, 02:39 UTC
Message-ID
<20080422023918.GA5402@sigill.intra.peff.net>
In-Reply-To
<32541b130804211453x77f3fd49hef645a417a9919ca@mail.gmail.com>
On Mon, Apr 21, 2008 at 05:53:34PM -0400, Avery Pennarun wrote:
> Does anyone know the most efficient way to do this with
> git-filter-branch, when there are already thousands of files in the
> repo with CRLF in them?  Running dos2unix on all the files for every
> single revision could take a *very* long time.

Yes, a tree filter would probably be quite slow due to checking out, and then munging all of the files.

You could maybe do an index filter that gets the blob SHA1 of each file that is new, and just munges those. But I think it is even simpler to just keep a cache of original blob hashes mapping to munged blob hashes.

Something like:
  git filter-branch --index-filter '
    git ls-files --stage |
    perl /path/to/caching-munger |
    git update-index --index-info
  '
where your caching munger looks something like:

-- >8 -- #!/usr/bin/perl

use strict;
use DB_File;
use Fcntl;
tie my %cache, 'DB_File', "$ENV{HOME}/filter-cache", O_RDWR|O_CREAT, 0666
  or die "unable to open db: $!";
while(<>) {
  my ($mode, $hash, $path) = /^(\d+) ([0-9a-f]{40}) \d\t(.*)/
    or die "bad ls-files line: $_";
  $cache{$hash} = munge($hash)
    unless exists $cache{$hash};
  print "$mode $cache{$hash}\t$path\n";
}
sub munge {
  my $h = shift;
  my $r = scalar `git show $h | sed 's/\$/\\r/' | git hash-object -w --stdin`;
  chomp $r;
  return $r;
}
-- 8< --

so we keep a dbm of the hash mapping, and do no work if we have already seen this blob. If we don't, then we actually do the expensive 'show | munge | hash-object'. And here our munge adds a CR, but you should be able to do an arbitrary transformation.

-Peff
Previous: Avery PennarunNext: Avery Pennarun
Message 4 of 30 in “Git on Windows, CRLF issues”
  1. Peter KarlssonApr 21, 2008
  2. Johannes SchindelinApr 21, 2008
  3. Avery PennarunApr 21, 2008
  4. Jeff KingApr 22, 2008
  5. Avery PennarunApr 22, 2008
  6. Peter KarlssonApr 23, 2008
  7. Jeff KingApr 23, 2008
  8. Peter KarlssonApr 23, 2008
  9. Johan HerlandApr 23, 2008
  10. Johannes SixtApr 23, 2008
  11. Jeff KingApr 23, 2008
  12. Johannes SchindelinApr 23, 2008
  13. Jeff KingApr 23, 2008
  14. Johannes SixtApr 23, 2008
  15. Jeff KingApr 23, 2008
  16. Johannes SixtApr 23, 2008
  17. Jeff KingApr 23, 2008
  18. Junio C HamanoApr 23, 2008
  19. Avery PennarunApr 23, 2008
  20. Johannes SchindelinApr 24, 2008
  21. Avery PennarunApr 24, 2008
  22. Jeff KingApr 24, 2008
  23. Avery PennarunApr 23, 2008
  24. Johannes SixtApr 24, 2008
  25. Johannes SixtApr 22, 2008
  26. Jakub NarebskiApr 21, 2008
  27. Peter KarlssonApr 22, 2008
  28. Johannes SixtApr 22, 2008
  29. Johannes SixtApr 22, 2008
  30. Peter KarlssonApr 22, 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.