summaryrefslogtreecommitdiffstats
path: root/pkg/commands/loading_commit_files.go
blob: 2c71cc0680eeb91ce69f4c962793bffdd597985c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package commands

import (
	"strings"

	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/commands/patch"
)

// GetFilesInDiff get the specified commit files
func (c *GitCommand) GetFilesInDiff(from string, to string, reverse bool, patchManager *patch.PatchManager) ([]*models.CommitFile, error) {
	reverseFlag := ""
	if reverse {
		reverseFlag = " -R "
	}

	filenames, err := c.OSCommand.RunCommandWithOutput("git diff --submodule --no-ext-diff --name-status -z --no-renames %s %s %s", reverseFlag, from, to)
	if err != nil {
		return nil, err
	}

	return c.getCommitFilesFromFilenames(filenames, to, patchManager), nil
}

// filenames string is something like "file1\nfile2\nfile3"
func (c *GitCommand) getCommitFilesFromFilenames(filenames string, parent string, patchManager *patch.PatchManager) []*models.CommitFile {
	commitFiles := make([]*models.CommitFile, 0)

	lines := strings.Split(strings.TrimRight(filenames, "\x00"), "\x00")
	n := len(lines)
	for i := 0; i < n-1; i += 2 {
		// typical result looks like 'A my_file' meaning my_file was added
		changeStatus := lines[i]
		name := lines[i+1]
		status := patch.UNSELECTED
		if patchManager != nil && patchManager.To == parent {
			status = patchManager.GetFileStatus(name)
		}

		commitFiles = append(commitFiles, &models.CommitFile{
			Parent:       parent,
			Name:         name,
			ChangeStatus: changeStatus,
			PatchStatus:  status,
		})
	}

	return commitFiles
}