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

Wrong commit order

Open ffleandro opened this issue 2 years ago • 2 comments

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?

ffleandro avatar Apr 01 '22 15:04 ffleandro

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 avatar Apr 02 '22 06:04 steveukx

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?

ffleandro avatar Apr 04 '22 21:04 ffleandro