Skip to content

Commit

Permalink
git subrepo pull .git-utils
Browse files Browse the repository at this point in the history
subrepo:
  subdir:   ".git-utils"
  merged:   "967dd93f84"
upstream:
  origin:   "[email protected]:daisy/pipeline.git"
  branch:   "git-utils"
  commit:   "c3c0ca6aeb"
git-subrepo:
  version:  "0.3.1"
  origin:   "???"
  commit:   "???"
  • Loading branch information
bertfrees committed Sep 11, 2018
1 parent ba7e8d8 commit 79a53ba
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .git-utils/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
remote = [email protected]:daisy/pipeline.git
branch = git-utils
commit = c3c0ca6aeb203fbde1888fd555d9971d5e469449
parent = 5bcc1637ed85650d38df2e75d326ecf9a3c487d5
parent = 68652ff423460f458583b45f1568b9e9e0526ceb
cmdver = 0.3.1
80 changes: 58 additions & 22 deletions .git-utils/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package Utils;
use strict;
use warnings;
use Exporter;
use Try::Tiny;

our @ISA= qw( Exporter );

our @EXPORT = qw(
die_with_stack_trace
git
revision_exists
is_ancestor
in_remote_branches
Expand All @@ -18,6 +21,35 @@ our @EXPORT = qw(
distance
);

sub stack_trace {
my $trace = "";
my $i = 1;
while ((my @call_details = (caller($i++)))){
$trace = $trace." ".$call_details[1].":".$call_details[2]." in function ".$call_details[3]."\n";
}
return $trace;
}

sub die_with_stack_trace {
my @msg = @_;
# print STDERR "__\n";
# for (split("\n", join("", @msg)."\nStack Trace:\n".stack_trace())) {
# print STDERR "| $_\n";
# }
die join("", @msg)."\nStack Trace:\n".stack_trace();
}

sub git {
my ($cmd) = @_;
my @result = `git $cmd`;
$? == 0 or die_with_stack_trace "git command failed: git $cmd";
if (wantarray) {
return @result;
} elsif (defined wantarray) {
return join("",@result);
}
}

sub revision_exists {
my ($rev) = @_;
if ($rev =~ /^\$/) {
Expand All @@ -36,7 +68,7 @@ sub is_ancestor {
sub in_remote_branches {
my ($rev, $remote) = @_;
my @branches;
for (`git branch -r --contains $rev`) {
for (git "branch -r --contains $rev") {
if ($_ =~ /^ *\Q$remote\E\/(.+)$/) {
push @branches, $1;
}
Expand All @@ -46,61 +78,65 @@ sub in_remote_branches {

sub cmp_tree {
my ($revision_a, $revision_b) = @_;
my $tree_a = `git ls-tree -r $revision_a 2>/dev/null | awk '{print \$3}'`;
my $tree_b = `git ls-tree -r $revision_b 2>/dev/null | awk '{print \$3}'`;
my $tree_a = git "ls-tree -r $revision_a 2>/dev/null | awk '{print \$3}'";
my $tree_b = git "ls-tree -r $revision_b 2>/dev/null | awk '{print \$3}'";
return ("$tree_a" eq "$tree_b");
}

sub cmp_tree_with_subtree {
my ($revision_a, $revision_b, $subdir_b) = @_;
my $tree_a = `git ls-tree -r $revision_a 2>/dev/null | awk '{print \$3}'`;
my $tree_b = `git ls-tree -r $revision_b $subdir_b 2>/dev/null | grep -vF $subdir_b/.gitrepo | awk '{print \$3}'`;
my $tree_a = git "ls-tree -r $revision_a 2>/dev/null | awk '{print \$3}'";
my $tree_b = git "ls-tree -r $revision_b $subdir_b 2>/dev/null | grep -vF $subdir_b/.gitrepo | awk '{print \$3}'";
return ("$tree_a" eq "$tree_b");
}

sub cmp_subtree_with_subtree {
my ($revision_a, $revision_b, $subdir) = @_;
my $tree_a = `git ls-tree -r $revision_a $subdir 2>/dev/null | grep -vF $subdir/.gitrepo | awk '{print \$3}'`;
my $tree_b = `git ls-tree -r $revision_b $subdir 2>/dev/null | grep -vF $subdir/.gitrepo | awk '{print \$3}'`;
my $tree_a = git "ls-tree -r $revision_a $subdir 2>/dev/null | grep -vF $subdir/.gitrepo | awk '{print \$3}'";
my $tree_b = git "ls-tree -r $revision_b $subdir 2>/dev/null | grep -vF $subdir/.gitrepo | awk '{print \$3}'";
return ("$tree_a" eq "$tree_b");
}

sub rev_parse {
my ($revision) = @_;
chomp(my $commit = `git rev-parse $revision`);
chomp(my $commit = git "rev-parse $revision");
return $commit;
}

my %git_subrepo_commits = ();

sub find_last_git_subrepo_commit {
my ($subdir, $start) = @_;
$start = $start || "HEAD";
my ($subdir, $to, $from) = @_;
$to = $to || "HEAD";
my @commits; {
if (exists $git_subrepo_commits{$start}) {
@commits = @{$git_subrepo_commits{$start}};
if (exists $git_subrepo_commits{($from||"")."..".$to}) {
@commits = @{$git_subrepo_commits{($from||"")."..".$to}};
} else {
@commits = `git log $start --grep='subrepo:' --format=format:%H`;
if ($from) {
@commits = git "log $to ^$from --grep='subrepo:' --format=format:%H";
} else {
@commits = git "log $to --grep='subrepo:' --format=format:%H";
}
for (@commits) {
chomp;
}
@{$git_subrepo_commits{$start}} = @commits;
@{$git_subrepo_commits{($from||"")."..".$to}} = @commits;
}
}
for my $commit (@commits) {
my $commit_message = `git log --format=%B -n 1 $commit`;
my $commit_message = git "log --format=%B -n 1 $commit";
if ($commit_message =~ /^subrepo:\n +subdir: *"([^"]*)"\n merged: *"([0-9a-f]{7,})"/m) {
if ($1 eq $subdir) {
if (not revision_exists($2)) {
print STDERR "ERROR: corrupt git-subrepo commit: $commit";
print STDERR " (commit $2 does not exist: might have been purged, or was never pushed)\n";
return;
die_with_stack_trace "corrupt git-subrepo commit: "
. substr($commit, 0, 7)
. " (commit $2 does not exist: might have been purged, or was never pushed)";
} elsif (cmp_tree_with_subtree($2, $commit, $subdir)) {
return ($commit, rev_parse($2));
} else {
print STDERR "ERROR: corrupt git-subrepo commit: $commit";
print STDERR " (is not tree-equal to commit $2)\n";
return;
die_with_stack_trace "corrupt git-subrepo commit: "
. substr($commit, 0, 7)
. " (is not tree-equal to commit $2)";
}
}
}
Expand All @@ -110,7 +146,7 @@ sub find_last_git_subrepo_commit {

sub distance {
my ($from, $to) = @_;
my @commits = defined $from ? `git rev-list $to ^$from` : `git rev-list $to`;
my @commits = defined $from ? git "rev-list $to ^$from" : git "rev-list $to";
for (@commits) {
chomp;
}
Expand Down
Loading

0 comments on commit 79a53ba

Please sign in to comment.