Skip to content

Commit 2ef85ca

Browse files
authored
feat: guess the indentation for <dependency> element when writing XML (#1856)
Currently we use the common indentation when writing `<dependency>` elements. This PR improves the writer behaviour by getting the indentation of the first `<dependency>` element in dependencies so the newly added XMLs are more readable.
1 parent 8fd626b commit 2ef85ca

3 files changed

Lines changed: 80 additions & 53 deletions

File tree

cmd/osv-scanner/fix/__snapshots__/command_test.snap

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3892,25 +3892,25 @@ Rewriting <tempdir>/pom.xml...
38923892
<httpclient.version>4.5.13</httpclient.version>
38933893
</properties>
38943894

3895-
<dependencyManagement>
3896-
<dependencies>
3897-
<dependency>
3898-
<groupId>commons-io</groupId>
3899-
<artifactId>commons-io</artifactId>
3900-
<version>2.14.0</version>
3901-
</dependency>
3902-
<dependency>
3903-
<groupId>org.jsoup</groupId>
3904-
<artifactId>jsoup</artifactId>
3905-
<version>1.15.3</version>
3906-
</dependency>
3907-
<dependency>
3908-
<groupId>org.apache.httpcomponents</groupId>
3909-
<artifactId>httpclient</artifactId>
3910-
<version>${httpclient.version}</version>
3911-
</dependency>
3912-
</dependencies>
3913-
</dependencyManagement>
3895+
<dependencyManagement>
3896+
<dependencies>
3897+
<dependency>
3898+
<groupId>commons-io</groupId>
3899+
<artifactId>commons-io</artifactId>
3900+
<version>2.14.0</version>
3901+
</dependency>
3902+
<dependency>
3903+
<groupId>org.jsoup</groupId>
3904+
<artifactId>jsoup</artifactId>
3905+
<version>1.15.3</version>
3906+
</dependency>
3907+
<dependency>
3908+
<groupId>org.apache.httpcomponents</groupId>
3909+
<artifactId>httpclient</artifactId>
3910+
<version>${httpclient.version}</version>
3911+
</dependency>
3912+
</dependencies>
3913+
</dependencyManagement>
39143914

39153915
<dependencies>
39163916
<dependency>
@@ -4082,25 +4082,25 @@ Rewriting <tempdir>/pom.xml...
40824082
<httpclient.version>4.5.13</httpclient.version>
40834083
</properties>
40844084

4085-
<dependencyManagement>
4086-
<dependencies>
4087-
<dependency>
4088-
<groupId>commons-io</groupId>
4089-
<artifactId>commons-io</artifactId>
4090-
<version>2.14.0</version>
4091-
</dependency>
4092-
<dependency>
4093-
<groupId>org.jsoup</groupId>
4094-
<artifactId>jsoup</artifactId>
4095-
<version>1.15.3</version>
4096-
</dependency>
4097-
<dependency>
4098-
<groupId>org.apache.httpcomponents</groupId>
4099-
<artifactId>httpclient</artifactId>
4100-
<version>${httpclient.version}</version>
4101-
</dependency>
4102-
</dependencies>
4103-
</dependencyManagement>
4085+
<dependencyManagement>
4086+
<dependencies>
4087+
<dependency>
4088+
<groupId>commons-io</groupId>
4089+
<artifactId>commons-io</artifactId>
4090+
<version>2.14.0</version>
4091+
</dependency>
4092+
<dependency>
4093+
<groupId>org.jsoup</groupId>
4094+
<artifactId>jsoup</artifactId>
4095+
<version>1.15.3</version>
4096+
</dependency>
4097+
<dependency>
4098+
<groupId>org.apache.httpcomponents</groupId>
4099+
<artifactId>httpclient</artifactId>
4100+
<version>${httpclient.version}</version>
4101+
</dependency>
4102+
</dependencies>
4103+
</dependencyManagement>
41044104

41054105
<dependencies>
41064106
<dependency>

cmd/osv-scanner/fix/fixtures/override-maven/pom.xml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
<httpclient.version>4.0</httpclient.version>
1010
</properties>
1111

12-
<dependencyManagement>
13-
<dependencies>
14-
<dependency>
15-
<groupId>org.jsoup</groupId>
16-
<artifactId>jsoup</artifactId>
17-
<version>1.14.1</version>
18-
</dependency>
19-
<dependency>
20-
<groupId>org.apache.httpcomponents</groupId>
21-
<artifactId>httpclient</artifactId>
22-
<version>${httpclient.version}</version>
23-
</dependency>
24-
</dependencies>
25-
</dependencyManagement>
12+
<dependencyManagement>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.jsoup</groupId>
16+
<artifactId>jsoup</artifactId>
17+
<version>1.14.1</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.apache.httpcomponents</groupId>
21+
<artifactId>httpclient</artifactId>
22+
<version>${httpclient.version}</version>
23+
</dependency>
24+
</dependencies>
25+
</dependencyManagement>
2626

2727
<dependencies>
2828
<dependency>

internal/resolution/manifest/maven.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,32 @@ func writeProject(w io.Writer, enc *internalxml.Encoder, raw, prefix, id string,
854854
return enc.Flush()
855855
}
856856

857+
// indentation returns the indentation of the dependency element.
858+
// If dependencies or dependency elements are not found, the default
859+
// indentation (four space) is returned.
860+
func indentation(raw string) string {
861+
i := strings.Index(raw, "<dependencies>")
862+
if i < 0 {
863+
return " "
864+
}
865+
866+
raw = raw[i+len("<dependencies>"):]
867+
// Find the first dependency element.
868+
j := strings.Index(raw, "<dependency>")
869+
if j < 0 {
870+
return " "
871+
}
872+
873+
raw = raw[:j]
874+
// Find the last new line and get the space between.
875+
k := strings.LastIndex(raw, "\n")
876+
if k < 0 {
877+
return " "
878+
}
879+
880+
return raw[k+1:]
881+
}
882+
857883
func writeDependency(w io.Writer, enc *internalxml.Encoder, raw string, patches map[MavenPatch]bool) error {
858884
dec := internalxml.NewDecoder(bytes.NewReader([]byte(raw)))
859885
for {
@@ -889,7 +915,8 @@ func writeDependency(w io.Writer, enc *internalxml.Encoder, raw string, patches
889915
// Sort dependencies for consistency in testing.
890916
slices.SortFunc(deps, compareDependency)
891917

892-
enc.Indent(" ", " ")
918+
enc.Indent(indentation(raw), " ")
919+
893920
// Write a new line to keep the format.
894921
if _, err := w.Write([]byte("\n")); err != nil {
895922
return err

0 commit comments

Comments
 (0)