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

Re: git filter-branch --subdirectory-filter

From
Jeff King <peff@peff.net>
Date
May 10, 2008, 05:53 UTC
Message-ID
<20080510055332.GB11556@sigill.intra.peff.net>
In-Reply-To
<e5e204700805092031m14c3d6c2kb85b51af5a1ee8f7@mail.gmail.com>
On Sat, May 10, 2008 at 01:31:37PM +1000, James Sadler wrote:
Show 6 quoted lines
> Does anybody have a script that can take an existing repo, and create
> a new one with garbled-but-equivalent commits?  i.e.  file and
> directory structure is same with names changed, and there is a one-one
> relationship between lines of text in new repo and old one except the
> lines have been scrambled?  It would be a useful tool for distributing
> private repositories for debugging reasons.

This is only lightly tested, but the script below should do the trick. It works as an index filter which munges all content in such a way that a particular line is always given the same replacement text. That means that diffs will look approximately the same, but will add and remove lines that say "Fake line XXX" instead of the actual content.

You can munge the commit messages themselves by just replacing them with some unique text; in the example below, we just replace them with the md5sum of the content.

This will leave the original author, committer, and date, which is presumably non-proprietary.

-- >8 -- #!/usr/bin/perl # # Obscure a repository while still maintaining the same history # structure and diffs. # # Invoke as: # git filter-branch \ # --msg-filter md5sum \ # --index-filter /path/to/this/script

use strict; use IPC::Open2; use DB_File; use Fcntl; tie my %blob_cache, 'DB_File', 'blob-cache', O_RDWR|O_CREAT, 0666; tie my %line_cache, 'DB_File', 'line-cache', O_RDWR|O_CREAT, 0666;

open(my $lsfiles, '-|', qw(git ls-files --stage))
  or die "unable to open ls-files: $!";
open(my $update, '|-', qw(git update-index --index-info))
  or die "unable to open upate-inex: $!";
while(<$lsfiles>) {
  my ($mode, $hash, $path) = /^(\d+) ([0-9a-f]{40}) \d\t(.*)/
    or die "bad ls-files line: $_";
  $blob_cache{$hash} = munge($hash)
    unless exists $blob_cache{$hash};
  print $update "$mode $blob_cache{$hash}\t$path\n";
}

close($lsfiles); close($update); exit $?;

sub munge {
  my $h = shift;
  open(my $in, '-|', qw(git show), $h)
    or die "unable to open git show: $!";
  open2(my $hash, my $out, qw(git hash-object -w --stdin));
  while(<$in>) {
    $line_cache{$_} ||= 'Fake line ' . $line_cache{CURRENT}++ . "\n";
    print $out $line_cache{$_};
  }
  close($in);
  close($out);
  my $r = <$hash>;
  chomp $r;
  return $r;
}
Previous: James SadlerNext: James Sadler
Message 7 of 10 in “git filter-branch --subdirectory-filter”
  1. James SadlerMay 9, 2008
  2. Jeff KingMay 9, 2008
  3. James SadlerMay 9, 2008
  4. Johannes SixtMay 9, 2008
  5. Jeff KingMay 9, 2008
  6. James SadlerMay 10, 2008
  7. Jeff KingMay 10, 2008
  8. James SadlerMay 10, 2008
  9. James SadlerMay 10, 2008
  10. Jeff KingMay 10, 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.