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

Code before:
			Name:     goGitRemote.Config().Name,
			Urls:     goGitRemote.Config().URLs,
			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)

	cmdArgs := NewGitCmd("for-each-ref").
		Arg(fmt.Sprintf("--sort=%s", self.AppState.RemoteBranchSortOrder)).
		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{}
		}

		remoteBranchesByRemoteName[remoteName] = append(remoteBranchesByRemoteName[remoteName],
			&models.RemoteBranch{
				Name:       name,
				RemoteName: remoteName,
			})
		return false, nil

Review comment (suggestion):
Using the AppState value without validating it in any way makes me nervous. If you edit your state.yml to contain `remotebranchsortorder: xyz`, you won't see any branches, which is not good.

I wonder if we should store "alphabetical" or "date" for `remotebranchsortorder` instead of the real sort key, and then have a function that maps this to the real sort key, defaulting to "refname" for unknown entries. It's more code, but would make it a bit more robust.

Code after:
			Name:     goGitRemote.Config().Name,
			Urls:     goGitRemote.Config().URLs,
			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]