Skip to content

Commit

Permalink
Merge branch 'cr/cvsimport-local-zone'
Browse files Browse the repository at this point in the history
Allows "cvsimport" to read per-author timezone from the author info
file.

* cr/cvsimport-local-zone:
  cvsimport: work around perl tzset issue
  git-cvsimport: allow author-specific timezones
  • Loading branch information
Junio C Hamano committed Nov 15, 2012
2 parents 6b87312 + c2b3af0 commit a2055c2
Show file tree
Hide file tree
Showing 6 changed files with 372 additions and 9 deletions.
8 changes: 5 additions & 3 deletions Documentation/git-cvsimport.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,19 @@ This option can be used several times to provide several detection regexes.
-A <author-conv-file>::
CVS by default uses the Unix username when writing its
commit logs. Using this option and an author-conv-file
in this format
maps the name recorded in CVS to author name, e-mail and
optional timezone:
+
---------
exon=Andreas Ericsson <ae@op5.se>
spawn=Simon Pawn <spawn@frog-pond.org>
spawn=Simon Pawn <spawn@frog-pond.org> America/Chicago

---------
+
'git cvsimport' will make it appear as those authors had
their GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL set properly
all along.
all along. If a timezone is specified, GIT_AUTHOR_DATE will
have the corresponding offset applied.
+
For convenience, this data is saved to `$GIT_DIR/cvs-authors`
each time the '-A' option is provided and read from that same
Expand Down
35 changes: 29 additions & 6 deletions git-cvsimport.perl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
use Time::Local;
use IO::Socket;
use IO::Pipe;
use POSIX qw(strftime dup2 ENOENT);
use POSIX qw(strftime tzset dup2 ENOENT);
use IPC::Open2;

$SIG{'PIPE'}="IGNORE";
$ENV{'TZ'}="UTC";
set_timezone('UTC');

our ($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_P, $opt_s,$opt_m,@opt_M,$opt_A,$opt_S,$opt_L, $opt_a, $opt_r, $opt_R);
my (%conv_author_name, %conv_author_email);
my (%conv_author_name, %conv_author_email, %conv_author_tz);

