Merge branch 'jc/gitpm' into next

* jc/gitpm:
  Revert "Make it possible to set up libgit directly (instead of from the environment)"
  Revert "Git.pm: Introduce fast get_object() method"
  Revert "Convert git-annotate to use Git.pm"
This commit is contained in:
Junio C Hamano
2006-09-02 23:02:44 -07:00
8 changed files with 175 additions and 187 deletions

View File

@@ -117,9 +117,6 @@ extern unsigned int active_nr, active_alloc, active_cache_changed;
extern struct cache_tree *active_cache_tree;
extern int cache_errno;
extern void setup_git(char *new_git_dir, char *new_git_object_dir,
char *new_git_index_file, char *new_git_graft_file);
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"

View File

@@ -163,14 +163,6 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
return 0;
}
void free_commit_grafts(void)
{
int pos = commit_graft_nr;
while (pos >= 0)
free(commit_graft[pos--]);
commit_graft_nr = 0;
}
struct commit_graft *read_graft_line(char *buf, int len)
{
/* The format is just "Commit Parent1 Parent2 ...\n" */
@@ -223,18 +215,11 @@ int read_graft_file(const char *graft_file)
static void prepare_commit_graft(void)
{
static int commit_graft_prepared;
static char *last_graft_file;
char *graft_file = get_graft_file();
if (last_graft_file) {
if (!strcmp(graft_file, last_graft_file))
return;
free_commit_grafts();
}
if (last_graft_file)
free(last_graft_file);
last_graft_file = strdup(graft_file);
char *graft_file;
if (commit_graft_prepared)
return;
graft_file = get_graft_file();
read_graft_file(graft_file);
commit_graft_prepared = 1;
}

View File

@@ -25,61 +25,29 @@ int zlib_compression_level = Z_DEFAULT_COMPRESSION;
int pager_in_use;
int pager_use_color = 1;
static int dyn_git_object_dir, dyn_git_index_file, dyn_git_graft_file;
static const char *git_dir;
static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;
void setup_git(char *new_git_dir, char *new_git_object_dir,
char *new_git_index_file, char *new_git_graft_file)
static void setup_git_env(void)
{
git_dir = new_git_dir;
git_dir = getenv(GIT_DIR_ENVIRONMENT);
if (!git_dir)
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
if (dyn_git_object_dir)
free(git_object_dir);
git_object_dir = new_git_object_dir;
git_object_dir = getenv(DB_ENVIRONMENT);
if (!git_object_dir) {
git_object_dir = xmalloc(strlen(git_dir) + 9);
sprintf(git_object_dir, "%s/objects", git_dir);
dyn_git_object_dir = 1;
} else {
dyn_git_object_dir = 0;
}
if (git_refs_dir)
free(git_refs_dir);
git_refs_dir = xmalloc(strlen(git_dir) + 6);
sprintf(git_refs_dir, "%s/refs", git_dir);
if (dyn_git_index_file)
free(git_index_file);
git_index_file = new_git_index_file;
git_index_file = getenv(INDEX_ENVIRONMENT);
if (!git_index_file) {
git_index_file = xmalloc(strlen(git_dir) + 7);
sprintf(git_index_file, "%s/index", git_dir);
dyn_git_index_file = 1;
} else {
dyn_git_index_file = 0;
}
if (dyn_git_graft_file)
free(git_graft_file);
git_graft_file = new_git_graft_file;
if (!git_graft_file) {
git_graft_file = getenv(GRAFT_ENVIRONMENT);
if (!git_graft_file)
git_graft_file = xstrdup(git_path("info/grafts"));
dyn_git_graft_file = 1;
} else {
dyn_git_graft_file = 0;
}
}
static void setup_git_env(void)
{
setup_git(getenv(GIT_DIR_ENVIRONMENT),
getenv(DB_ENVIRONMENT),
getenv(INDEX_ENVIRONMENT),
getenv(GRAFT_ENVIRONMENT));
}
const char *get_git_dir(void)

View File

@@ -11,7 +11,6 @@ use strict;
use Getopt::Long;
use POSIX qw(strftime gmtime);
use File::Basename qw(basename dirname);
use Git;
sub usage() {
print STDERR "Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
@@ -30,7 +29,7 @@ sub usage() {
exit(1);
}
our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file, $repo) = (0, 0, 1);
our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file) = (0, 0, 1);
my $rc = GetOptions( "long|l" => \$longrev,
"time|t" => \$rawtime,
@@ -53,8 +52,6 @@ my @stack = (
},
);
$repo = Git->repository();
our @filelines = ();
if (defined $starting_rev) {
@@ -105,11 +102,15 @@ while (my $bound = pop @stack) {
push @revqueue, $head;
init_claim( defined $starting_rev ? $head : 'dirty');
unless (defined $starting_rev) {
my %ident;
@ident{'author', 'author_email', 'author_date'} = $repo->ident('author');
my $diff = $repo->command_output_pipe('diff', '-R', 'HEAD', '--', $filename);
_git_diff_parse($diff, [$head], "dirty", %ident);
$repo->command_close_pipe($diff);
my $diff = open_pipe("git","diff","HEAD", "--",$filename)
or die "Failed to call git diff to check for dirty state: $!";
_git_diff_parse($diff, [$head], "dirty", (
'author' => gitvar_name("GIT_AUTHOR_IDENT"),
'author_date' => sprintf("%s +0000",time()),
)
);
close($diff);
}
handle_rev();
@@ -179,7 +180,8 @@ sub git_rev_list {
open($revlist, '<' . $rev_file)
or die "Failed to open $rev_file : $!";
} else {
$revlist = $repo->command_output_pipe('rev-list', '--parents', '--remove-empty', $rev, '--', $file);
$revlist = open_pipe("git-rev-list","--parents","--remove-empty",$rev,"--",$file)
or die "Failed to exec git-rev-list: $!";
}
my @revs;
@@ -188,7 +190,7 @@ sub git_rev_list {
my ($rev, @parents) = split /\s+/, $line;
push @revs, [ $rev, @parents ];
}
$repo->command_close_pipe($revlist);
close($revlist);
printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
return @revs;
@@ -197,7 +199,8 @@ sub git_rev_list {
sub find_parent_renames {
my ($rev, $file) = @_;
my $patch = $repo->command_output_pipe('diff-tree', '-M50', '-r', '--name-status', '-z', $rev);
my $patch = open_pipe("git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
or die "Failed to exec git-diff: $!";
local $/ = "\0";
my %bound;
@@ -223,7 +226,7 @@ sub find_parent_renames {
}
}
}
$repo->command_close_pipe($patch);
close($patch);
return \%bound;
}
@@ -232,9 +235,14 @@ sub find_parent_renames {
sub git_find_parent {
my ($rev, $filename) = @_;
my $parentline = $repo->command_oneline('rev-list', '--remove-empty',
'--parents', '--max-count=1', $rev, '--', $filename);
my ($revfound, $parent) = split m/\s+/, $parentline;
my $revparent = open_pipe("git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
or die "Failed to open git-rev-list to find a single parent: $!";
my $parentline = <$revparent>;
chomp $parentline;
my ($revfound,$parent) = split m/\s+/, $parentline;
close($revparent);
return $parent;
}
@@ -242,16 +250,29 @@ sub git_find_parent {
sub git_find_all_parents {
my ($rev) = @_;
my $parentline = $repo->command_oneline("rev-list","--remove-empty", "--parents","--max-count=1","$rev");
my $revparent = open_pipe("git-rev-list","--remove-empty", "--parents","--max-count=1","$rev")
or die "Failed to open git-rev-list to find a single parent: $!";
my $parentline = <$revparent>;
chomp $parentline;
my ($origrev, @parents) = split m/\s+/, $parentline;
close($revparent);
return @parents;
}
sub git_merge_base {
my ($rev1, $rev2) = @_;
my $base = $repo->command_oneline("merge-base", $rev1, $rev2);
my $mb = open_pipe("git-merge-base", $rev1, $rev2)
or die "Failed to open git-merge-base: $!";
my $base = <$mb>;
chomp $base;
close($mb);
return $base;
}
@@ -316,7 +337,7 @@ sub git_diff_parse {
my ($parents, $rev, %revinfo) = @_;
my @pseudo_parents;
my @command = ("diff-tree");
my @command = ("git-diff-tree");
my $revision_spec;
if (scalar @$parents == 1) {
@@ -345,11 +366,12 @@ sub git_diff_parse {
push @command, "-p", "-M", $revision_spec, "--", @filenames;
my $diff = $repo->command_output_pipe(@command);
my $diff = open_pipe( @command )
or die "Failed to call git-diff for annotation: $!";
_git_diff_parse($diff, \@pseudo_parents, $rev, %revinfo);
$repo->command_close_pipe($diff);
close($diff);
}
sub _git_diff_parse {
@@ -525,25 +547,36 @@ sub git_cat_file {
my $blob = git_ls_tree($rev, $filename);
die "Failed to find a blob for $filename in rev $rev\n" if !defined $blob;
my @lines = split(/\n/, $repo->get_object('blob', $blob));
pop @lines unless $lines[$#lines]; # Trailing newline
my $catfile = open_pipe("git","cat-file", "blob", $blob)
or die "Failed to git-cat-file blob $blob (rev $rev, file $filename): " . $!;
my @lines;
while(<$catfile>) {
chomp;
push @lines, $_;
}
close($catfile);
return @lines;
}
sub git_ls_tree {
my ($rev, $filename) = @_;
my $lstree = $repo->command_output_pipe('ls-tree', $rev, $filename);
my $lstree = open_pipe("git","ls-tree",$rev,$filename)
or die "Failed to call git ls-tree: $!";
my ($mode, $type, $blob, $tfilename);
while(<$lstree>) {
chomp;
($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
last if ($tfilename eq $filename);
}
$repo->command_close_pipe($lstree);
close($lstree);
return $blob if ($tfilename eq $filename);
die "git-ls-tree failed to find blob for $filename";
}
@@ -559,17 +592,25 @@ sub claim_line {
sub git_commit_info {
my ($rev) = @_;
my $commit = $repo->get_object('commit', $rev);
my $commit = open_pipe("git-cat-file", "commit", $rev)
or die "Failed to call git-cat-file: $!";
my %info;
while ($commit =~ /(.*?)\n/g) {
my $line = $1;
if ($line =~ s/^author //) {
@info{'author', 'author_email', 'author_date'} = $repo->ident($line);
} elsif ($line =~ s/^committer//) {
@info{'committer', 'committer_email', 'committer_date'} = $repo->ident($line);
while(<$commit>) {
chomp;
last if (length $_ == 0);
if (m/^author (.*) <(.*)> (.*)$/) {
$info{'author'} = $1;
$info{'author_email'} = $2;
$info{'author_date'} = $3;
} elsif (m/^committer (.*) <(.*)> (.*)$/) {
$info{'committer'} = $1;
$info{'committer_email'} = $2;
$info{'committer_date'} = $3;
}
}
close($commit);
return %info;
}
@@ -587,3 +628,81 @@ sub format_date {
my $t = $timestamp + $minutes * 60;
return strftime("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($t));
}
# Copied from git-send-email.perl - We need a Git.pm module..
sub gitvar {
my ($var) = @_;
my $fh;
my $pid = open($fh, '-|');
die "$!" unless defined $pid;
if (!$pid) {
exec('git-var', $var) or die "$!";
}
my ($val) = <$fh>;
close $fh or die "$!";
chomp($val);
return $val;
}
sub gitvar_name {
my ($name) = @_;
my $val = gitvar($name);
my @field = split(/\s+/, $val);
return join(' ', @field[0...(@field-4)]);
}
sub open_pipe {
if ($^O eq '##INSERT_ACTIVESTATE_STRING_HERE##') {
return open_pipe_activestate(@_);
} else {
return open_pipe_normal(@_);
}
}
sub open_pipe_activestate {
tie *fh, "Git::ActiveStatePipe", @_;
return *fh;
}
sub open_pipe_normal {
my (@execlist) = @_;
my $pid = open my $kid, "-|";
defined $pid or die "Cannot fork: $!";
unless ($pid) {
exec @execlist;
die "Cannot exec @execlist: $!";
}
return $kid;
}
package Git::ActiveStatePipe;
use strict;
sub TIEHANDLE {
my ($class, @params) = @_;
my $cmdline = join " ", @params;
my @data = qx{$cmdline};
bless { i => 0, data => \@data }, $class;
}
sub READLINE {
my $self = shift;
if ($self->{i} >= scalar @{$self->{data}}) {
return undef;
}
return $self->{'data'}->[ $self->{i}++ ];
}
sub CLOSE {
my $self = shift;
delete $self->{data};
delete $self->{i};
}
sub EOF {
my $self = shift;
return ($self->{i} >= scalar @{$self->{data}});
}

View File

@@ -98,8 +98,6 @@ XSLoader::load('Git', $VERSION);
}
my $instance_id = 0;
=head1 CONSTRUCTORS
@@ -217,7 +215,7 @@ sub repository {
delete $opts{Directory};
}
$self = { opts => \%opts, id => $instance_id++ };
$self = { opts => \%opts };
bless $self, $class;
}
@@ -572,24 +570,6 @@ sub ident_person {
}
=item get_object ( TYPE, SHA1 )
Return contents of the given object in a scalar string. If the object has
not been found, undef is returned; however, do not rely on this! Currently,
if you use multiple repositories at once, get_object() on one repository
_might_ return the object even though it exists only in another repository.
(But do not rely on this behaviour either.)
The method must be called on a repository instance.
Implementation of this method is very fast; no external command calls
are involved. That's why it is broken, too. ;-)
=cut
# Implemented in Git.xs.
=item hash_object ( TYPE, FILENAME )
=item hash_object ( TYPE, FILEHANDLE )
@@ -853,10 +833,11 @@ sub _call_gate {
if (defined $self) {
# XXX: We ignore the WorkingCopy! To properly support
# that will require heavy changes in libgit.
# For now, when we will need to do it we could temporarily
# chdir() there and then chdir() back after the call is done.
xs__call_gate($self->{id}, $self->repo_path());
# XXX: And we ignore everything else as well. libgit
# at least needs to be extended to let us specify
# the $GIT_DIR instead of looking it up in environment.
#xs_call_gate($self->{opts}->{Repository});
}
# Having to call throw from the C code is a sure path to insanity.

View File

@@ -52,21 +52,7 @@ BOOT:
}
void
xs__call_gate(repoid, git_dir)
long repoid;
char *git_dir;
CODE:
{
static long last_repoid;
if (repoid != last_repoid) {
setup_git(git_dir,
getenv(DB_ENVIRONMENT),
getenv(INDEX_ENVIRONMENT),
getenv(GRAFT_ENVIRONMENT));
last_repoid = repoid;
}
}
# /* TODO: xs_call_gate(). See Git.pm. */
char *
@@ -111,30 +97,6 @@ CODE:
free((char **) argv);
}
SV *
xs_get_object(type, id)
char *type;
char *id;
CODE:
{
unsigned char sha1[20];
unsigned long size;
void *buf;
if (strlen(id) != 40 || get_sha1_hex(id, sha1) < 0)
XSRETURN_UNDEF;
buf = read_sha1_file(sha1, type, &size);
if (!buf)
XSRETURN_UNDEF;
RETVAL = newSVpvn(buf, size);
free(buf);
}
OUTPUT:
RETVAL
char *
xs_hash_object_pipe(type, fd)
char *type;

View File

@@ -126,22 +126,16 @@ static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
char *sha1_file_name(const unsigned char *sha1)
{
static char *name, *base;
static const char *last_objdir;
const char *sha1_file_directory = get_object_directory();
if (!last_objdir || strcmp(last_objdir, sha1_file_directory)) {
if (!base) {
const char *sha1_file_directory = get_object_directory();
int len = strlen(sha1_file_directory);
if (base)
free(base);
base = xmalloc(len + 60);
memcpy(base, sha1_file_directory, len);
memset(base+len, 0, 60);
base[len] = '/';
base[len+3] = '/';
name = base + len + 1;
if (last_objdir)
free((char *) last_objdir);
last_objdir = strdup(sha1_file_directory);
}
fill_sha1_path(name, sha1);
return base;
@@ -151,20 +145,14 @@ char *sha1_pack_name(const unsigned char *sha1)
{
static const char hex[] = "0123456789abcdef";
static char *name, *base, *buf;
static const char *last_objdir;
const char *sha1_file_directory = get_object_directory();
int i;
if (!last_objdir || strcmp(last_objdir, sha1_file_directory)) {
if (!base) {
const char *sha1_file_directory = get_object_directory();
int len = strlen(sha1_file_directory);
if (base)
free(base);
base = xmalloc(len + 60);
sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
name = base + len + 11;
if (last_objdir)
free((char *) last_objdir);
last_objdir = strdup(sha1_file_directory);
}
buf = name;
@@ -182,20 +170,14 @@ char *sha1_pack_index_name(const unsigned char *sha1)
{
static const char hex[] = "0123456789abcdef";
static char *name, *base, *buf;
static const char *last_objdir;
const char *sha1_file_directory = get_object_directory();
int i;
if (!last_objdir || strcmp(last_objdir, sha1_file_directory)) {
if (!base) {
const char *sha1_file_directory = get_object_directory();
int len = strlen(sha1_file_directory);
if (base)
free(base);
base = xmalloc(len + 60);
sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
name = base + len + 11;
if (last_objdir)
free((char *) last_objdir);
last_objdir = strdup(sha1_file_directory);
}
buf = name;

View File

@@ -12,21 +12,15 @@ static int find_short_object_filename(int len, const char *name, unsigned char *
char hex[40];
int found = 0;
static struct alternate_object_database *fakeent;
static const char *last_objdir;
const char *objdir = get_object_directory();
if (!last_objdir || strcmp(last_objdir, objdir)) {
if (!fakeent) {
const char *objdir = get_object_directory();
int objdir_len = strlen(objdir);
int entlen = objdir_len + 43;
if (fakeent)
free(fakeent);
fakeent = xmalloc(sizeof(*fakeent) + entlen);
memcpy(fakeent->base, objdir, objdir_len);
fakeent->name = fakeent->base + objdir_len + 1;
fakeent->name[-1] = '/';
if (last_objdir)
free((char *) last_objdir);
last_objdir = strdup(objdir);
}
fakeent->next = alt_odb_list;