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

Wrong commit order #779

Open
ffleandro opened this issue Apr 1, 2022 · 2 comments
Open

Wrong commit order #779

ffleandro opened this issue Apr 1, 2022 · 2 comments

Comments

@ffleandro
Copy link

I'm seeing some cases where the git history shows commits in the wrong order from which they were created.

Here is my code:

    await this.git.checkout('master');
    await this.git.add('.');
    await this.git.commit('commit 1');
    const hash = await this.git.revparse('HEAD');
    
    await this.git.checkout(['-b', 'new_branch']);
    // apply some changes in a file
    await this.git.add('.');
    await this.git.commit('commit 2');
    await this.git.addTag('new_tag');
    await this.git.push(['--atomic', 'origin', 'master', 'new_branch', 'new_tag']);
    await this.git.checkout('master');

What I expected was to always branch new_branch from the commit 1, but sometimes the branch is made before the commit 1.

Result:

4282224 (HEAD -> master) commit 1
1752c05 (tag: new_tag, new_branch) commit 2
5db3065 Base commit

It seems that commit 4282224 and 1752c05 are in the wrong order.

I'm I doing something wrong or is this the expected behaviour?

@steveukx
Copy link
Owner

steveukx commented Apr 2, 2022

Hi, I wrote an integration test to cover this:

describe('log-commit-order', () => {

   it('commits and logs in chronological order', async () => {
      const context = await createTestContext();
      await setUpInit(context);

      const git = newSimpleGit(context.root);
      const hashes: string[] = [];

      await setUpFilesAdded(context, ['first'], '.',' commit 1');
      hashes.push(await git.revparse('HEAD'));

      await git.checkout(['-b', 'new_branch']);
      await setUpFilesAdded(context, ['second'], '.',' commit 2');
      hashes.push(await git.revparse('HEAD'));

      await git.addTag('new_tag');
      await git.checkout('master');

      const tagHashes = (await git.log(['new_tag']))
         .all
         .map(({hash}) => hash);

      expect(hashes).toEqual(tagHashes.reverse());
   });
})

And found that I always had master with a single commit, new_branch and new_tag have the same two commits that appear in git.log in reverse chronological order.

Is it possible that some other process is accessing this.git while your script is running?

@steveukx steveukx added the more-info-needed More information is required in order to investigate label Apr 2, 2022
@ffleandro
Copy link
Author

I'm using this code inside of a NodeJS/ExpressJS API running in dev mode, so there is the chance that there are more than one process spinned up.

Is it possible that some other process is accessing this.git while your script is running?
What would be the the explanation if this was the case?

I mean, all the commits and branching/tagging are done by a single API call, is there a way to make this work as expected?

@no-response no-response bot removed the more-info-needed More information is required in order to investigate label Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants