Skip to content

Commit 5524b1c

Browse files
committed
Content: Empty ref means default branch
Before we hard-coded main. This lead to failure with old repos that used main as default branch
1 parent 7054dc4 commit 5524b1c

File tree

12 files changed

+36
-17
lines changed

12 files changed

+36
-17
lines changed

docs/configuration.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
},
188188
"ref" : {
189189
"type" : [ "string", "null" ],
190-
"description" : "Reference for a specific branch, tag, or commit"
190+
"description" : "Reference for a specific branch, tag, or commit. Emtpy defaults to default branch of the repo"
191191
},
192192
"target" : {
193193
"type" : [ "string", "null" ],

src/main/groovy/com/cloudogu/gitops/config/Config.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Config {
109109
String path = '.'
110110

111111
@JsonPropertyDescription(CONTENT_REPO_REF_DESCRIPTION)
112-
String ref = 'main'
112+
String ref = ''
113113

114114
@JsonPropertyDescription(CONTENT_REPO_USERNAME_DESCRIPTION)
115115
String username = ''

src/main/groovy/com/cloudogu/gitops/config/ConfigConstants.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface ConfigConstants {
3333
String CONTENT_REPO_DESCRIPTION = "Content repos to push into target environment"
3434
String CONTENT_REPO_URL_DESCRIPTION = "URL of the content repo"
3535
String CONTENT_REPO_PATH_DESCRIPTION = "Path within the content repo to process"
36-
String CONTENT_REPO_REF_DESCRIPTION = "Reference for a specific branch, tag, or commit"
36+
String CONTENT_REPO_REF_DESCRIPTION = "Reference for a specific branch, tag, or commit. Emtpy defaults to default branch of the repo"
3737
String CONTENT_REPO_USERNAME_DESCRIPTION = "Username to authenticate against content repo"
3838
String CONTENT_REPO_PASSWORD_DESCRIPTION = "Password to authenticate against content repo"
3939
String CONTENT_REPO_TEMPLATING_DESCRIPTION = "When true, template all files ending in .ftl within the repo"

src/main/groovy/com/cloudogu/gitops/features/Content.groovy

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import jakarta.inject.Singleton
1717
import org.apache.commons.io.FileUtils
1818
import org.eclipse.jgit.api.CloneCommand
1919
import org.eclipse.jgit.api.Git
20-
import org.eclipse.jgit.api.ResetCommand
2120
import org.eclipse.jgit.lib.Ref
2221
import org.eclipse.jgit.lib.Repository
2322
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
@@ -170,18 +169,18 @@ class Content extends Feature {
170169
def cloneCommand = gitClone()
171170
.setURI(repo.url)
172171
.setDirectory(repoTmpDir)
172+
.setNoCheckout(false) // Checkout default branch
173173

174174
if (repo.username != null && repo.password != null) {
175175
cloneCommand.setCredentialsProvider(
176176
new UsernamePasswordCredentialsProvider(repo.username, repo.password))
177177
}
178178
def git = cloneCommand.call()
179179

180-
def actualRef = findRef(repo, git.repository)
181-
182-
// Avoid jgit removing and staging all files except .git which might lead to CheckoutConflictException during checkout
183-
git.reset().setMode(ResetCommand.ResetType.HARD).call()
184-
git.checkout().setName(actualRef).call()
180+
if (repo.ref) {
181+
def actualRef = findRef(repo, git.repository)
182+
git.checkout().setName(actualRef).call()
183+
}
185184
}
186185

187186
private String findRef(Config.ContentSchema.ContentRepositorySchema repoConfig, Repository gitRepo) {

src/test/groovy/com/cloudogu/gitops/features/ContentTest.groovy

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ class ContentTest {
213213
assertThat(new File(findRoot(repos), "common/branch/README.md").text).contains("someBranch")
214214
}
215215

216+
@Test
217+
void 'Checks out default branch when no ref set'() {
218+
config.content.repos = [
219+
new Config.ContentSchema.ContentRepositorySchema(url: createContentRepo('', 'git-repo-different-default-branch'), target: 'common/default'),
220+
]
221+
222+
def repos = createContent().cloneContentRepos()
223+
224+
assertThat(new File(findRoot(repos), "common/default/README.md")).exists().isFile()
225+
assertThat(new File(findRoot(repos), "common/default/README.md").text).contains("different")
226+
}
227+
216228
@Test
217229
void 'Fails if commit refs does not exit'() {
218230
config.content.repos = [
@@ -489,35 +501,35 @@ class ContentTest {
489501
}
490502

491503

492-
static String createContentRepo(String srcPath = '') {
504+
static String createContentRepo(String initPath = '', String baseBareRepo = 'git-repository') {
493505
// The bare repo works as the "remote"
494506
def bareRepoDir = File.createTempDir('gitops-playground-test-content-repo')
495507
bareRepoDir.deleteOnExit()
496508
foldersToDelete << bareRepoDir
497509
// init with bare repo
498-
FileUtils.copyDirectory(new File(System.getProperty("user.dir") + "/src/test/groovy/com/cloudogu/gitops/utils/data/git-repository/"), bareRepoDir)
510+
FileUtils.copyDirectory(new File(System.getProperty("user.dir") + "/src/test/groovy/com/cloudogu/gitops/utils/data/${baseBareRepo}/"), bareRepoDir)
499511
def bareRepoUri = 'file://' + bareRepoDir.absolutePath
500-
log.debug("Repo $srcPath: bare repo $bareRepoUri")
512+
log.debug("Repo $initPath: bare repo $bareRepoUri")
501513

502-
if (srcPath) {
503-
// Add srcPath to bare repo
514+
if (initPath) {
515+
// Add initPath to bare repo
504516
def tempRepo = File.createTempDir('gitops-playground-temp-repo')
505517
tempRepo.deleteOnExit()
506518
foldersToDelete << tempRepo
507-
log.debug("Repo $srcPath: cloned bare repo to $tempRepo")
519+
log.debug("Repo $initPath: cloned bare repo to $tempRepo")
508520
def git = Git.cloneRepository()
509521
.setURI(bareRepoUri)
510522
.setBranch('main')
511523
.setDirectory(tempRepo)
512524
.call()
513525

514-
FileUtils.copyDirectory(new File(System.getProperty("user.dir") + '/src/test/groovy/com/cloudogu/gitops/utils/data/contentRepos/' + srcPath), tempRepo)
526+
FileUtils.copyDirectory(new File(System.getProperty("user.dir") + '/src/test/groovy/com/cloudogu/gitops/utils/data/contentRepos/' + initPath), tempRepo)
515527

516528
git.add().addFilepattern(".").call()
517529

518530
// Avoid complications with local developer's git config, e.g. when git config --global commit.gpgSign true
519531
SystemReader.getInstance().userConfig.clear()
520-
git.commit().setMessage("Initialize with $srcPath").call()
532+
git.commit().setMessage("Initialize with $initPath").call()
521533
git.push().call()
522534
tempRepo.delete()
523535
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/different
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.

src/test/groovy/com/cloudogu/gitops/utils/data/git-repo-different-default-branch/objects/23/cb5a712ce9ea3dc4770a350fc8ef5f51789d14

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x��1� �agN�v��A�Tc�&v1q��H������`�\���Oi����M�΁�hD��T�X��qG�I{�����I3k )�5�[�A!�R\v��6�OO�N�Mϵ�4�k%P�+آ@dU�I������r�`'��Z,��O`

0 commit comments

Comments
 (0)