# Re: git-filehistory (3rd try at detecting renames)

2 messages from 2005-10-20 to 2005-10-20. Participants: David Ho, Johannes Schindelin.
Thread: https://gitlist.dev/t/2169

## David Ho, 2005-10-20 21:23

Subject: Re: git-filehistory (3rd try at detecting renames)
Message-ID: <4dd15d180510201423y5e200b68xec32bffec0f0cf84@mail.gmail.com>
URL: https://gitlist.dev/e/4dd15d180510201423y5e200b68xec32bffec0f0cf84%40mail.gmail.com

```
Sorry, once again, this calls git-diff-tree once per commit.

[davidho@penguin git-tutorial]$ cat git-filehistory.perl
#!/usr/bin/perl

use warnings;
use strict;

sub usage($);

# Sanity checks:
my $GIT_DIR = $ENV{'GIT_DIR'} || ".git";

unless ( -d $GIT_DIR && -d $GIT_DIR . "/objects" &&
        -d $GIT_DIR . "/objects/" && -d $GIT_DIR . "/refs") {
        usage("Git repository not found.");
}

usage("") if scalar @ARGV != 1;

my ($file) = @ARGV;

unless (-f $file) {
        usage("git filehistory: '$file' does not exist");
}

open(F,"-|","git-rev-parse",,"--default","HEAD","--revs-only",$file);
my $git_rev_args = <F>;
chomp ($git_rev_args);
close (F);

open(F,"-|","git-rev-list", $git_rev_args);
my @commits = <F>;
close (F);

my $nextname = $file;
my (@renamecommit, @renamecomment, @renameentry);
for (my $i=0; $i < scalar @commits; $i++)
{
        my $commit = $commits[$i];
        chomp ($commit);
        open (F, "-|","git-diff-tree","-r","-M","--pretty=medium",$commit);
        @renamecommit = <F>;
        close(F);
        @renamecomment = grep /^[^:]/, @renamecommit;
        @renameentry = grep /$nextname/, @renamecommit;
        push @renamecomment, @renameentry;
        if (scalar @renameentry) {
                if ($renameentry[0] =~ /\t$nextname$/) {
                        foreach (@renamecomment) {
                                print;
                        }
                }
                if ($renameentry[0] =~ /R\d{3}\t(.+)\t(.+)/) {
                        #The file is renamed
                        $nextname = $1;
                }
        }
}

sub usage($) {
        my $s = shift;
        print $s, "\n" if (length $s != 0);
        print <<EOT;
$0 <file>
This script traces renames to find the origin of the file
EOT
        exit(1);
}

```

## Johannes Schindelin, 2005-10-20 23:32

Subject: Re: git-filehistory (3rd try at detecting renames)
Message-ID: <Pine.LNX.4.63.0510210130480.10503@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0510210130480.10503%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <4dd15d180510201423y5e200b68xec32bffec0f0cf84@mail.gmail.com>

```
Hi,

On Thu, 20 Oct 2005, David Ho wrote:

> Sorry, once again, this calls git-diff-tree once per commit.

How about detecting renames only when the tree does not have an object of 
the same name? You could even use the pathspec optimizations to track just 
that file through history, and do a git-diff-tree on the last commit 
containing that file.

Ciao,
Dscho

```
