@@ -117,15 +117,6 @@ sub may_exit {
117117sub git_shift {
118118 my ($function , @revspec ) = @_ ;
119119
120- if (` git diff --shortstat` =~ / \w / ) {
121- print STDERR " $0 : work directory is not clean.\n " ;
122- exit 1;
123- }
124- if (` git diff --shortstat --cached` =~ / \w / ) {
125- print STDERR " $0 : index is not clean.\n " ;
126- exit 1;
127- }
128-
129120 my %revs = map { ($_ => 0) } map { expand_revspec($_ ) } @revspec ;
130121
131122 if (!%revs ) {
@@ -142,19 +133,11 @@ sub git_shift {
142133 my ($rev , @rev_info ) = split (' ' , $line );
143134 if (exists ($revs {$rev })) {
144135 $revs {$rev }++;
145- $found ||= do {
146- if (!$start ) {
147- print STDERR " $0 : cannot modify the root commit.\n " ;
148- exit 1;
149- }
150- 1;
151- };
136+ $found = 1;
152137 }
153138 if ($found ) {
154139 push @all_revs , $rev ;
155140 $rev_info {$rev } = \@rev_info ;
156- } else {
157- $start = $rev
158141 }
159142 }
160143 waitpid $git , 0;
@@ -167,12 +150,7 @@ sub git_shift {
167150 }
168151 }
169152
170- unless ($opt_n ) {
171- system ' git' , ' reset' , ' --hard' , $start ;
172- exit ($? >> 8) if $? ;
173- }
174-
175- my $keep_prev_reflog = 1;
153+ my (%commit_map , $last_rev );
176154
177155 for my $rev (@all_revs ) {
178156 my ($atime , $atz , $ctime , $ctz ) = @{$rev_info {$rev }};
@@ -193,11 +171,12 @@ sub git_shift {
193171 $changed = 1;
194172 }
195173
196- git_cherry_pick($rev , $atime , $atz , $ctime , $ctz );
197-
198- delete_prev_reflog() unless $keep_prev_reflog ;
174+ $last_rev = git_rewrite_commit($rev , $atime , $atz , $ctime , $ctz , \%commit_map );
175+ }
199176
200- $keep_prev_reflog = $changed ;
177+ unless ($opt_n ) {
178+ system ' git' , ' reset' , ' --soft' , $last_rev ;
179+ exit ($? >> 8) if $? ;
201180 }
202181}
203182
@@ -226,26 +205,46 @@ sub git_revparse {
226205 $rev ;
227206}
228207
229- sub git_cherry_pick {
230- my ($rev , $atime , $atz , $ctime , $ctz ) = @_ ;
208+ sub git_rewrite_commit {
209+ my ($rev , $atime , $atz , $ctime , $ctz , $commit_map ) = @_ ;
231210
232211 return if $opt_n ;
233212
234- system " git cherry-pick $rev >/dev/null" ;
213+ my $git_log = open3(my $in1 , my $out1 , ' >&2' ,
214+ ' git' , ' log' , $rev . ' ^!' , ' --pretty=format:%P%x00%an%x00%ae%x00%cn%x00%ce%x00%B' ) or die $! ;
215+ my $log_output = do { local $/ ; <$out1 > };
216+ waitpid $git_log , 0;
235217 exit ($? >> 8) if $? ;
236218
219+ my ($parents , $author_name , $author_email , $committer_name , $committer_email , $message ) = split (/ \000 / , $log_output );
220+
221+ my @parent_revs = split (' ' , $parents );
222+ my @parent_opts ;
223+ for (@parent_revs ) {
224+ push @parent_opts , ' -p' ;
225+ if (defined (my $new_parent = $commit_map -> {$_ })) {
226+ push @parent_opts , $new_parent ;
227+ } else {
228+ push @parent_opts , $_ ;
229+ }
230+ }
231+
237232 local $ENV {GIT_COMMITTER_DATE } = " $ctime $ctz " ;
238- system ' git' , ' commit' , ' --amend' ,
239- " --date=$atime $atz " , ' -C' , $rev ;
233+ local $ENV {GIT_AUTHOR_DATE } = " $atime $atz " ;
234+ local $ENV {GIT_COMMITTER_NAME } = $committer_name ;
235+ local $ENV {GIT_AUTHOR_NAME } = $author_name ;
236+ local $ENV {GIT_COMMITTER_EMAIL } = $committer_email ;
237+ local $ENV {GIT_AUTHOR_EMAIL } = $author_email ;
238+ my $git_commit_tree = open3(my $in2 , my $out2 , ' >&2' ,
239+ ' git' , ' commit-tree' , @parent_opts , ' -m' , $message , $rev . ' ^{tree}' ) or die $! ;
240+ my $new_rev = do { local $/ ; <$out2 > };
241+ chomp $new_rev ;
242+ waitpid $git_commit_tree , 0;
240243 exit ($? >> 8) if $? ;
241244
242- delete_prev_reflog();
243- }
244-
245- sub delete_prev_reflog {
246- return if $opt_n ;
245+ $commit_map -> {$rev } = $new_rev ;
247246
248- system ' git ' , ' reflog ' , ' delete ' , ' HEAD@{1} ' ;
247+ return $new_rev ;
249248}
250249
251250sub expand_revspec {
0 commit comments