2020-08-15 03:18:40 +02:00
package patch
2019-11-04 10:47:25 +02:00
import (
"sort"
2020-08-22 08:46:19 +02:00
"strings"
2019-11-04 10:47:25 +02:00
2022-03-19 07:34:46 +02:00
"github.com/jesseduffield/generics/maps"
"github.com/jesseduffield/generics/slices"
2022-03-19 03:26:30 +02:00
"github.com/samber/lo"
2019-11-04 10:47:25 +02:00
"github.com/sirupsen/logrus"
)
2021-03-31 13:39:55 +02:00
type PatchStatus int
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
2021-03-31 13:39:55 +02:00
UNSELECTED PatchStatus = iota
2020-08-15 03:18:40 +02:00
// 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 {
2021-03-31 13:39:55 +02:00
mode PatchStatus
2019-11-04 10:47:25 +02:00
includedLineIndices [ ] int
diff string
}
2022-03-19 00:38:49 +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
2023-03-19 07:09:03 +02:00
// PatchBuilder 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
type PatchBuilder 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
2022-01-05 02:59:04 +02:00
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-23 02:13:56 +02:00
// TODO: move this out into a proper mode struct in the gui package: it doesn't really belong here
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
2020-08-22 08:46:19 +02:00
2022-01-05 02:59:04 +02:00
// loadFileDiff loads the diff of a file, for a given to (typically a commit SHA)
loadFileDiff loadFileDiffFunc
2019-11-04 10:47:25 +02:00
}
2023-05-19 12:18:02 +02:00
func NewPatchBuilder ( log * logrus . Entry , loadFileDiff loadFileDiffFunc ) * PatchBuilder {
2023-03-19 07:09:03 +02:00
return & PatchBuilder {
2020-08-22 08:46:19 +02:00
Log : log ,
2022-01-05 02:59:04 +02:00
loadFileDiff : loadFileDiff ,
2019-11-05 09:10:47 +02:00
}
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) Start ( from , to string , reverse bool , canRebase bool ) {
2020-08-22 10:29:09 +02:00
p . To = to
p . From = from
2022-01-05 02:59:04 +02:00
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
}
2023-05-19 12:18:02 +02:00
func ( p * PatchBuilder ) PatchToApply ( reverse bool ) string {
patch := ""
for filename , info := range p . fileInfoMap {
if info . mode == UNSELECTED {
continue
}
patch += p . RenderPatchForFile ( filename , true , reverse )
}
return patch
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) addFileWhole ( info * fileInfo ) {
2020-08-22 08:46:19 +02:00
info . mode = WHOLE
lineCount := len ( strings . Split ( info . diff , "\n" ) )
// add every line index
2022-03-19 07:34:46 +02:00
// TODO: add tests and then use lo.Range to simplify
info . includedLineIndices = make ( [ ] int , lineCount )
2020-08-22 08:46:19 +02:00
for i := 0 ; i < lineCount ; i ++ {
info . includedLineIndices [ i ] = i
}
2019-11-04 10:47:25 +02:00
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) removeFile ( info * fileInfo ) {
2020-08-22 08:46:19 +02:00
info . mode = UNSELECTED
info . includedLineIndices = nil
2019-11-04 10:47:25 +02:00
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) AddFileWhole ( filename string ) error {
2020-08-22 08:46:19 +02:00
info , err := p . getFileInfo ( filename )
if err != nil {
return err
}
2021-03-31 13:39:55 +02:00
p . addFileWhole ( info )
return nil
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) RemoveFile ( filename string ) error {
2021-03-31 13:39:55 +02:00
info , err := p . getFileInfo ( filename )
if err != nil {
return err
2019-11-04 10:47:25 +02:00
}
2020-08-22 08:46:19 +02:00
2021-03-31 13:39:55 +02:00
p . removeFile ( info )
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
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) getFileInfo ( filename string ) ( * fileInfo , error ) {
2020-08-22 08:46:19 +02:00
info , ok := p . fileInfoMap [ filename ]
if ok {
return info , nil
}
2022-01-05 02:59:04 +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
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) AddFileLineRange ( filename string , firstLineIdx , lastLineIdx int ) error {
2020-08-22 08:46:19 +02:00
info , err := p . getFileInfo ( filename )
if err != nil {
return err
}
2019-11-04 10:47:25 +02:00
info . mode = PART
2022-03-19 03:26:30 +02:00
info . includedLineIndices = lo . Union ( info . includedLineIndices , getIndicesForRange ( firstLineIdx , lastLineIdx ) )
2020-08-22 08:46:19 +02:00
return nil
2019-11-04 10:47:25 +02:00
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) RemoveFileLineRange ( filename string , firstLineIdx , lastLineIdx int ) error {
2020-08-22 08:46:19 +02:00
info , err := p . getFileInfo ( filename )
if err != nil {
return err
}
2019-11-04 10:47:25 +02:00
info . mode = PART
2022-03-19 03:26:30 +02:00
info . includedLineIndices , _ = lo . Difference ( info . includedLineIndices , getIndicesForRange ( firstLineIdx , lastLineIdx ) )
2019-11-04 10:47:25 +02:00
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
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) RenderPatchForFile ( filename string , plain bool , reverse bool ) string {
2020-08-22 08:46:19 +02:00
info , err := p . getFileInfo ( filename )
if err != nil {
p . Log . Error ( err )
2019-11-04 10:47:25 +02:00
return ""
}
2023-03-08 07:55:44 +02:00
if info . mode == UNSELECTED {
2019-11-04 10:47:25 +02:00
return ""
}
2023-03-08 07:55:44 +02:00
if info . mode == WHOLE && plain {
// Use the whole diff (spares us parsing it and then formatting it).
// TODO: see if this is actually noticeably faster.
// The reverse flag is only for part patches so we're ignoring it here.
return info . diff
2019-11-04 10:47:25 +02:00
}
2021-04-18 08:30:34 +02:00
2023-03-08 07:55:44 +02:00
patch := Parse ( info . diff ) .
Transform ( TransformOpts {
Reverse : reverse ,
IncludedLineIndices : info . includedLineIndices ,
} )
if plain {
return patch . FormatPlain ( )
} else {
return patch . FormatView ( FormatViewOpts {
IsFocused : false ,
} )
}
2019-11-04 10:47:25 +02:00
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) renderEachFilePatch ( plain bool ) [ ] string {
2019-11-04 10:47:25 +02:00
// sort files by name then iterate through and render each patch
2022-03-19 07:34:46 +02:00
filenames := maps . Keys ( p . fileInfoMap )
2019-11-04 10:47:25 +02:00
sort . Strings ( filenames )
2022-03-19 07:34:46 +02:00
patches := slices . Map ( filenames , func ( filename string ) string {
2023-02-23 19:50:53 +02:00
return p . RenderPatchForFile ( filename , plain , false )
2022-03-19 07:34:46 +02:00
} )
output := slices . Filter ( patches , func ( patch string ) bool {
return patch != ""
} )
2019-11-04 10:47:25 +02:00
return output
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) RenderAggregatedPatch ( plain bool ) string {
2023-03-08 07:55:44 +02:00
return strings . Join ( p . renderEachFilePatch ( plain ) , "" )
2019-11-04 10:47:25 +02:00
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) GetFileStatus ( filename string , parent string ) PatchStatus {
2021-03-31 13:46:42 +02:00
if parent != p . To {
return UNSELECTED
}
2020-08-22 09:17:08 +02:00
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
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) GetFileIncLineIndices ( filename string ) ( [ ] int , error ) {
2020-08-22 08:46:19 +02:00
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
}
2019-11-05 09:10:47 +02:00
// clears the patch
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) Reset ( ) {
2020-08-22 10:29:09 +02:00
p . To = ""
2019-11-05 09:10:47 +02:00
p . fileInfoMap = map [ string ] * fileInfo { }
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) Active ( ) bool {
2020-08-22 10:29:09 +02:00
return p . To != ""
2019-11-10 06:24:20 +02:00
}
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) 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
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) NewPatchRequired ( from string , to string , reverse bool ) bool {
2022-01-05 02:59:04 +02:00
return from != p . From || to != p . To || reverse != p . reverse
2020-08-22 10:29:09 +02:00
}
2023-03-18 09:17:47 +02:00
2023-03-19 07:09:03 +02:00
func ( p * PatchBuilder ) AllFilesInPatch ( ) [ ] string {
2023-03-18 09:17:47 +02:00
files := make ( [ ] string , 0 , len ( p . fileInfoMap ) )
for filename := range p . fileInfoMap {
files = append ( files , filename )
}
return files
}