Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File reported as Untracked while it is commited (i.e. Unmodified) #119

Closed
pantelis-karamolegkos opened this issue Jul 4, 2020 · 6 comments · May be fixed by #1023
Closed

File reported as Untracked while it is commited (i.e. Unmodified) #119

pantelis-karamolegkos opened this issue Jul 4, 2020 · 6 comments · May be fixed by #1023
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed stale Issues/PRs that are marked for closure due to inactivity

Comments

@pantelis-karamolegkos
Copy link

Assuming my full path to repo is /Users/panteliskaramolegkos/myrepo, I am going through the following steps to check the status of a specific file (code below is part of a specific function)

	repo, err := git.PlainOpen(fullPathToRepo)
	if err != nil {
		return false, fmt.Errorf("ERROR: Unable to open repository %s\n%s", fullPathToRepo, err)
	}

	workTree, err := repo.Worktree()
	if err != nil {
		return false, fmt.Errorf("ERROR: Unable to open worktree for repository %s\n%s", fullPathToRepo, err)
	}
	workTreeStatus, err := workTree.Status()
	if err != nil {
		return false, fmt.Errorf("ERROR: Unable to retrieve worktree status for repository %s\n%s", fullPathToRepo, err)
	}
	fmt.Printf("%q\n", workTreeStatus.File("relative/path/to/file.yaml").Worktree)
	fmt.Printf("%q\n", workTreeStatus.File("/Users/panteliskaramolegkos/full/path/to/file.yaml").Worktree)

In both cases (i.e. using full or relative path, the following gets printed out

'?'
'?'

According to the documentation, this corresponds to status Untracked

However the file is committed (and pushed to the remote, but I guess this is irrelevant)

@dmoles
Copy link

dmoles commented Nov 29, 2020

FWIW, I came up with the following hack:

// cheap workaround for https://github.com/go-git/go-git/issues/119
func status(path string) (*git.FileStatus, error) {
	cmd := exec.Command("git", "status", "-s", path)
	output, err := cmd.Output()
	if err != nil {
		return nil, err
	}
	fs := git.FileStatus{
		Staging:  git.Untracked,
		Worktree: git.Untracked,
	}
	// TODO: detect merge conflicts etc.
	if len(output) >= 2 {
		fs.Staging = git.StatusCode(output[0])
		fs.Worktree = git.StatusCode(output[1])
	}
	return &fs, nil
}

@nygymankussainov
Copy link

nygymankussainov commented Apr 19, 2021

Workaround:

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

func (r *Repo) FileStatus(filename string) (string, string, error) {
        w, err := r.worktree()
        if err != nil {
                return "", "", err
        }
        s, err := w.Status()
        if err != nil {
                return "", "", err
        }
        if s != nil {
                var untracked bool
                if s.IsUntracked(filename) {
                        untracked = true
                }
                fileStatus := s.File(filename)
                if !untracked && fileStatus.Staging == git.Untracked &&
                        fileStatus.Worktree == git.Untracked {
                        fileStatus.Staging = git.Unmodified
                        fileStatus.Worktree = git.Unmodified
                }
                return fileStatusMapping[fileStatus.Staging], fileStatusMapping[fileStatus.Worktree], nil
        }
        return "", "", nil
}

NOTE: s.IsUntracked() called before s.File() on purpose

@moritzschmitz-oviva
Copy link

This has been open for a while and is a clear bug, isn't it? Having the same problem.

@alita1991
Copy link

Does anyone else have an issue in detecting changes with status? If I run git status, I get some information, while with file status, the list is empty.

Copy link

To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while.

This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed.

We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active.

Thanks for your understanding and cooperation!

@github-actions github-actions bot added the stale Issues/PRs that are marked for closure due to inactivity label Nov 28, 2023
@pjbgf pjbgf added bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed and removed stale Issues/PRs that are marked for closure due to inactivity labels Dec 15, 2023
rodrigocam added a commit to rodrigocam/go-git that referenced this issue Feb 5, 2024
Copy link

github-actions bot commented Apr 5, 2024

To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while.

This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed.

We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active.

Thanks for your understanding and cooperation!

@github-actions github-actions bot added the stale Issues/PRs that are marked for closure due to inactivity label Apr 5, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed stale Issues/PRs that are marked for closure due to inactivity
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants