1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-07 22:02:56 +03:00

rename patch manager to patch builder

This commit is contained in:
Jesse Duffield
2023-03-19 16:09:03 +11:00
parent 7ce3165afa
commit 60f902f026
15 changed files with 83 additions and 84 deletions

View File

@@ -33,8 +33,8 @@ type (
loadFileDiffFunc func(from string, to string, reverse bool, filename string, plain bool) (string, error)
)
// 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
type PatchManager struct {
// 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 {
// To is the commit sha if we're dealing with files of a commit, or a stash ref for a stash
To string
From string
@@ -53,17 +53,16 @@ type PatchManager struct {
loadFileDiff loadFileDiffFunc
}
// NewPatchManager returns a new PatchManager
func NewPatchManager(log *logrus.Entry, applyPatch applyPatchFunc, loadFileDiff loadFileDiffFunc) *PatchManager {
return &PatchManager{
// NewPatchBuilder returns a new PatchBuilder
func NewPatchBuilder(log *logrus.Entry, applyPatch applyPatchFunc, loadFileDiff loadFileDiffFunc) *PatchBuilder {
return &PatchBuilder{
Log: log,
applyPatch: applyPatch,
loadFileDiff: loadFileDiff,
}
}
// NewPatchManager returns a new PatchManager
func (p *PatchManager) Start(from, to string, reverse bool, canRebase bool) {
func (p *PatchBuilder) Start(from, to string, reverse bool, canRebase bool) {
p.To = to
p.From = from
p.reverse = reverse
@@ -71,7 +70,7 @@ func (p *PatchManager) Start(from, to string, reverse bool, canRebase bool) {
p.fileInfoMap = map[string]*fileInfo{}
}
func (p *PatchManager) addFileWhole(info *fileInfo) {
func (p *PatchBuilder) addFileWhole(info *fileInfo) {
info.mode = WHOLE
lineCount := len(strings.Split(info.diff, "\n"))
// add every line index
@@ -82,12 +81,12 @@ func (p *PatchManager) addFileWhole(info *fileInfo) {
}
}
func (p *PatchManager) removeFile(info *fileInfo) {
func (p *PatchBuilder) removeFile(info *fileInfo) {
info.mode = UNSELECTED
info.includedLineIndices = nil
}
func (p *PatchManager) AddFileWhole(filename string) error {
func (p *PatchBuilder) AddFileWhole(filename string) error {
info, err := p.getFileInfo(filename)
if err != nil {
return err
@@ -98,7 +97,7 @@ func (p *PatchManager) AddFileWhole(filename string) error {
return nil
}
func (p *PatchManager) RemoveFile(filename string) error {
func (p *PatchBuilder) RemoveFile(filename string) error {
info, err := p.getFileInfo(filename)
if err != nil {
return err
@@ -117,7 +116,7 @@ func getIndicesForRange(first, last int) []int {
return indices
}
func (p *PatchManager) getFileInfo(filename string) (*fileInfo, error) {
func (p *PatchBuilder) getFileInfo(filename string) (*fileInfo, error) {
info, ok := p.fileInfoMap[filename]
if ok {
return info, nil
@@ -137,7 +136,7 @@ func (p *PatchManager) getFileInfo(filename string) (*fileInfo, error) {
return info, nil
}
func (p *PatchManager) AddFileLineRange(filename string, firstLineIdx, lastLineIdx int) error {
func (p *PatchBuilder) AddFileLineRange(filename string, firstLineIdx, lastLineIdx int) error {
info, err := p.getFileInfo(filename)
if err != nil {
return err
@@ -148,7 +147,7 @@ func (p *PatchManager) AddFileLineRange(filename string, firstLineIdx, lastLineI
return nil
}
func (p *PatchManager) RemoveFileLineRange(filename string, firstLineIdx, lastLineIdx int) error {
func (p *PatchBuilder) RemoveFileLineRange(filename string, firstLineIdx, lastLineIdx int) error {
info, err := p.getFileInfo(filename)
if err != nil {
return err
@@ -162,7 +161,7 @@ func (p *PatchManager) RemoveFileLineRange(filename string, firstLineIdx, lastLi
return nil
}
func (p *PatchManager) RenderPatchForFile(filename string, plain bool, reverse bool) string {
func (p *PatchBuilder) RenderPatchForFile(filename string, plain bool, reverse bool) string {
info, err := p.getFileInfo(filename)
if err != nil {
p.Log.Error(err)
@@ -195,7 +194,7 @@ func (p *PatchManager) RenderPatchForFile(filename string, plain bool, reverse b
}
}
func (p *PatchManager) renderEachFilePatch(plain bool) []string {
func (p *PatchBuilder) renderEachFilePatch(plain bool) []string {
// sort files by name then iterate through and render each patch
filenames := maps.Keys(p.fileInfoMap)
@@ -210,11 +209,11 @@ func (p *PatchManager) renderEachFilePatch(plain bool) []string {
return output
}
func (p *PatchManager) RenderAggregatedPatch(plain bool) string {
func (p *PatchBuilder) RenderAggregatedPatch(plain bool) string {
return strings.Join(p.renderEachFilePatch(plain), "")
}
func (p *PatchManager) GetFileStatus(filename string, parent string) PatchStatus {
func (p *PatchBuilder) GetFileStatus(filename string, parent string) PatchStatus {
if parent != p.To {
return UNSELECTED
}
@@ -227,7 +226,7 @@ func (p *PatchManager) GetFileStatus(filename string, parent string) PatchStatus
return info.mode
}
func (p *PatchManager) GetFileIncLineIndices(filename string) ([]int, error) {
func (p *PatchBuilder) GetFileIncLineIndices(filename string) ([]int, error) {
info, err := p.getFileInfo(filename)
if err != nil {
return nil, err
@@ -235,7 +234,7 @@ func (p *PatchManager) GetFileIncLineIndices(filename string) ([]int, error) {
return info.includedLineIndices, nil
}
func (p *PatchManager) ApplyPatches(reverse bool) error {
func (p *PatchBuilder) ApplyPatches(reverse bool) error {
patch := ""
applyFlags := []string{"index", "3way"}
@@ -255,16 +254,16 @@ func (p *PatchManager) ApplyPatches(reverse bool) error {
}
// clears the patch
func (p *PatchManager) Reset() {
func (p *PatchBuilder) Reset() {
p.To = ""
p.fileInfoMap = map[string]*fileInfo{}
}
func (p *PatchManager) Active() bool {
func (p *PatchBuilder) Active() bool {
return p.To != ""
}
func (p *PatchManager) IsEmpty() bool {
func (p *PatchBuilder) IsEmpty() bool {
for _, fileInfo := range p.fileInfoMap {
if fileInfo.mode == WHOLE || (fileInfo.mode == PART && len(fileInfo.includedLineIndices) > 0) {
return false
@@ -275,11 +274,11 @@ func (p *PatchManager) IsEmpty() bool {
}
// 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 {
func (p *PatchBuilder) NewPatchRequired(from string, to string, reverse bool) bool {
return from != p.From || to != p.To || reverse != p.reverse
}
func (p *PatchManager) AllFilesInPatch() []string {
func (p *PatchBuilder) AllFilesInPatch() []string {
files := make([]string, 0, len(p.fileInfoMap))
for filename := range p.fileInfoMap {