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

Unexpected behavior of simpleGit.log when trying to get commits before a tag #956

Closed
maxpatiiuk opened this issue Nov 15, 2023 · 3 comments
Labels

Comments

@maxpatiiuk
Copy link

maxpatiiuk commented Nov 15, 2023

I expected this to return list of commits between the first commit in the repository and the v1.0.0 tag:

await simpleGit.log({
  from: undefined,
  to: 'v1.0.0',
  symmetric: false,
});

In reality, it does not return any results.

That is because the above is translated into a git command like this:

git log ..v1.0.0

(which might be interpreted by git as Show me the commits that are in v1.0.0 but not in the current HEAD - meaningless most of the time)

to get the expected results (Show me all the commits from the beginning of the repository's history up to the v1.0.0 tag.), the command should look like this:

git log v1.0.0

Bug occurs on this line - https://github.com/steveukx/git-js/blob/main/simple-git/src/lib/tasks/log.ts#L126

Workaround

Until this is fixed, I use the following workaround to get expected results:

await simpleGit.log({
  'v1.0.0': null
});

which looks harmlessly simple in this tiny example, but is quite ugly in my actual code:

await git.log({
  ...(isFirst
    ? tag === undefined
      ? {}
      : { [tag]: null }
    : {
        from: tags[index - 1],
        to: isLast ? undefined : tag,
        symmetric: false,
      }),
});

in comparison, if this bug is fixed, the above would just look like this:

await git.log({
  from: isFirst ? undefined : tags[index - 1],
  to: isLast ? undefined : tag,
  symmetric: false,
});

Version

Simple Git version: 3.20.0
Git version: 2.40.1

@steveukx
Copy link
Owner

Hi, thanks for opening the detailed issue.

The from and to fields aren't intended to work as you had expected, which is why the confusion here. The library is designed to use the from and to fields along with symmetric to create the either double or triple dot separated version range.

To explicitly set the bounds of the log, use rev-list to first find the commit hash of the initial commit, then use default values in the call to git.log

const git = simpleGit({ trimmed: true });
const initialCommit = await git.raw('rev-list', '--max-parents=0', 'HEAD');

await git.log({
  from: from ?? initialCommit,
  to: to ?? 'HEAD',
  symmetric: false,
});

@steveukx steveukx added question more-info-needed More information is required in order to investigate labels Nov 19, 2023
@maxpatiiuk
Copy link
Author

maxpatiiuk commented Nov 20, 2023

Thank you for the suggestion. This works great!

The library is designed to use the from and to fields along with symmetric

(optional) Consider adding a warning when not all attributes are provided (or using TypeScript typing to enforce correct usage)

@github-actions github-actions bot removed the more-info-needed More information is required in order to investigate label Nov 20, 2023
maxpatiiuk added a commit to maxpatiiuk/text-hoarder that referenced this issue Nov 20, 2023
@steveukx
Copy link
Owner

As of version 3.21.0 (on npm now) you can get the initial commit hash with await git.firstCommit() which will further simplify your code.

maxpatiiuk added a commit to maxpatiiuk/text-hoarder that referenced this issue Dec 3, 2023
maxpatiiuk added a commit to maxpatiiuk/text-hoarder that referenced this issue Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants