dotnet-affected icon indicating copy to clipboard operation
dotnet-affected copied to clipboard

Error: Couldn't find Git Commit or Branch with name

Open CHarnel opened this issue 1 year ago • 3 comments

Hi, I'm running a Github workflow and getting the following error: Unhandled exception: System.InvalidOperationException: Couldn't find Git Commit or Branch with name develop in repository /home/runner/work/[...]/[...]/.git/

The command that runs is: dotnet affected -f text traversal --from feature/nuget-publish-workflow --to develop

When I run this on my local working dir I'm not getting the error, but if I fetch the repo with the same command the GH action does, then I can reproduce the error locally (I'm fetching with fetch-depth: 0).

Full stack trace:

/usr/share/dotnet/dotnet affected -f text traversal --from feature/nuget-publish-workflow --to develop
Unhandled exception: System.InvalidOperationException: Couldn't find Git Commit or Branch with name develop in repository /home/runner/work/[...]/[...]/.git/
   at Affected.Cli.GitChangesProvider.GetCommitOrThrow(Repository repo, String name) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line 137
   at Affected.Cli.GitChangesProvider.GetCommitOrHead(Repository repository, String name) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line [12](https://github.com/[...]/[...]/actions/runs/3215661710/jobs/5256915063#step:5:13)0
   at Affected.Cli.GitChangesProvider.ParseRevisionRanges(Repository repository, String from, String to) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line 67
   at Affected.Cli.GitChangesProvider.GetChangesForRange[T](Repository repository, String from, String to) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line 86
   at Affected.Cli.GitChangesProvider.GetChangedFiles(String directory, String from, String to) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line 15
   at Affected.Cli.Commands.CommandExecutionContext.DetermineChangedFiles() in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Commands/Context/CommandExecutionContext.cs:line 56
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Affected.Cli.Commands.CommandExecutionContext.DetermineChangedProjects() in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Commands/Context/CommandExecutionContext.cs:line 66
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Affected.Cli.Commands.CommandExecutionContext.get_ChangedProjects() in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Commands/Context/CommandExecutionContext.cs:line 47
   at Affected.Cli.Commands.AffectedRootCommand.AffectedCommandHandler.InvokeAsync(InvocationContext ic) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Commands/AffectedRootCommand.cs:line 69
   at System.CommandLine.NamingConventionBinder.CommandHandler.GetExitCodeAsync(Object returnValue, InvocationContext context)
   at System.CommandLine.NamingConventionBinder.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass18_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Affected.Cli.ErrorHandlingMiddleware.<>c__DisplayClass0_0.<<UseRenderingErrorHandler>b__0>d.MoveNext() in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/CommandLine/ErrorHandlingMiddleware.cs:line 26
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<AddMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<AddMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<AddMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass13_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass20_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__19_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass17_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__6_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass9_0.<<UseExceptionHandler>b__0>d.MoveNext()
Error: dotnet affected failed!
Error: Unhandled exception: System.InvalidOperationException: Couldn't find Git Commit or Branch with name develop in repository /home/runner/work/[...]/[...]/.git/
   at Affected.Cli.GitChangesProvider.GetCommitOrThrow(Repository repo, String name) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line [13](https://github.com/[...]/[...]/actions/runs/3215661710/jobs/5256915063#step:5:14)7
   at Affected.Cli.GitChangesProvider.GetCommitOrHead(Repository repository, String name) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line 120
   at Affected.Cli.GitChangesProvider.ParseRevisionRanges(Repository repository, String from, String to) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line 67
   at Affected.Cli.GitChangesProvider.GetChangesForRange[T](Repository repository, String from, String to) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line 86
   at Affected.Cli.GitChangesProvider.GetChangedFiles(String directory, String from, String to) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Infrastructure/GitChangesProvider.cs:line [15](https://github.com/[...]/[...]/actions/runs/3215661710/jobs/5256915063#step:5:16)
   at Affected.Cli.Commands.CommandExecutionContext.DetermineChangedFiles() in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Commands/Context/CommandExecutionContext.cs:line 56
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Affected.Cli.Commands.CommandExecutionContext.DetermineChangedProjects() in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Commands/Context/CommandExecutionContext.cs:line 66
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Affected.Cli.Commands.CommandExecutionContext.get_ChangedProjects() in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Commands/Context/CommandExecutionContext.cs:line 47
   at Affected.Cli.Commands.AffectedRootCommand.AffectedCommandHandler.InvokeAsync(InvocationContext ic) in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/Commands/AffectedRootCommand.cs:line 69
   at System.CommandLine.NamingConventionBinder.CommandHandler.GetExitCodeAsync(Object returnValue, InvocationContext context)
   at System.CommandLine.NamingConventionBinder.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass18_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Affected.Cli.ErrorHandlingMiddleware.<>c__DisplayClass0_0.<<UseRenderingErrorHandler>b__0>d.MoveNext() in /home/runner/work/dotnet-affected/dotnet-affected/src/dotnet-affected/CommandLine/ErrorHandlingMiddleware.cs:line 26
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass[16](https://github.com/[...]/[...]/actions/runs/3215661710/jobs/5256915063#step:5:17)_0.<<AddMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<AddMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<AddMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass13_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass20_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__19_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass[17](https://github.com/[...]/[...]/actions/runs/3215661710/jobs/5256915063#step:5:18)_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__6_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass9_0.<<UseExceptionHandler>b__0>d.MoveNext()

CHarnel avatar Oct 09 '22 22:10 CHarnel

Hi @CHarnel. For Github Actions we have our own action to simplify stuff: https://github.com/leonardochaia/dotnet-affected-action#for-building-branches

However, I am not sure why develop is not being found, perhaps its origin/develop or similar? We usually use commit sha's in CI instead of branch names. There's an example in our our workflow for release here:

https://github.com/leonardochaia/dotnet-affected/blob/002cc490659b9cfeb39c01412ea45e34be1b15ff/.github/workflows/release.yml#L49-L58

PD: Are you running 2.2.0?

leonardochaia avatar Oct 09 '22 22:10 leonardochaia

Yes, I forgot to mention that I used your action for it (well I forked it 😄 )

Here's a piece of the workflow:

on: pull_request

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Nuget disable source
      run: |
        dotnet nuget list source
        dotnet nuget disable source github
    - uses: Octoplay/[email protected]
      id: dotnet_affected
      with:
        from: ${{ github.head_ref }}
        to: ${{ github.base_ref }}
...

It had an issue installing dotnet affected (Nuget didn't work) so I'm disabling and re-enabling the Nuget source. Here's the tree of .git dir in the GH runner:

.git
├── FETCH_HEAD
├── HEAD
├── branches
├── config
├── description
├── hooks
...
├── index
├── info
│   └── exclude
├── logs
│   ├── HEAD
│   └── refs
│       └── remotes
│           ├── origin
│           │   ├── develop
│           │   ├── feature
│           │   │   └── nuget-publish-workflow
│           │   └── master
│           └── pull
│               └── 2
│                   └── merge
├── objects
│   ├── info
│   └── pack
...
└── refs
    ├── heads
    ├── remotes
    │   ├── origin
    │   │   ├── develop
    │   │   ├── feature
    │   │   │   └── nuget-publish-workflow
    │   │   └── master
    │   └── pull
    │       └── 2
    │           └── merge
    └── tags

I used an example for PRs, that's why it's different... should I try the one on push you mentioned?

Regarding version - yes, it's 2.2.0

/usr/share/dotnet/dotnet tool install -g dotnet-affected
You can invoke the tool using the following command: dotnet-affected
Tool 'dotnet-affected' (version '2.2.0') was successfully installed.

CHarnel avatar Oct 10 '22 17:10 CHarnel

Hi @CHarnel Still not sure why it wouldn't work.

I've had these issues before when using develop instead of origin/develop, but I'm not sure it is the case here. Repository path has been an issue before, but in the case of the action it is set to the github path by code so it should work. And fetch-depth as well, but again, looks good here. I'm a bit baffled.

should I try the one on push you mentioned?

Yeah, perhaps give it a go with a workflow copied from our samples? Just for a sanity check.

Thanks and keep me posted. I will try to make a PR workflow run during the weekend.

PD: BTW, what kind of changes did you need to do on the github action? Feel free to throw a PR to that repo for review :+1:

Regards, Leo.

leonardochaia avatar Oct 11 '22 11:10 leonardochaia

Hi @CHarnel , have you managed to fix this issue?

Regards, Leo.

leonardochaia avatar Jan 02 '23 13:01 leonardochaia

Hi @leonardochaia , no actually I went with another approach, less comprehensive but good enough for our case. I'm using tj-actions/changed-files to detect changes from git, and am triggering builds when a project or Common changed. Since we have a simple solution hierarchy right now, only independent projects and just one Common project, this works fine.

Also in the end I didn't need to make any changes to the actions repo 🙂

CHarnel avatar Jan 03 '23 09:01 CHarnel

Understood. I'll close this one in the meantime :+1:

leonardochaia avatar Jan 03 '23 11:01 leonardochaia