Ninguno de los git show
sugerencias satisfacer verdaderamente porque (mucho que lo intentaba), no puedo encontrar una manera de no obtener el metadata cruft desde la parte superior de la salida. El espíritu de cat (1) es solo para mostrar los contenidos. Esto (abajo) toma un nombre de archivo y un número opcional. El número es cómo se compromete a volver. (Confirma que se modificó ese archivo. No se cuentan los errores que no cambian el archivo de destino).)
gitcat.pl filename.txt
gitcat.pl -3 filename.txt
muestra el contenido de filename.txt al cierre del último cometer de filename.txt y los contenidos a partir de 3 confirmaciones antes de eso.
#!/usr/bin/perl -w
use strict;
use warnings;
use FileHandle;
use Cwd;
# Have I mentioned lately how much I despise git?
(my $prog = $0) =~ s!.*/!!;
my $usage = "Usage: $prog [revisions-ago] filename\n";
die($usage) if(! @ARGV);
my($revision, $fname) = @ARGV;
if(! $fname && -f $revision) {
($fname, $revision) = ($revision, 0);
}
gitcat($fname, $revision);
sub gitcat {
my($fname, $revision) = @_;
my $rev = $revision;
my $file = FileHandle->new("git log --format=oneline '$fname' |");
# Get the $revisionth line from the log.
my $line;
for(0..$revision) {
$line = $file->getline();
}
die("Could not get line $revision from the log for $fname.\n")
if(! $line);
# Get the hash from that.
my $hash = substr($line, 0, 40);
if(! $hash =~ m/^([0-9a-fA-F]{40})/x) {
die("The commit hash does not look a hash.\n");
}
# Git needs the path from the root of the repo to the file because it can
# not work out the path itself.
my $path = pathhere();
if(! $path) {
die("Could not find the git repository.\n");
}
exec("git cat-file blob $hash:$path/'$fname'");
}
# Get the path from the git repo to the current dir.
sub pathhere {
my $cwd = getcwd();
my @cwd = split('/', $cwd);
my @path;
while(! -d "$cwd/.git") {
my $path = pop(@cwd);
unshift(@path, $path);
if(! @cwd) {
die("Did not find .git in or above your pwd.\n");
}
$cwd = join('/', @cwd);
}
return join('/', map { "'$_'"; } @path);
}
Mi solución final fue usar primero "git ls-files---full nombre" para obtener la ruta completa en relación con la parte superior del depósito, y luego "git show HEAD: " –
posible duplicado de [¿Cómo recuperar un único archivo de una revisión específica en Git?] (http://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file -from-specific-revision-in-git) – ks1322
posible duplicado de [¿Hay un comando rápido de git para ver una versión anterior de un archivo?] (http://stackoverflow.com/questions/338436/is-there-a -quick-git-command-to-see-an-old-version-of-a-fil e) –