jonschlinkert / git-branch

Get the current branch for a local git repository
https://github.com/jonschlinkert
MIT License
91 stars 16 forks source link

Not working on Azure Git Pipeline #13

Open Makio64 opened 4 years ago

Makio64 commented 4 years ago

Hi,

git-branch return null when executed from a Azure Git Pipeline, I dont think it come from the git as there is no problem with another utility : git-last-commit

doowb commented 4 years ago

This package looks for the .git directory in relation to the current working directory. There is an option to pass in the current working directory to specify where to start.

It also looks like Azure Pipelines allows configuring checkout paths, which might affect the current working directory.

I'm not familiar with how Azure Pipelines runs your code, so I don't know which directory it will start in, without more information.

Makio64 commented 4 years ago

I switch to another utility current-git-branch which work locally and on Azure, it indicate me Azure create a detached HEAD for the pipeline, I think detached HEAD are the problem here.

joscha commented 3 years ago

current-git-branch relies on git and a few unix built-ins (grep) to be there, whilst this package has no dependencies of this sort. Detached state means there is explicitly no branch name, which is why this library returns null. Looking at https://github.com/JPeer264/node-current-git-branch/blob/d6914733978f4ccb0490e5601093b1f154aa37c5/index.js#L23 I believe the code run is:

git branch | grep '\*'

which actually means that current-git-branch does not return a branch either but:

* (HEAD detached at c9db328)

which is not null, but arguably more incorrect. I think this ticket should probably be closed, we can potentially add a test case for explicitly not supporting detached HEAD state. After merging #14 we can easily do so by adding:

diff --git a/test/test.js b/test/test.js
index bf04550..6996af4 100644
--- a/test/test.js
+++ b/test/test.js
@@ -38,4 +38,8 @@ describe('git-branch', function() {
     });
   });
   it('should work with a git worktree', () => assert.strictEqual(branch.sync(worktreeFixtures), 'some-branch'));
+  it('should work with a detached HEAD', async () => {
+    await exec(['git', 'checkout', '--detach'].join(' '), { cwd: fixtures });
+    assert.strictEqual(branch.sync(fixtures), null)
+  });
 });

cc @jonschlinkert

slikts commented 2 years ago

Azure Pipelines check out detached commits, not branches, because the branch can change between pipeline runs. You can get the actual branch name with process.env.SYSTEM_PULLREQUEST_SOURCEBRANCH for pipelines triggered by PRs and with process.env.BUILD_SOURCEBRANCH for other triggers.