github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/commit_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package git 7 8 import ( 9 "path/filepath" 10 "strings" 11 "testing" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestCommitsCount(t *testing.T) { 17 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 18 19 commitsCount, err := CommitsCount(DefaultContext, bareRepo1Path, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0") 20 assert.NoError(t, err) 21 assert.Equal(t, int64(3), commitsCount) 22 } 23 24 func TestGetFullCommitID(t *testing.T) { 25 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 26 27 id, err := GetFullCommitID(DefaultContext, bareRepo1Path, "8006ff9a") 28 assert.NoError(t, err) 29 assert.Equal(t, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", id) 30 } 31 32 func TestGetFullCommitIDError(t *testing.T) { 33 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 34 35 id, err := GetFullCommitID(DefaultContext, bareRepo1Path, "unknown") 36 assert.Empty(t, id) 37 if assert.Error(t, err) { 38 assert.EqualError(t, err, "object does not exist [id: unknown, rel_path: ]") 39 } 40 } 41 42 func TestCommitFromReader(t *testing.T) { 43 commitString := `feaf4ba6bc635fec442f46ddd4512416ec43c2c2 commit 1074 44 tree f1a6cb52b2d16773290cefe49ad0684b50a4f930 45 parent 37991dec2c8e592043f47155ce4808d4580f9123 46 author silverwind <me@silverwind.io> 1563741793 +0200 47 committer silverwind <me@silverwind.io> 1563741793 +0200 48 gpgsig -----BEGIN PGP SIGNATURE----- 49 ` + " " + ` 50 iQIzBAABCAAdFiEEWPb2jX6FS2mqyJRQLmK0HJOGlEMFAl00zmEACgkQLmK0HJOG 51 lEMDFBAAhQKKqLD1VICygJMEB8t1gBmNLgvziOLfpX4KPWdPtBk3v/QJ7OrfMrVK 52 xlC4ZZyx6yMm1Q7GzmuWykmZQJ9HMaHJ49KAbh5MMjjV/+OoQw9coIdo8nagRUld 53 vX8QHzNZ6Agx77xHuDJZgdHKpQK3TrMDsxzoYYMvlqoLJIDXE1Sp7KYNy12nhdRg 54 R6NXNmW8oMZuxglkmUwayMiPS+N4zNYqv0CXYzlEqCOgq9MJUcAMHt+KpiST+sm6 55 FWkJ9D+biNPyQ9QKf1AE4BdZia4lHfPYU/C/DEL/a5xQuuop/zMQZoGaIA4p2zGQ 56 /maqYxEIM/yRBQpT1jlODKPJrMEgx7SgY2hRU47YZ4fj6350fb6fNBtiiMAfJbjL 57 S3Gh85E9fm3hJaNSPKAaJFYL1Ya2svuWfgHj677C56UcmYis7fhiiy1aJuYdHnSm 58 sD53z/f0J+We4VZjY+pidvA9BGZPFVdR3wd3xGs8/oH6UWaLJAMGkLG6dDb3qDLm 59 1LFZwsX8sdD32i1SiWanYQYSYMyFWr0awi4xdoMtYCL7uKBYtwtPyvq3cj4IrJlb 60 mfeFhT57UbE4qukTDIQ0Y0WM40UYRTakRaDY7ubhXgLgx09Cnp9XTVMsHgT6j9/i 61 1pxsB104XLWjQHTjr1JtiaBQEwFh9r2OKTcpvaLcbNtYpo7CzOs= 62 =FRsO 63 -----END PGP SIGNATURE----- 64 65 empty commit` 66 67 sha := SHA1{0xfe, 0xaf, 0x4b, 0xa6, 0xbc, 0x63, 0x5f, 0xec, 0x44, 0x2f, 0x46, 0xdd, 0xd4, 0x51, 0x24, 0x16, 0xec, 0x43, 0xc2, 0xc2} 68 gitRepo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare")) 69 assert.NoError(t, err) 70 assert.NotNil(t, gitRepo) 71 defer gitRepo.Close() 72 73 commitFromReader, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString)) 74 assert.NoError(t, err) 75 if !assert.NotNil(t, commitFromReader) { 76 return 77 } 78 assert.EqualValues(t, sha, commitFromReader.ID) 79 assert.EqualValues(t, `-----BEGIN PGP SIGNATURE----- 80 81 iQIzBAABCAAdFiEEWPb2jX6FS2mqyJRQLmK0HJOGlEMFAl00zmEACgkQLmK0HJOG 82 lEMDFBAAhQKKqLD1VICygJMEB8t1gBmNLgvziOLfpX4KPWdPtBk3v/QJ7OrfMrVK 83 xlC4ZZyx6yMm1Q7GzmuWykmZQJ9HMaHJ49KAbh5MMjjV/+OoQw9coIdo8nagRUld 84 vX8QHzNZ6Agx77xHuDJZgdHKpQK3TrMDsxzoYYMvlqoLJIDXE1Sp7KYNy12nhdRg 85 R6NXNmW8oMZuxglkmUwayMiPS+N4zNYqv0CXYzlEqCOgq9MJUcAMHt+KpiST+sm6 86 FWkJ9D+biNPyQ9QKf1AE4BdZia4lHfPYU/C/DEL/a5xQuuop/zMQZoGaIA4p2zGQ 87 /maqYxEIM/yRBQpT1jlODKPJrMEgx7SgY2hRU47YZ4fj6350fb6fNBtiiMAfJbjL 88 S3Gh85E9fm3hJaNSPKAaJFYL1Ya2svuWfgHj677C56UcmYis7fhiiy1aJuYdHnSm 89 sD53z/f0J+We4VZjY+pidvA9BGZPFVdR3wd3xGs8/oH6UWaLJAMGkLG6dDb3qDLm 90 1LFZwsX8sdD32i1SiWanYQYSYMyFWr0awi4xdoMtYCL7uKBYtwtPyvq3cj4IrJlb 91 mfeFhT57UbE4qukTDIQ0Y0WM40UYRTakRaDY7ubhXgLgx09Cnp9XTVMsHgT6j9/i 92 1pxsB104XLWjQHTjr1JtiaBQEwFh9r2OKTcpvaLcbNtYpo7CzOs= 93 =FRsO 94 -----END PGP SIGNATURE----- 95 `, commitFromReader.Signature.Signature) 96 assert.EqualValues(t, `tree f1a6cb52b2d16773290cefe49ad0684b50a4f930 97 parent 37991dec2c8e592043f47155ce4808d4580f9123 98 author silverwind <me@silverwind.io> 1563741793 +0200 99 committer silverwind <me@silverwind.io> 1563741793 +0200 100 101 empty commit`, commitFromReader.Signature.Payload) 102 assert.EqualValues(t, "silverwind <me@silverwind.io>", commitFromReader.Author.String()) 103 104 commitFromReader2, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString+"\n\n")) 105 assert.NoError(t, err) 106 commitFromReader.CommitMessage += "\n\n" 107 commitFromReader.Signature.Payload += "\n\n" 108 assert.EqualValues(t, commitFromReader, commitFromReader2) 109 } 110 111 func TestHasPreviousCommit(t *testing.T) { 112 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 113 114 repo, err := openRepositoryWithDefaultContext(bareRepo1Path) 115 assert.NoError(t, err) 116 defer repo.Close() 117 118 commit, err := repo.GetCommit("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0") 119 assert.NoError(t, err) 120 121 parentSHA := MustIDFromString("8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2") 122 notParentSHA := MustIDFromString("2839944139e0de9737a044f78b0e4b40d989a9e3") 123 124 haz, err := commit.HasPreviousCommit(parentSHA) 125 assert.NoError(t, err) 126 assert.True(t, haz) 127 128 hazNot, err := commit.HasPreviousCommit(notParentSHA) 129 assert.NoError(t, err) 130 assert.False(t, hazNot) 131 132 selfNot, err := commit.HasPreviousCommit(commit.ID) 133 assert.NoError(t, err) 134 assert.False(t, selfNot) 135 } 136 137 func TestParseCommitFileStatus(t *testing.T) { 138 type testcase struct { 139 output string 140 added []string 141 removed []string 142 modified []string 143 } 144 145 kases := []testcase{ 146 { 147 // Merge commit 148 output: "MM\x00options/locale/locale_en-US.ini\x00", 149 modified: []string{ 150 "options/locale/locale_en-US.ini", 151 }, 152 added: []string{}, 153 removed: []string{}, 154 }, 155 { 156 // Spaces commit 157 output: "D\x00b\x00D\x00b b/b\x00A\x00b b/b b/b b/b\x00A\x00b b/b b/b b/b b/b\x00", 158 removed: []string{ 159 "b", 160 "b b/b", 161 }, 162 modified: []string{}, 163 added: []string{ 164 "b b/b b/b b/b", 165 "b b/b b/b b/b b/b", 166 }, 167 }, 168 { 169 // larger commit 170 output: "M\x00go.mod\x00M\x00go.sum\x00M\x00modules/ssh/ssh.go\x00M\x00vendor/github.com/gliderlabs/ssh/circle.yml\x00M\x00vendor/github.com/gliderlabs/ssh/context.go\x00A\x00vendor/github.com/gliderlabs/ssh/go.mod\x00A\x00vendor/github.com/gliderlabs/ssh/go.sum\x00M\x00vendor/github.com/gliderlabs/ssh/server.go\x00M\x00vendor/github.com/gliderlabs/ssh/session.go\x00M\x00vendor/github.com/gliderlabs/ssh/ssh.go\x00M\x00vendor/golang.org/x/sys/unix/mkerrors.sh\x00M\x00vendor/golang.org/x/sys/unix/syscall_darwin.go\x00M\x00vendor/golang.org/x/sys/unix/zerrors_darwin_amd64.go\x00M\x00vendor/golang.org/x/sys/unix/zerrors_darwin_arm64.go\x00M\x00vendor/golang.org/x/sys/unix/zerrors_freebsd_386.go\x00M\x00vendor/golang.org/x/sys/unix/zerrors_freebsd_amd64.go\x00M\x00vendor/golang.org/x/sys/unix/zerrors_freebsd_arm.go\x00M\x00vendor/golang.org/x/sys/unix/zerrors_freebsd_arm64.go\x00M\x00vendor/golang.org/x/sys/unix/zerrors_linux.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_darwin_amd64.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_darwin_arm64.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_dragonfly_amd64.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_freebsd_386.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_freebsd_amd64.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_freebsd_arm.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_freebsd_arm64.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_netbsd_386.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_netbsd_amd64.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_netbsd_arm.go\x00M\x00vendor/golang.org/x/sys/unix/ztypes_netbsd_arm64.go\x00M\x00vendor/modules.txt\x00", 171 modified: []string{ 172 "go.mod", 173 "go.sum", 174 "modules/ssh/ssh.go", 175 "vendor/github.com/gliderlabs/ssh/circle.yml", 176 "vendor/github.com/gliderlabs/ssh/context.go", 177 "vendor/github.com/gliderlabs/ssh/server.go", 178 "vendor/github.com/gliderlabs/ssh/session.go", 179 "vendor/github.com/gliderlabs/ssh/ssh.go", 180 "vendor/golang.org/x/sys/unix/mkerrors.sh", 181 "vendor/golang.org/x/sys/unix/syscall_darwin.go", 182 "vendor/golang.org/x/sys/unix/zerrors_darwin_amd64.go", 183 "vendor/golang.org/x/sys/unix/zerrors_darwin_arm64.go", 184 "vendor/golang.org/x/sys/unix/zerrors_freebsd_386.go", 185 "vendor/golang.org/x/sys/unix/zerrors_freebsd_amd64.go", 186 "vendor/golang.org/x/sys/unix/zerrors_freebsd_arm.go", 187 "vendor/golang.org/x/sys/unix/zerrors_freebsd_arm64.go", 188 "vendor/golang.org/x/sys/unix/zerrors_linux.go", 189 "vendor/golang.org/x/sys/unix/ztypes_darwin_amd64.go", 190 "vendor/golang.org/x/sys/unix/ztypes_darwin_arm64.go", 191 "vendor/golang.org/x/sys/unix/ztypes_dragonfly_amd64.go", 192 "vendor/golang.org/x/sys/unix/ztypes_freebsd_386.go", 193 "vendor/golang.org/x/sys/unix/ztypes_freebsd_amd64.go", 194 "vendor/golang.org/x/sys/unix/ztypes_freebsd_arm.go", 195 "vendor/golang.org/x/sys/unix/ztypes_freebsd_arm64.go", 196 "vendor/golang.org/x/sys/unix/ztypes_netbsd_386.go", 197 "vendor/golang.org/x/sys/unix/ztypes_netbsd_amd64.go", 198 "vendor/golang.org/x/sys/unix/ztypes_netbsd_arm.go", 199 "vendor/golang.org/x/sys/unix/ztypes_netbsd_arm64.go", 200 "vendor/modules.txt", 201 }, 202 added: []string{ 203 "vendor/github.com/gliderlabs/ssh/go.mod", 204 "vendor/github.com/gliderlabs/ssh/go.sum", 205 }, 206 removed: []string{}, 207 }, 208 { 209 // git 1.7.2 adds an unnecessary \x00 on merge commit 210 output: "\x00MM\x00options/locale/locale_en-US.ini\x00", 211 modified: []string{ 212 "options/locale/locale_en-US.ini", 213 }, 214 added: []string{}, 215 removed: []string{}, 216 }, 217 { 218 // git 1.7.2 adds an unnecessary \n on normal commit 219 output: "\nD\x00b\x00D\x00b b/b\x00A\x00b b/b b/b b/b\x00A\x00b b/b b/b b/b b/b\x00", 220 removed: []string{ 221 "b", 222 "b b/b", 223 }, 224 modified: []string{}, 225 added: []string{ 226 "b b/b b/b b/b", 227 "b b/b b/b b/b b/b", 228 }, 229 }, 230 } 231 232 for _, kase := range kases { 233 fileStatus := NewCommitFileStatus() 234 parseCommitFileStatus(fileStatus, strings.NewReader(kase.output)) 235 236 assert.Equal(t, kase.added, fileStatus.Added) 237 assert.Equal(t, kase.removed, fileStatus.Removed) 238 assert.Equal(t, kase.modified, fileStatus.Modified) 239 } 240 }