Trim 来自 git 的路径在 Groovy 脚本中产生不同的结果

Trim the paths from git diff result in Groovyscript

我正在尝试 trim 来自 git diff 结果的路径。这是我尝试过的:

def ti = "git -C C:\PIE\src\repo diff --name-only".execute()
String directory =ti.text
File  file= new File(directory)

 def newlines = []
    file.toString().eachLine {String line ->
    if(line.contains("src")){

        line = line.replace("/src","")
    }
    newlines<<line

    }
    println newlines
    

gif diff 的输出例如:

a/b/c/src/ping.java
d/e/f/src/vim.java
g/h/j/src/key.java

我想 trim 文件 currentDir 中的每一行作为 :

a/b/c
d/e/f
g/h/j

如果您想删除 /src 部分及其后面的所有内容,您可以这样做:

line = line.replaceAll("/src.*","") 

而不是你拥有的:

line = line.replace("/src","")