git-js icon indicating copy to clipboard operation
git-js copied to clipboard

Correct way to get log of a specific file

Open escoand opened this issue 3 years ago • 2 comments

I'm not able to request the log of a specific file, like this:

 simpleGit().log({file: "path/to/my/file"})

It fails with this error:

GitError: fatal: ambiguous argument 'path/to/my/file': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

But I found no way to circumvent this problem.

escoand avatar Oct 07 '22 09:10 escoand

Ok, the reason seems to be I'm within the .git directory or in a bare repository. Is there a way to accomplish this for this cases as well without using .raw()?

escoand avatar Oct 07 '22 09:10 escoand

Once again, found a way to do so:

simpleGit().log({"--":null, "path/to/my/file":null})

But is there a reason not to add -- before .file in any case?

Edit: this is still not superb because filenames with only digits are not appended after the -- but before. My only stable solution (at least for bare repos or the .git directory as they don't use cwd) is to use this:

simpleGit().log({"--":null, "./path/to/my/file":null})

escoand avatar Oct 07 '22 09:10 escoand

This is still a problem:

export async function getCommitsFileFullInfo(filePath:string):Promise<LogResult>{
    const parentPath = path.dirname(filePath)
    return await git.cwd({
        path: parentPath,
    }).log({
        file: filePath,
    })
}

The return value was all commits in that folder, disregarding the provided options.file

Adding "--":null solved it.

carafelix avatar Sep 21 '23 19:09 carafelix

Thanks for bringing this one up again - simple-git now has a pathspec utility that does exactly this.

From the next version you will no longer need to include '--': null in the options object, a pathspec splitter will automatically be added for you.

steveukx avatar Sep 23 '23 12:09 steveukx