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

Code before:
			return nil
		},
		Key: 'p',
	}
	copyFileDiffItem := &types.MenuItem{
		Label:   self.c.Tr.CopySelectedDiff,
		Tooltip: self.c.Tr.CopyFileDiffTooltip,
		OnPress: func() error {
			path, hasStaged := self.context().GetSelectedPath(), self.hasPathStagedChanges(node)
			diff, err := self.c.Git().Diff.GetPathDiff(path, hasStaged)
			if err != nil {
				return self.c.Error(err)
			}
			if err := self.c.OS().CopyToClipboard(diff); err != nil {
				return self.c.Error(err)
			}
			self.c.Toast(self.c.Tr.FileDiffCopiedToast)
			return nil
		},
		Key: 's',
	}
	copyAllDiff := &types.MenuItem{
		Label:   self.c.Tr.CopyAllFilesDiff,
		Tooltip: self.c.Tr.CopyFileDiffTooltip,
		OnPress: func() error {
			diff, err := self.c.Git().Diff.GetAllDiff(self.c.Helpers().WorkingTree.AnyStagedFiles())
			if err != nil {
				return self.c.Error(err)
			}
			if err := self.c.OS().CopyToClipboard(diff); err != nil {
				return self.c.Error(err)
			}
			self.c.Toast(self.c.Tr.AllFilesDiffCopiedToast)
			return nil
		},
		Key: 'a',
	}

	if node == nil {
		copyNameItem.DisabledReason = self.c.Tr.FileCopyNoItemSelectedError
		copyPathItem.DisabledReason = self.c.Tr.FileCopyNoItemSelectedError
		copyFileDiffItem.DisabledReason = self.c.Tr.FileCopyNoItemSelectedError
	}
	if node != nil && !node.GetHasStagedOrTrackedChanges() {
		copyFileDiffItem.DisabledReason = self.c.Tr.NoContentToCopyError
	}
	if !self.anyStagedOrTrackedFile() {
		copyAllDiff.DisabledReason = self.c.Tr.NoContentToCopyError
	}

	return self.c.Menu(types.CreateMenuOptions{

Review comment (refactor):
Nitpick: I'd extract a `hasStaged := self.c.Helpers().WorkingTree.AnyStagedFiles()` variable, it makes this block more analogous to the one before.

Code after:
			return nil
		},
		Key: 'p',
	}
	copyFileDiffItem := &types.MenuItem{
		Label:   self.c.Tr.CopySelectedDiff,
		Tooltip: self.c.Tr.CopyFileDiffTooltip,
		OnPress: func() error {
			path := self.context().GetSelectedPath()
			hasStaged := self.hasPathStagedChanges(node)
			diff, err := self.c.Git().Diff.GetPathDiff(path, hasStaged)
			if err != nil {
				return self.c.Error(err)
			}
			if err := self.c.OS().CopyToClipboard(diff); err != nil {
				return self.c.Error(err)
			}
			self.c.Toast(self.c.Tr.FileDiffCopiedToast)
			return nil
		},
		Key: 's',
	}
	copyAllDiff := &types.MenuItem{
		Label:   self.c.Tr.CopyAllFilesDiff,
		Tooltip: self.c.Tr.CopyFileDiffTooltip,
		OnPress: func() error {
			hasStaged := self.c.Helpers().WorkingTree.AnyStagedFiles()
			diff, err := self.c.Git().Diff.GetAllDiff(hasStaged)
			if err != nil {
				return self.c.Error(err)
			}
			if err := self.c.OS().CopyToClipboard(diff); err != nil {
				return self.c.Error(err)
			}
			self.c.Toast(self.c.Tr.AllFilesDiffCopiedToast)
			return nil
		},
		Key: 'a',
	}

	if node == nil {
		copyNameItem.DisabledReason = self.c.Tr.NoContentToCopyError
		copyPathItem.DisabledReason = self.c.Tr.NoContentToCopyError
		copyFileDiffItem.DisabledReason = self.c.Tr.NoContentToCopyError
	}
	if node != nil && !node.GetHasStagedOrTrackedChanges() {
		copyFileDiffItem.DisabledReason = self.c.Tr.NoContentToCopyError
	}
	if !self.anyStagedOrTrackedFile() {
		copyAllDiff.DisabledReason = self.c.Tr.NoContentToCopyError
	}