threads / discuss / 2167

git-filehistory (first cut at detecting renames)

Subject: git-filehistory (first cut at detecting renames)

## tl;dr

2 messages between Oct 20, 2005 and Oct 20, 2005.

replies: 1people: 1as markdown or json

David Ho· Oct 20, 2005, 19:51 UTC · lore
Hi all,

This is my stupid perl script to detect renames all the way to it's origin. This is my first attempt at writing a perl script so it looks a bit awful. It's not perfect, if a file is renamed, the entire commit is dumped out. Maybe if git-diff-tree -M <pathname> does post processing to output changes relevant to <pathname> then it will work quite well.

David

[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;
for (my $i=0; $i < scalar @commits; $i++)
{
        my $commit = $commits[$i];
        chomp ($commit);
        open(F,"-|","git-diff-tree","-r","-M", $commit)
                or die "Failed to open pipe from git-diff-tree: " . $!;
        my @lines = grep /R\d{3}.+$nextname/, <F>;
        close(F);
        if (scalar @lines >=1) {
                #The file is renamed!
                my $rc = system
("git-diff-tree","-r","-M","-p","--pretty=medium",$commit);
                die "git-diff-tree failed" if $rc;
                print "\n";
                if ($lines[0] =~ /R\d{3}\t(.+)\t(.+)/) {
                        $nextname = $1;
                }
        }
        else {
                #Not renamed
                my $rc = system
("git-diff-tree","-r","-m","-p","--pretty=medium",$commit, $nextname);
                die "git-diff-tree failed" if $rc;
                print "\n";
        }
}
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);
}
David Ho· Oct 20, 2005, 20:19 UTC · re: David Ho · lore

Re: git-filehistory (first cut at detecting renames)

Actually, just tried it on the git repository. It is very slow. Maybe because I'm doing a complete diff-tree on each commit. Mmm... maybe there is a better way to detect renames when walking up the file history.

David
On 10/20/05, David Ho <davidkwho@gmail.com> wrote:
Show 83 quoted lines
> Hi all,
>
> This is my stupid perl script to detect renames all the way to it's
> origin.  This is my first attempt at writing a perl script so it looks
> a bit awful.  It's not perfect, if a file is renamed, the entire
> commit is dumped out.  Maybe if git-diff-tree -M <pathname> does post
> processing to output changes relevant to <pathname> then it will work
> quite well.
>
> David
>
> [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;
> for (my $i=0; $i < scalar @commits; $i++)
> {
>         my $commit = $commits[$i];
>         chomp ($commit);
>
>         open(F,"-|","git-diff-tree","-r","-M", $commit)
>                 or die "Failed to open pipe from git-diff-tree: " . $!;
>         my @lines = grep /R\d{3}.+$nextname/, <F>;
>         close(F);
>         if (scalar @lines >=1) {
>                 #The file is renamed!
>                 my $rc = system
> ("git-diff-tree","-r","-M","-p","--pretty=medium",$commit);
>                 die "git-diff-tree failed" if $rc;
>                 print "\n";
>                 if ($lines[0] =~ /R\d{3}\t(.+)\t(.+)/) {
>                         $nextname = $1;
>                 }
>         }
>         else {
>                 #Not renamed
>                 my $rc = system
> ("git-diff-tree","-r","-m","-p","--pretty=medium",$commit, $nextname);
>                 die "git-diff-tree failed" if $rc;
>                 print "\n";
>         }
> }
>
> 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);
> }
>

← back to recent threads