@@ -115,15 +115,6 @@ sub may_exit {
115115sub git_shift {
116116 my ($function , @revspec ) = @_ ;
117117
118- if (` git diff --shortstat` =~ / \w / ) {
119- print STDERR " $0 : work directory is not clean.\n " ;
120- exit 1;
121- }
122- if (` git diff --shortstat --cached` =~ / \w / ) {
123- print STDERR " $0 : index is not clean.\n " ;
124- exit 1;
125- }
126-
127118 my %revs = map { ($_ => 0) } map { expand_revspec($_ ) } @revspec ;
128119
129120 if (!%revs ) {
@@ -140,19 +131,11 @@ sub git_shift {
140131 my ($rev , @rev_info ) = split (' ' , $line );
141132 if (exists ($revs {$rev })) {
142133 $revs {$rev }++;
143- $found ||= do {
144- if (!$start ) {
145- print STDERR " $0 : cannot modify the root commit.\n " ;
146- exit 1;
147- }
148- 1;
149- };
134+ $found = 1;
150135 }
151136 if ($found ) {
152137 push @all_revs , $rev ;
153138 $rev_info {$rev } = \@rev_info ;
154- } else {
155- $start = $rev
156139 }
157140 }
158141 waitpid $git , 0;
@@ -165,12 +148,7 @@ sub git_shift {
165148 }
166149 }
167150
168- unless ($opt_n ) {
169- system ' git' , ' reset' , ' --hard' , $start ;
170- exit ($? >> 8) if $? ;
171- }
172-
173- my $keep_prev_reflog = 1;
151+ my (%commit_map , $last_rev );
174152
175153 for my $rev (@all_revs ) {
176154 my ($atime , $atz , $ctime , $ctz ) = @{$rev_info {$rev }};
@@ -191,11 +169,12 @@ sub git_shift {
191169 $changed = 1;
192170 }
193171
194- git_cherry_pick($rev , $atime , $atz , $ctime , $ctz );
195-
196- delete_prev_reflog() unless $keep_prev_reflog ;
172+ $last_rev = git_rewrite_commit($rev , $atime , $atz , $ctime , $ctz , \%commit_map );
173+ }
197174
198- $keep_prev_reflog = $changed ;
175+ unless ($opt_n ) {
176+ system ' git' , ' reset' , ' --soft' , $last_rev ;
177+ exit ($? >> 8) if $? ;
199178 }
200179}
201180
@@ -224,26 +203,46 @@ sub git_revparse {
224203 $rev ;
225204}
226205
227- sub git_cherry_pick {
228- my ($rev , $atime , $atz , $ctime , $ctz ) = @_ ;
206+ sub git_rewrite_commit {
207+ my ($rev , $atime , $atz , $ctime , $ctz , $commit_map ) = @_ ;
229208
230209 return if $opt_n ;
231210
232- system " git cherry-pick $rev >/dev/null" ;
211+ my $git_log = open3(my $in1 , my $out1 , ' >&2' ,
212+ ' git' , ' log' , $rev . ' ^!' , ' --pretty=format:%P%x00%an%x00%ae%x00%cn%x00%ce%x00%B' ) or die $! ;
213+ my $log_output = do { local $/ ; <$out1 > };
214+ waitpid $git_log , 0;
233215 exit ($? >> 8) if $? ;
234216
217+ my ($parents , $author_name , $author_email , $committer_name , $committer_email , $message ) = split (/ \000 / , $log_output );
218+
219+ my @parent_revs = split (' ' , $parents );
220+ my @parent_opts ;
221+ for (@parent_revs ) {
222+ push @parent_opts , ' -p' ;
223+ if (defined (my $new_parent = $commit_map -> {$_ })) {
224+ push @parent_opts , $new_parent ;
225+ } else {
226+ push @parent_opts , $_ ;
227+ }
228+ }
229+
235230 local $ENV {GIT_COMMITTER_DATE } = " $ctime $ctz " ;
236- system ' git' , ' commit' , ' --amend' ,
237- " --date=$atime $atz " , ' -C' , $rev ;
231+ local $ENV {GIT_AUTHOR_DATE } = " $atime $atz " ;
232+ local $ENV {GIT_COMMITTER_NAME } = $committer_name ;
233+ local $ENV {GIT_AUTHOR_NAME } = $author_name ;
234+ local $ENV {GIT_COMMITTER_EMAIL } = $committer_email ;
235+ local $ENV {GIT_AUTHOR_EMAIL } = $author_email ;
236+ my $git_commit_tree = open3(my $in2 , my $out2 , ' >&2' ,
237+ ' git' , ' commit-tree' , @parent_opts , ' -m' , $message , $rev . ' ^{tree}' ) or die $! ;
238+ my $new_rev = do { local $/ ; <$out2 > };
239+ chomp $new_rev ;
240+ waitpid $git_commit_tree , 0;
238241 exit ($? >> 8) if $? ;
239242
240- delete_prev_reflog();
241- }
242-
243- sub delete_prev_reflog {
244- return if $opt_n ;
243+ $commit_map -> {$rev } = $new_rev ;
245244
246- system ' git ' , ' reflog ' , ' delete ' , ' HEAD@{1} ' ;
245+ return $new_rev ;
247246}
248247
249248sub expand_revspec {
0 commit comments