From e483ec8b0d7fc165eb7b57b3f1bcec8f46e04c13 Mon Sep 17 00:00:00 2001
From: nitul1991 <nitul1991@users.noreply.github.com>
Date: Tue, 10 Aug 2021 00:54:31 +0530
Subject: [PATCH] Change the implementation of the go-git version of GetNote to
 mirror the non go-git version when passed a non-existent commit (#16658)
 (#16659)

Backport #16658

Fixes #16657
---
 modules/git/notes_gogit.go |  3 +++
 modules/git/notes_test.go  | 12 ++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/modules/git/notes_gogit.go b/modules/git/notes_gogit.go
index 534a5d5171..702754069b 100644
--- a/modules/git/notes_gogit.go
+++ b/modules/git/notes_gogit.go
@@ -36,6 +36,9 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
 			remainingCommitID = remainingCommitID[2:]
 		}
 		if err != nil {
+			if err == object.ErrDirectoryNotFound {
+				return ErrNotExist{ID: remainingCommitID, RelPath: path}
+			}
 			return err
 		}
 	}
diff --git a/modules/git/notes_test.go b/modules/git/notes_test.go
index f66a191e6a..fec46e5960 100644
--- a/modules/git/notes_test.go
+++ b/modules/git/notes_test.go
@@ -39,3 +39,15 @@ func TestGetNestedNotes(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, []byte("Note 1"), note.Message)
 }
+
+func TestGetNonExistentNotes(t *testing.T) {
+	bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
+	bareRepo1, err := OpenRepository(bareRepo1Path)
+	assert.NoError(t, err)
+	defer bareRepo1.Close()
+
+	note := Note{}
+	err = GetNote(context.Background(), bareRepo1, "non_existent_sha", &note)
+	assert.Error(t, err)
+	assert.IsType(t, ErrNotExist{}, err)
+}