2020-08-15 03:18:40 +02:00
package patch
2019-11-04 10:47:25 +02:00
import (
2020-08-22 08:46:19 +02:00
"errors"
2019-11-04 10:47:25 +02:00
"sort"
2020-08-22 08:46:19 +02:00
"strings"
2019-11-04 10:47:25 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
)
2020-08-15 03:18:40 +02:00
const (
// UNSELECTED is for when the commit file has not been added to the patch in any way
UNSELECTED = iota
// WHOLE is for when you want to add the whole diff of a file to the patch,
// including e.g. if it was deleted
2020-08-21 12:31:21 +02:00
WHOLE
2020-08-15 03:18:40 +02:00
// PART is for when you're only talking about specific lines that have been modified
PART
)
2019-11-04 10:47:25 +02:00
type fileInfo struct {
mode int // one of WHOLE/PART
includedLineIndices [ ] int
diff string
}
2019-11-05 09:44:46 +02:00
type applyPatchFunc func ( patch string , flags ... string ) error
2020-08-22 10:29:09 +02:00
type loadFileDiffFunc func ( from string , to string , reverse bool , filename string , plain bool ) ( string , error )
2019-11-04 10:47:25 +02:00
2020-08-21 12:31:21 +02:00
// PatchManager manages the building of a patch for a commit to be applied to another commit (or the working tree, or removed from the current commit). We also support building patches from things like stashes, for which there is less flexibility
2019-11-04 10:47:25 +02:00
type PatchManager struct {
2020-08-22 10:29:09 +02:00
// To is the commit sha if we're dealing with files of a commit, or a stash ref for a stash
2020-08-22 23:35:25 +02:00
To string
2020-08-22 10:29:09 +02:00
From string
Reverse bool
2020-08-21 12:31:21 +02:00
// CanRebase tells us whether we're allowed to modify our commits. CanRebase should be true for commits of the currently checked out branch and false for everything else
2020-08-22 08:46:19 +02:00
CanRebase bool
// fileInfoMap starts empty but you add files to it as you go along
2019-11-04 10:47:25 +02:00
fileInfoMap map [ string ] * fileInfo
Log * logrus . Entry
ApplyPatch applyPatchFunc
2020-08-22 08:46:19 +02:00
2020-08-22 10:29:09 +02:00
// LoadFileDiff loads the diff of a file, for a given to (typically a commit SHA)
2020-08-22 08:46:19 +02:00
LoadFileDiff loadFileDiffFunc
2019-11-04 10:47:25 +02:00
}
2020-08-09 07:42:20 +02:00
// NewPatchManager returns a new PatchManager
2020-08-22 08:46:19 +02:00
func NewPatchManager ( log * logrus . Entry , applyPatch applyPatchFunc , loadFileDiff loadFileDiffFunc ) * PatchManager {
2019-11-05 09:10:47 +02:00
return & PatchManager {
2020-08-22 08:46:19 +02:00
Log : log ,
ApplyPatch : applyPatch ,
LoadFileDiff : loadFileDiff ,
2019-11-05 09:10:47 +02:00
}
}
2020-08-09 07:42:20 +02:00
// NewPatchManager returns a new PatchManager
2020-08-22 10:29:09 +02:00
func ( p * PatchManager ) Start ( from , to string , reverse bool , canRebase bool ) {
p . To = to
p . From = from
p . Reverse = reverse
2020-08-21 12:50:54 +02:00
p . CanRebase = canRebase
2019-11-05 09:10:47 +02:00
p . fileInfoMap = map [ string ] * fileInfo { }
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
func ( p * PatchManager ) addFileWhole ( info * fileInfo ) {
info . mode = WHOLE
lineCount := len ( strings . Split ( info . diff , "\n" ) )
info . includedLineIndices = make ( [ ] int , lineCount )
// add every line index
for i := 0 ; i < lineCount ; i ++ {
info . includedLineIndices [ i ] = i
}
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
func ( p * PatchManager ) removeFile ( info * fileInfo ) {
info . mode = UNSELECTED
info . includedLineIndices = nil
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
func ( p * PatchManager ) ToggleFileWhole ( filename string ) error {
info , err := p . getFileInfo ( filename )
if err != nil {
return err
}
2019-11-04 10:47:25 +02:00
switch info . mode {
2020-08-22 08:46:19 +02:00
case UNSELECTED , PART :
p . addFileWhole ( info )
2019-11-04 10:47:25 +02:00
case WHOLE :
2020-08-22 08:46:19 +02:00
p . removeFile ( info )
default :
return errors . New ( "unknown file mode" )
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
return nil
2019-11-04 10:47:25 +02:00
}
func getIndicesForRange ( first , last int ) [ ] int {
indices := [ ] int { }
for i := first ; i <= last ; i ++ {
indices = append ( indices , i )
}
return indices
}
2020-08-22 08:46:19 +02:00
func ( p * PatchManager ) getFileInfo ( filename string ) ( * fileInfo , error ) {
info , ok := p . fileInfoMap [ filename ]
if ok {
return info , nil
}
2020-08-22 10:29:09 +02:00
diff , err := p . LoadFileDiff ( p . From , p . To , p . Reverse , filename , true )
2020-08-22 08:46:19 +02:00
if err != nil {
return nil , err
}
info = & fileInfo {
mode : UNSELECTED ,
diff : diff ,
}
p . fileInfoMap [ filename ] = info
return info , nil
}
func ( p * PatchManager ) AddFileLineRange ( filename string , firstLineIdx , lastLineIdx int ) error {
info , err := p . getFileInfo ( filename )
if err != nil {
return err
}
2019-11-04 10:47:25 +02:00
info . mode = PART
info . includedLineIndices = utils . UnionInt ( info . includedLineIndices , getIndicesForRange ( firstLineIdx , lastLineIdx ) )
2020-08-22 08:46:19 +02:00
return nil
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
func ( p * PatchManager ) RemoveFileLineRange ( filename string , firstLineIdx , lastLineIdx int ) error {
info , err := p . getFileInfo ( filename )
if err != nil {
return err
}
2019-11-04 10:47:25 +02:00
info . mode = PART
info . includedLineIndices = utils . DifferenceInt ( info . includedLineIndices , getIndicesForRange ( firstLineIdx , lastLineIdx ) )
if len ( info . includedLineIndices ) == 0 {
2020-08-22 08:46:19 +02:00
p . removeFile ( info )
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
return nil
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
func ( p * PatchManager ) renderPlainPatchForFile ( filename string , reverse bool , keepOriginalHeader bool ) string {
info , err := p . getFileInfo ( filename )
if err != nil {
p . Log . Error ( err )
2019-11-04 10:47:25 +02:00
return ""
}
switch info . mode {
case WHOLE :
// use the whole diff
// the reverse flag is only for part patches so we're ignoring it here
return info . diff
case PART :
// generate a new diff with just the selected lines
2020-08-09 07:42:20 +02:00
return ModifiedPatchForLines ( p . Log , filename , info . diff , info . includedLineIndices , reverse , keepOriginalHeader )
2019-11-04 10:47:25 +02:00
default :
return ""
}
}
2019-11-05 04:01:58 +02:00
func ( p * PatchManager ) RenderPatchForFile ( filename string , plain bool , reverse bool , keepOriginalHeader bool ) string {
2020-08-22 08:46:19 +02:00
patch := p . renderPlainPatchForFile ( filename , reverse , keepOriginalHeader )
2019-11-04 10:47:25 +02:00
if plain {
return patch
}
parser , err := NewPatchParser ( p . Log , patch )
if err != nil {
// swallowing for now
return ""
}
// not passing included lines because we don't want to see them in the secondary panel
return parser . Render ( - 1 , - 1 , nil )
}
2020-08-22 08:46:19 +02:00
func ( p * PatchManager ) renderEachFilePatch ( plain bool ) [ ] string {
2019-11-04 10:47:25 +02:00
// sort files by name then iterate through and render each patch
filenames := make ( [ ] string , len ( p . fileInfoMap ) )
index := 0
for filename := range p . fileInfoMap {
filenames [ index ] = filename
index ++
}
sort . Strings ( filenames )
output := [ ] string { }
for _ , filename := range filenames {
2019-11-05 04:01:58 +02:00
patch := p . RenderPatchForFile ( filename , plain , false , true )
2019-11-04 10:47:25 +02:00
if patch != "" {
output = append ( output , patch )
}
}
return output
}
func ( p * PatchManager ) RenderAggregatedPatchColored ( plain bool ) string {
2019-11-05 09:21:09 +02:00
result := ""
2020-08-22 08:46:19 +02:00
for _ , patch := range p . renderEachFilePatch ( plain ) {
2019-11-05 09:21:09 +02:00
if patch != "" {
result += patch + "\n"
}
}
return result
2019-11-04 10:47:25 +02:00
}
2020-08-22 09:17:08 +02:00
func ( p * PatchManager ) GetFileStatus ( filename string ) int {
info , ok := p . fileInfoMap [ filename ]
if ! ok {
return UNSELECTED
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
2020-08-22 09:17:08 +02:00
return info . mode
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
func ( p * PatchManager ) GetFileIncLineIndices ( filename string ) ( [ ] int , error ) {
info , err := p . getFileInfo ( filename )
if err != nil {
return nil , err
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
return info . includedLineIndices , nil
2019-11-04 10:47:25 +02:00
}
func ( p * PatchManager ) ApplyPatches ( reverse bool ) error {
// for whole patches we'll apply the patch in reverse
// but for part patches we'll apply a reverse patch forwards
for filename , info := range p . fileInfoMap {
if info . mode == UNSELECTED {
continue
}
2019-11-05 09:44:46 +02:00
applyFlags := [ ] string { "index" , "3way" }
2019-11-04 10:47:25 +02:00
reverseOnGenerate := false
if reverse {
if info . mode == WHOLE {
2019-11-05 09:44:46 +02:00
applyFlags = append ( applyFlags , "reverse" )
2019-11-04 10:47:25 +02:00
} else {
reverseOnGenerate = true
}
}
2019-11-05 04:01:58 +02:00
var err error
// first run we try with the original header, then without
for _ , keepOriginalHeader := range [ ] bool { true , false } {
patch := p . RenderPatchForFile ( filename , true , reverseOnGenerate , keepOriginalHeader )
if patch == "" {
continue
}
2019-11-05 09:44:46 +02:00
if err = p . ApplyPatch ( patch , applyFlags ... ) ; err != nil {
2019-11-05 04:01:58 +02:00
continue
}
break
2019-11-04 10:47:25 +02:00
}
2019-11-05 04:01:58 +02:00
if err != nil {
2019-11-04 10:47:25 +02:00
return err
}
}
return nil
}
2019-11-05 09:10:47 +02:00
// clears the patch
func ( p * PatchManager ) Reset ( ) {
2020-08-22 10:29:09 +02:00
p . To = ""
2019-11-05 09:10:47 +02:00
p . fileInfoMap = map [ string ] * fileInfo { }
}
2020-08-21 12:31:21 +02:00
func ( p * PatchManager ) Active ( ) bool {
2020-08-22 10:29:09 +02:00
return p . To != ""
2019-11-10 06:24:20 +02:00
}
2019-11-05 09:10:47 +02:00
func ( p * PatchManager ) IsEmpty ( ) bool {
2019-11-10 06:24:20 +02:00
for _ , fileInfo := range p . fileInfoMap {
if fileInfo . mode == WHOLE || ( fileInfo . mode == PART && len ( fileInfo . includedLineIndices ) > 0 ) {
return false
}
}
return true
2019-11-05 09:10:47 +02:00
}
2020-08-22 10:29:09 +02:00
// if any of these things change we'll need to reset and start a new patch
func ( p * PatchManager ) NewPatchRequired ( from string , to string , reverse bool ) bool {
return from != p . From || to != p . To || reverse != p . Reverse
}