git-svn log: handle unreachable revisions like "svn log"
When unreachable revisions are given to "svn log", it displays all commit logs in the given range that exist in the current tree. (If no commit logs are found in the current tree, it simply prints a single commit log separator.) This patch makes "git-svn log" behave the same way. Ten tests added to t/t9116-git-svn-log.sh. Signed-off-by: David D Kilzer <ddkilzer@kilzer.net> Acked-by: Eric Wong <normalperson@yhbt.net>
David D Kilzer committed
Nov 11, 2007 at 22:56 UTC
111947ef8cd3071faadb527c20047efd4330138a
2 files changed
+110
-19
git-svn.perl
+44
-19
@@ -2399,10 +2399,15 @@ sub rev_db_get {
2399
$ret;
2400
}
2401
2402
+# Finds the first svn revision that exists on (if $eq_ok is true) or
2403
+# before $rev for the current branch. It will not search any lower
2404
+# than $min_rev. Returns the git commit hash and svn revision number
2405
+# if found, else (undef, undef).
2406
sub find_rev_before {
2403
- my ($self, $rev, $eq_ok) = @_;
2407
+ my ($self, $rev, $eq_ok, $min_rev) = @_;
2408
--$rev unless $eq_ok;
2405
- while ($rev > 0) {
2409
+ $min_rev ||= 1;
2410
+ while ($rev >= $min_rev) {
2411
if (my $c = $self->rev_db_get($rev)) {
2412
return ($rev, $c);
2413
}
@@ -2411,6 +2416,23 @@ sub find_rev_before {
2416
return (undef, undef);
2417
}
2418
2419
+# Finds the first svn revision that exists on (if $eq_ok is true) or
2420
+# after $rev for the current branch. It will not search any higher
2421
+# than $max_rev. Returns the git commit hash and svn revision number
2422
+# if found, else (undef, undef).
2423
+sub find_rev_after {
2424
+ my ($self, $rev, $eq_ok, $max_rev) = @_;
2425
+ ++$rev unless $eq_ok;
2426
+ $max_rev ||= $self->rev_db_max();
2427
+ while ($rev <= $max_rev) {
2428
+ if (my $c = $self->rev_db_get($rev)) {
2429
+ return ($rev, $c);
2430
+ }
2431
+ ++$rev;
2432
+ }
2433
+ return (undef, undef);
2434
+}
2435
+
2436
sub _new {
2437
my ($class, $repo_id, $ref_id, $path) = @_;
2438
unless (defined $repo_id && length $repo_id) {
@@ -3701,6 +3723,7 @@ package Git::SVN::Log;
3723
use strict;
3724
use warnings;
3725
use POSIX qw/strftime/;
3726
+use constant commit_log_separator => ('-' x 72) . "\n";
3727
use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
3728
%rusers $show_commit $incremental/;
3729
my $l_fmt;
@@ -3794,19 +3817,19 @@ sub git_svn_log_cmd {
3817
push @cmd, $c;
3818
}
3819
} elsif (defined $r_max) {
3797
- my ($c_min, $c_max);
3798
- $c_max = $gs->rev_db_get($r_max);
3799
- $c_min = $gs->rev_db_get($r_min);
3800
- if (defined $c_min && defined $c_max) {
3801
- if ($r_max > $r_min) {
3802
- push @cmd, "--boundary", "$c_min..$c_max";
3803
- } else {
3804
- push @cmd, "--boundary", "$c_max..$c_min";
3805
- }
3806
- } elsif ($r_max > $r_min) {
3807
- push @cmd, $c_max;
3820
+ if ($r_max < $r_min) {
3821
+ ($r_min, $r_max) = ($r_max, $r_min);
3822
+ }
3823
+ my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
3824
+ my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
3825
+ # If there are no commits in the range, both $c_max and $c_min
3826
+ # will be undefined. If there is at least 1 commit in the
3827
+ # range, both will be defined.
3828
+ return () if !defined $c_min || !defined $c_max;
3829
+ if ($c_min eq $c_max) {
3830
+ push @cmd, '--max-count=1', $c_min;
3831
} else {
3809
- push @cmd, $c_min;
3832
+ push @cmd, '--boundary', "$c_min..$c_max";
3833
}
3834
}
3835
return (@cmd, @files);
@@ -3914,7 +3937,7 @@ sub show_commit_changed_paths {
3937
3938
sub show_commit_normal {
3939
my ($c) = @_;
3917
- print '-' x72, "\nr$c->{r} | ";
3940
+ print commit_log_separator, "r$c->{r} | ";
3941
print "$c->{c} | " if $show_commit;
3942
print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3943
localtime($c->{t_utc})), ' | ';
@@ -3975,6 +3998,10 @@ sub cmd_show_log {
3998
3999
config_pager();
4000
@args = git_svn_log_cmd($r_min, $r_max, @args);
4001
+ if (!@args) {
4002
+ print commit_log_separator unless $incremental || $oneline;
4003
+ return;
4004
+ }
4005
my $log = command_output_pipe(@args);
4006
run_pager();
4007
my (@k, $c, $d, $stat);
@@ -4023,14 +4050,12 @@ sub cmd_show_log {
4050
process_commit($c, $r_min, $r_max, \@k);
4051
}
4052
if (@k) {
4026
- my $swap = $r_max;
4027
- $r_max = $r_min;
4028
- $r_min = $swap;
4053
+ ($r_min, $r_max) = ($r_max, $r_min);
4054
process_commit($_, $r_min, $r_max) foreach reverse @k;
4055
}
4056
out:
4057
close $log;
4033
- print '-' x72,"\n" unless $incremental || $oneline;
4058
+ print commit_log_separator unless $incremental || $oneline;
4059
}
4060
4061
package Git::SVN::Migration;
t/t9116-git-svn-log.sh
+66
@@ -30,6 +30,12 @@ test_expect_success 'setup repository and import' "
30
git reset --hard trunk &&
31
echo aye >> README &&
32
git commit -a -m aye &&
33
+ git svn dcommit &&
34
+ git reset --hard b &&
35
+ echo spy >> README &&
36
+ git commit -a -m spy &&
37
+ echo try >> README &&
38
+ git commit -a -m try &&
39
git svn dcommit
40
"
41
@@ -59,4 +65,64 @@ test_expect_success 'test descending revision range' "
65
git svn log -r 4:1 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r4-r2-r1 -
66
"
67
68
+printf 'r1 \nr2 \n' > expected-range-r1-r2
69
+
70
+test_expect_success 'test ascending revision range with unreachable revision' "
71
+ git reset --hard trunk &&
72
+ git svn log -r 1:3 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r1-r2 -
73
+ "
74
+
75
+printf 'r2 \nr1 \n' > expected-range-r2-r1
76
+
77
+test_expect_success 'test descending revision range with unreachable revision' "
78
+ git reset --hard trunk &&
79
+ git svn log -r 3:1 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r2-r1 -
80
+ "
81
+
82
+printf 'r2 \n' > expected-range-r2
83
+
84
+test_expect_success 'test ascending revision range with unreachable upper boundary revision and 1 commit' "
85
+ git reset --hard trunk &&
86
+ git svn log -r 2:3 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r2 -
87
+ "
88
+
89
+test_expect_success 'test descending revision range with unreachable upper boundary revision and 1 commit' "
90
+ git reset --hard trunk &&
91
+ git svn log -r 3:2 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r2 -
92
+ "
93
+
94
+printf 'r4 \n' > expected-range-r4
95
+
96
+test_expect_success 'test ascending revision range with unreachable lower boundary revision and 1 commit' "
97
+ git reset --hard trunk &&
98
+ git svn log -r 3:4 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r4 -
99
+ "
100
+
101
+test_expect_success 'test descending revision range with unreachable lower boundary revision and 1 commit' "
102
+ git reset --hard trunk &&
103
+ git svn log -r 4:3 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r4 -
104
+ "
105
+
106
+printf -- '------------------------------------------------------------------------\n' > expected-separator
107
+
108
+test_expect_success 'test ascending revision range with unreachable boundary revisions and no commits' "
109
+ git reset --hard trunk &&
110
+ git svn log -r 5:6 | diff -u expected-separator -
111
+ "
112
+
113
+test_expect_success 'test descending revision range with unreachable boundary revisions and no commits' "
114
+ git reset --hard trunk &&
115
+ git svn log -r 6:5 | diff -u expected-separator -
116
+ "
117
+
118
+test_expect_success 'test ascending revision range with unreachable boundary revisions and 1 commit' "
119
+ git reset --hard trunk &&
120
+ git svn log -r 3:5 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r4 -
121
+ "
122
+
123
+test_expect_success 'test descending revision range with unreachable boundary revisions and 1 commit' "
124
+ git reset --hard trunk &&
125
+ git svn log -r 5:3 | grep '^r[0-9]' | cut -d'|' -f1 | diff -u expected-range-r4 -
126
+ "
127
+
128
test_done