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

Not working on Azure Git Pipeline

Open Makio64 opened this issue 6 years ago • 4 comments

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

Makio64 avatar Apr 07 '20 14:04 Makio64

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.

doowb avatar Apr 07 '20 14:04 doowb

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.

Makio64 avatar Apr 07 '20 15:04 Makio64

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

joscha avatar Feb 19 '21 00:02 joscha

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.

slikts avatar Oct 26 '21 11:10 slikts