sub usage(;$) {
my $msg = shift;
Expand Down Expand Up @@ -59,6 +59,14 @@ ($)
$conv_author_name{$user} = $2;
$conv_author_email{$user} = $3;
}
# or with an optional timezone:
# spawn=Simon Pawn <spawn@frog-pond.org> America/Chicago
elsif (m/^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*(\S+?)\s*$/) {
$user = $1;
$conv_author_name{$user} = $2;
$conv_author_email{$user} = $3;
$conv_author_tz{$user} = $4;
}
# However, we also read from CVSROOT/users format
# to ease migration.
elsif (/^(\w+):(['"]?)(.+?)\2\s*$/) {
Expand All @@ -84,11 +92,22 @@ ($)
die("Failed to open $file for writing: $!");

foreach (keys %conv_author_name) {
print $f "$_=$conv_author_name{$_} <$conv_author_email{$_}>\n";
print $f "$_=$conv_author_name{$_} <$conv_author_email{$_}>";
print $f " $conv_author_tz{$_}" if ($conv_author_tz{$_});
print $f "\n";
}
close ($f);
}

# Versions of perl before 5.10.0 may not automatically check $TZ each
# time localtime is run (most platforms will do so only the first time).
# We can work around this by using tzset() to update the internal
# variable whenever we change the environment.
sub set_timezone {
$ENV{TZ} = shift;
tzset();
}

# convert getopts specs for use by git config
my %longmap = (
'A:' => 'authors-file',
Expand Down Expand Up @@ -795,7 +814,7 @@ ()
return $tree;
}

my ($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
my ($patchset,$date,$author_name,$author_email,$author_tz,$branch,$ancestor,$tag,$logmsg);
my (@old,@new,@skipped,%ignorebranch,@commit_revisions);

# commits that cvsps cannot place anywhere...
Expand Down Expand Up @@ -844,7 +863,9 @@ sub commit {
}
}

my $commit_date = strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date));
set_timezone($author_tz);
my $commit_date = strftime("%s %z", localtime($date));
set_timezone('UTC');
$ENV{GIT_AUTHOR_NAME} = $author_name;
$ENV{GIT_AUTHOR_EMAIL} = $author_email;
$ENV{GIT_AUTHOR_DATE} = $commit_date;
Expand Down Expand Up @@ -945,12 +966,14 @@ sub commit {
}
$state=3;
} elsif ($state == 3 and s/^Author:\s+//) {
$author_tz = "UTC";
s/\s+$//;
if (/^(.*?)\s+<(.*)>/) {
($author_name, $author_email) = ($1, $2);
} elsif ($conv_author_name{$_}) {
$author_name = $conv_author_name{$_};
$author_email = $conv_author_email{$_};
$author_tz = $conv_author_tz{$_} if ($conv_author_tz{$_});
} else {
$author_name = $author_email = $_;
}
Expand Down
71 changes: 71 additions & 0 deletions t/t9604-cvsimport-timestamps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/sh

test_description='git cvsimport timestamps'
. ./lib-cvs.sh

setup_cvs_test_repository t9604

test_expect_success 'check timestamps are UTC (TZ=CST6CDT)' '
TZ=CST6CDT git cvsimport -p"-x" -C module-1 module &&
git cvsimport -p"-x" -C module-1 module &&
(
cd module-1 &&
git log --format="%s %ai"
) >actual-1 &&
cat >expect-1 <<-EOF &&
Rev 16 2006-10-29 07:00:01 +0000
Rev 15 2006-10-29 06:59:59 +0000
Rev 14 2006-04-02 08:00:01 +0000
Rev 13 2006-04-02 07:59:59 +0000
Rev 12 2005-12-01 00:00:00 +0000
Rev 11 2005-11-01 00:00:00 +0000
Rev 10 2005-10-01 00:00:00 +0000
Rev 9 2005-09-01 00:00:00 +0000
Rev 8 2005-08-01 00:00:00 +0000
Rev 7 2005-07-01 00:00:00 +0000
Rev 6 2005-06-01 00:00:00 +0000
Rev 5 2005-05-01 00:00:00 +0000
Rev 4 2005-04-01 00:00:00 +0000
Rev 3 2005-03-01 00:00:00 +0000
Rev 2 2005-02-01 00:00:00 +0000
Rev 1 2005-01-01 00:00:00 +0000
EOF
test_cmp actual-1 expect-1
'

test_expect_success 'check timestamps with author-specific timezones' '
cat >cvs-authors <<-EOF &&
user1=User One <user1@domain.org>
user2=User Two <user2@domain.org> CST6CDT
user3=User Three <user3@domain.org> EST5EDT
user4=User Four <user4@domain.org> MST7MDT
EOF
git cvsimport -p"-x" -A cvs-authors -C module-2 module &&
(
cd module-2 &&
git log --format="%s %ai %an"
) >actual-2 &&
cat >expect-2 <<-EOF &&
Rev 16 2006-10-29 01:00:01 -0600 User Two
Rev 15 2006-10-29 01:59:59 -0500 User Two
Rev 14 2006-04-02 03:00:01 -0500 User Two
Rev 13 2006-04-02 01:59:59 -0600 User Two
Rev 12 2005-11-30 17:00:00 -0700 User Four
Rev 11 2005-10-31 19:00:00 -0500 User Three
Rev 10 2005-09-30 19:00:00 -0500 User Two
Rev 9 2005-09-01 00:00:00 +0000 User One
Rev 8 2005-07-31 18:00:00 -0600 User Four
Rev 7 2005-06-30 20:00:00 -0400 User Three
Rev 6 2005-05-31 19:00:00 -0500 User Two
Rev 5 2005-05-01 00:00:00 +0000 User One
Rev 4 2005-03-31 17:00:00 -0700 User Four
Rev 3 2005-02-28 19:00:00 -0500 User Three
Rev 2 2005-01-31 18:00:00 -0600 User Two
Rev 1 2005-01-01 00:00:00 +0000 User One
EOF
test_cmp actual-2 expect-2
'

test_done
1 change: 1 addition & 0 deletions t/t9604/cvsroot/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* -whitespace
2 changes: 2 additions & 0 deletions t/t9604/cvsroot/CVSROOT/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
history
val-tags
Loading

0 comments on commit a2055c2

Please sign in to comment.