Correct way to get log of a specific file
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.
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()?
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})
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.
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.