Skip to content

worktree.Status does not handle a broken symlink correctly #253

@alexey-igrychev

Description

@alexey-igrychev

I expect the diffStagingWithWorktree function will work as git status, and a symlink pointing to a non-existent file will be handled correctly.

Check

$ ln -s /nowhere broken_symlink

$ git add broken_symlink

$ git commit -m "Add symlink"
[master a30a58d] Add symlink
 1 file changed, 1 insertion(+)
 create mode 120000 broken_symlink

$ git status
On branch master
nothing to commit, working tree clean

$ ./main
broken_symlink: [worktree: Modified, staging: Unmodified]

main.go

package main

import (
	"fmt"

	"github.com/go-git/go-git/v5"
)

var fileStatusMapping = map[git.StatusCode]string{
	git.Unmodified:         "Unmodified",
	git.Untracked:          "Untracked",
	git.Modified:           "Modified",
	git.Added:              "Added",
	git.Deleted:            "Deleted",
	git.Renamed:            "Renamed",
	git.Copied:             "Copied",
	git.UpdatedButUnmerged: "Updated",
}

func main() {
	repo, err := git.PlainOpen(".")
	if err != nil {
		panic(err)
	}

	tree, err := repo.Worktree()
	if err != nil {
		panic(err)
	}

	status, err := tree.Status()
	if err != nil {
		panic(err)
	}

	filePath := "broken_symlink"
	fileStatus := status.File(filePath)
	fmt.Printf("%s: [worktree: %s, staging: %s]\n", filePath, fileStatusMapping[fileStatus.Worktree], fileStatusMapping[fileStatus.Staging])
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleIssues/PRs that are marked for closure due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions