Skip to content

Commit

Permalink
set the content to empty instead of deleting the file
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaster99 committed Feb 22, 2025
1 parent 460aa58 commit a460c4e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
33 changes: 17 additions & 16 deletions pkg/archive/jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,25 @@ var StableJAROrderOfAttributeValues = ZipEntryStabilizer{
},
}

var StableGitProperties = ZipArchiveStabilizer{
var StableGitProperties = ZipEntryStabilizer{
Name: "jar-git-properties",
Func: func(mr *MutableZipReader) {
for _, mf := range mr.File {
// These files contain git properties set by git-commit-id-maven-plugin.
// They contain many unreproducible attributes as documented
// [here](https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/825).

// Only JSON and properties file formats are available as documented
// [here](https://github.com/git-commit-id/git-commit-id-maven-plugin/blob/95d616fc7e16018deff3f17e2d03a4b217e55294/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java#L454).
// By default, these file are created in ${project.build.outputDirectory} and are hence at the root of the jar.
// We assume that the file name is 'git' as this is the default value for the plugin.
// However, the plugin allows customizing the file name.
gitRegex := regexp.MustCompile(`\bgit\.(json|properties)$`)
if gitRegex.MatchString(mf.Name) {
mr.DeleteFile(mf.Name)
}
Func: func(zf *MutableZipFile) {
// These files contain git properties set by git-commit-id-maven-plugin.
// They contain many unreproducible attributes as documented
// [here](https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/825).
// Only JSON and properties file formats are available as documented
// [here](https://github.com/git-commit-id/git-commit-id-maven-plugin/blob/95d616fc7e16018deff3f17e2d03a4b217e55294/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java#L454).
// By default, these file are created in ${project.build.outputDirectory} and are hence at the root of the jar.
// We assume that the file name is 'git' as this is the default value for the plugin.
// We don't handle the case where the file name is changed by the user.
gitJsonRegex := regexp.MustCompile(`\bgit\.json$`)
if gitJsonRegex.MatchString(zf.Name) {
zf.SetContent([]byte("{}"))
}

gitPropertiesRegex := regexp.MustCompile(`\bgit\.properties$`)
if gitPropertiesRegex.MatchString(zf.Name) {
zf.SetContent([]byte{})
}
},
}
12 changes: 12 additions & 0 deletions pkg/archive/jar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ func TestStableGitProperties(t *testing.T) {
&zip.FileHeader{Name: "META-INF/MANIFEST.MF"},
[]byte("Built-By: root\r\n\r\n"),
},
{
&zip.FileHeader{Name: "git.properties"},
[]byte{},
},
},
},
{
Expand All @@ -362,6 +366,10 @@ func TestStableGitProperties(t *testing.T) {
&zip.FileHeader{Name: "META-INF/MANIFEST.MF"},
[]byte("Built-By: root\r\n\r\n"),
},
{
&zip.FileHeader{Name: "git.json"},
[]byte("{}"),
},
},
},
{
Expand Down Expand Up @@ -415,6 +423,10 @@ func TestStableGitProperties(t *testing.T) {
&zip.FileHeader{Name: "classes/foo"},
[]byte("bar"),
},
{
&zip.FileHeader{Name: "classes/git.json"},
[]byte("{}"),
},
},
},
}
Expand Down
9 changes: 0 additions & 9 deletions pkg/archive/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,6 @@ func (mr MutableZipReader) WriteTo(zw *zip.Writer) error {
return nil
}

func (mr *MutableZipReader) DeleteFile(name string) {
for i, zipEntry := range mr.File {
if zipEntry.Name == name {
mr.File = append(mr.File[:i], mr.File[i+1:]...)
break
}
}
}

type ZipArchiveStabilizer struct {
Name string
Func func(*MutableZipReader)
Expand Down

0 comments on commit a460c4e

Please sign in to comment.