Repository: jesseduffield/lazygit
File: pkg/commands/git_commands/remote_loader.go (Go)

Code before:
			Branches: branches,
		}
	})

	// now lets sort our remotes by name alphabetically
	slices.SortFunc(remotes, func(a, b *models.Remote) bool {
		// we want origin at the top because we'll be most likely to want it
		if a.Name == "origin" {
			return true
		}
		if b.Name == "origin" {
			return false
		}
		return strings.ToLower(a.Name) < strings.ToLower(b.Name)
	})

	return remotes, nil
}

func (self *RemoteLoader) getRemoteBranchesByRemoteName() (map[string][]*models.RemoteBranch, error) {
	remoteBranchesByRemoteName := make(map[string][]*models.RemoteBranch)

	sortOrder := "refname"
	switch strings.ToLower(self.AppState.RemoteBranchSortOrder) {
	case "alphabetical":
		sortOrder = "refname"
	case "date":
		sortOrder = "-committerdate"
	}

	cmdArgs := NewGitCmd("for-each-ref").
		Arg(fmt.Sprintf("--sort=%s", sortOrder)).
		Arg("--format=%(refname:short)").
		Arg("refs/remotes").
		ToArgv()

	err := self.cmd.New(cmdArgs).DontLog().RunAndProcessLines(func(line string) (bool, error) {
		line = strings.TrimSpace(line)

		split := strings.SplitN(line, "/", 2)
		if len(split) != 2 {
			return false, nil
		}
		remoteName := split[0]
		name := split[1]

		_, ok := remoteBranchesByRemoteName[remoteName]
		if !ok {
			remoteBranchesByRemoteName[remoteName] = []*models.RemoteBranch{}
		}


Review comment (bug):
This assignment is a no-op, and this (slightly) bugs me. On the other hand, the code documents what the official enum values are, which is nice. Not sure what to do about it; I experimented with other versions of expressing this code, including this one:
```go
	var sortOrder string
	switch strings.ToLower(self.AppState.RemoteBranchSortOrder) {
	case "date":
		sortOrder = "-committerdate"
	default:
		// including the "alphabetical" case
		sortOrder = "refname"
	}
```
but I'm not sure that's better. Your call.

Code after:
			Branches: branches,
		}
	})

	// now lets sort our remotes by name alphabetically
	slices.SortFunc(remotes, func(a, b *models.Remote) bool {
		// we want origin at the top because we'll be most likely to want it
		if a.Name == "origin" {
			return true
		}
		if b.Name == "origin" {
			return false
		}
		return strings.ToLower(a.Name) < strings.ToLower(b.Name)
	})

	return remotes, nil
}

func (self *RemoteLoader) getRemoteBranchesByRemoteName() (map[string][]*models.RemoteBranch, error) {
	remoteBranchesByRemoteName := make(map[string][]*models.RemoteBranch)

	var sortOrder string
	switch strings.ToLower(self.AppState.RemoteBranchSortOrder) {
	case "alphabetical":
		sortOrder = "refname"
	case "date":
		sortOrder = "-committerdate"
	default:
		sortOrder = "refname"
	}

	cmdArgs := NewGitCmd("for-each-ref").
		Arg(fmt.Sprintf("--sort=%s", sortOrder)).
		Arg("--format=%(refname:short)").
		Arg("refs/remotes").
		ToArgv()

	err := self.cmd.New(cmdArgs).DontLog().RunAndProcessLines(func(line string) (bool, error) {
		line = strings.TrimSpace(line)

		split := strings.SplitN(line, "/", 2)
		if len(split) != 2 {
			return false, nil
		}
		remoteName := split[0]
		name := split[1]

		_, ok := remoteBranchesByRemoteName[remoteName]
		if !ok {
			remoteBranchesByRemoteName[remoteName] = []*models.RemoteBranch{}