@@ -77,6 +77,10 @@ while ($i < $#line) {
7777 my $spit = 0;
7878 if (/ ^\s / or /^$/ ) { $orig ++; $new ++; }
7979 elsif (/ ^\+ / ) { $new ++; }
80+ elsif (/ ^-- $ / ) {
81+ # Git signature line - end of patch content
82+ $spit = 1;
83+ }
8084 elsif (/ ^-/ ) {
8185 # Look out for new file changes.
8286 if (/ ^--- / ) {
@@ -125,17 +129,33 @@ foreach (@off_lines) {
125129 next ;
126130 }
127131 $line [$_ ] =~ / ^@@ -(\d +),?(\d +)? \+ (\d +),?(\d +)? @@(.*)$ / ;
128- ($ooff ,$oc ,$nc ,$misc ) = ($1 ,$2 ,$4 ,$5 );
129- $oc = 1 unless defined ($oc );
132+ ($ooff ,$oc ,$noff_orig ,$nc ,$misc ) = ($1 ,$2 ,$3 ,$4 ,$5 );
133+
134+ # Check for new file before applying defaults
135+ $is_new_file = ($ooff == 0 && defined ($oc ) && $oc == 0);
136+
137+ # For new files, preserve oc=0; otherwise apply default
138+ if (!$is_new_file ) {
139+ $oc = 1 unless defined ($oc );
140+ }
130141 $nc = 1 unless defined ($nc );
131- $noff = $ooff + $offset ;
132- $noff ++ if ($oc == 0);
133- $noff -- if ($nc == 0);
142+
143+ # For new files (ooff=0, original oc=0), preserve the original new offset
144+ if ($is_new_file ) {
145+ $noff = $noff_orig + $offset ;
146+ } else {
147+ $noff = $ooff + $offset ;
148+ # Don't adjust offset for new files (when ooff is 0 and oc is 0)
149+ $noff ++ if ($oc == 0 && $ooff != 0);
150+ $noff -- if ($nc == 0);
151+ }
134152 $line [$_ ] = " @@ -$ooff " ;
135153 $line [$_ ] .= " ,$oc " if ($oc != 1);
136154 $line [$_ ] .= " +$noff " ;
137155 $line [$_ ] .= " ,$nc " if ($nc != 1);
138156 $line [$_ ] .= " @@" . " $misc \n " ;
157+
158+
139159 $offset += $nc - $oc ;
140160}
141161
0 commit comments