Repository: jesseduffield/lazygit
File: pkg/gui/controllers/commits_files_controller.go (Go)

Code before:
	to := ref.RefName()
	from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
	_, err := self.c.RunSubprocess(self.c.Git().Diff.OpenDiffToolCmdObj(
		git_commands.DiffToolCmdOptions{
			Filepath:    node.GetPath(),
			FromCommit:  from,
			ToCommit:    to,
			Reverse:     reverse,
			IsDirectory: !node.IsFile(),
			Staged:      false,
		}))
	return err
}

func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.CommitFileNode) error {
	toggle := func() error {
		return self.c.WithWaitingStatus(self.c.Tr.UpdatingPatch, func(gocui.Task) error {
			if !self.c.Git().Patch.PatchBuilder.Active() {
				if err := self.startPatchBuilder(); err != nil {
					return err
				}
			}

			selectedNodes = normalisedSelectedCommitFileNodes(selectedNodes)

			nodesFullyInPatch, nodesPartiallyOrNotInPatch := utils.Partition(selectedNodes, func(node *filetree.CommitFileNode) bool {
				return node.SomeFile(func(file *models.CommitFile) bool {
					return self.c.Git().Patch.PatchBuilder.GetFileStatus(file.Name, self.context().GetRef().RefName()) == patch.WHOLE
				})
			})

			if len(nodesFullyInPatch) > 0 && len(nodesPartiallyOrNotInPatch) > 0 {
				for _, node := range nodesPartiallyOrNotInPatch {

					err := node.ForEachFile(func(file *models.CommitFile) error {
						return self.c.Git().Patch.PatchBuilder.AddFileWhole(file.Name)
					})
					if err != nil {
						return self.c.Error(err)
					}
				}
			}

			if len(nodesFullyInPatch) == 0 && len(nodesPartiallyOrNotInPatch) > 0 {
				for _, node := range nodesPartiallyOrNotInPatch {
					err := node.ForEachFile(func(file *models.CommitFile) error {
						return self.c.Git().Patch.PatchBuilder.AddFileWhole(file.Name)
					})
					if err != nil {
						return self.c.Error(err)
					}

Review comment (refactor):
I would simplify this by updating AddFileWhole to make it do nothing if the file is already added whole. That way, we don't need to do this partition: we simply need to check if there is any node in the selection that contains a non-added or partially-added file, and if there is, we call AddFileWhole on each file, otherwise we call RemoveFile on each file. Note that RemoveFile doesn't need to change because it's already cheap to call for a file that's already been removed.

Code after:
	})
}

func (self *CommitFilesController) open(node *filetree.CommitFileNode) error {
	return self.c.Helpers().Files.OpenFile(node.GetPath())
}

func (self *CommitFilesController) edit(node *filetree.CommitFileNode) error {
	if node.File == nil {
		return self.c.ErrorMsg(self.c.Tr.ErrCannotEditDirectory)
	}

	return self.c.Helpers().Files.EditFile(node.GetPath())
}

func (self *CommitFilesController) openDiffTool(node *filetree.CommitFileNode) error {
	ref := self.context().GetRef()
	to := ref.RefName()
	from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
	_, err := self.c.RunSubprocess(self.c.Git().Diff.OpenDiffToolCmdObj(
		git_commands.DiffToolCmdOptions{
			Filepath:    node.GetPath(),
			FromCommit:  from,
			ToCommit:    to,
			Reverse:     reverse,
			IsDirectory: !node.IsFile(),
			Staged:      false,
		}))
	return err
}

func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.CommitFileNode) error {
	toggle := func() error {
		return self.c.WithWaitingStatus(self.c.Tr.UpdatingPatch, func(gocui.Task) error {
			if !self.c.Git().Patch.PatchBuilder.Active() {
				if err := self.startPatchBuilder(); err != nil {
					return err
				}
			}

			selectedNodes = normalisedSelectedCommitFileNodes(selectedNodes)

			// Find if any file in the selection is unselected or partially added
			adding := lo.SomeBy(selectedNodes, func(node *filetree.CommitFileNode) bool {
				return node.SomeFile(func(file *models.CommitFile) bool {
					fileStatus := self.c.Git().Patch.PatchBuilder.GetFileStatus(file.Name, self.context().GetRef().RefName())
					return fileStatus == patch.PART || fileStatus == patch.UNSELECTED
				})
			})

			patchOperationFunction := self.c.Git().Patch.PatchBuilder.RemoveFile