首页 > 解决方案 > git shortlog -s -n -e --no-merges output [] 在 go test 但主函数中的调用是正常输出

问题描述

这是作者结构

type Author struct {
    Name         string
    Email        string
    CommitNumber int
}
// get all authors
func GetAllAuthor() []Author {
    args := []string{"shortlog", "-s", "-n", "-e", "--no-merges"}
    var authors []Author

    info := utils.MustExecRtOut(Cmd, args...)

    lines := strings.Split(info, "\n")
    for _, line := range lines {
        if len(strings.TrimSpace(line)) > 0 {
            author := GetAuthor(line)
            authors = append(authors, author)
        }
    }
    return authors
}

// get information about an author
func GetAuthor(line string) Author {
    authorInfo := strings.Fields(strings.TrimSpace(line))
    emailIndex := len(authorInfo) - 1

    email := strings.TrimSpace(authorInfo[emailIndex])
    email = utils.SubString(email, 1, len(email)-1)
    author := strings.Join(authorInfo[1:emailIndex], " ")
    number, err := strconv.Atoi(authorInfo[0])

    if err != nil {
        fmt.Printf("%s parse commit number %s error!\n", line, authorInfo[0])
    }

    return Author{
        Name:         author,
        Email:        email,
        CommitNumber: number,
    }

}

当我调用GetAllAuthor测试函数时,输出是一个空数组

[]

但是当我在main函数中调试或调用时,输出可以像这样正常

[{Joeeeeeeey 123@xx.com 169} {托尼 123@xx.com 62}]

标签: gitgotestinglogging

解决方案


文档所述git shortlog

如果在命令行上没有传递修订,并且标准输入不是终端或没有当前分支,git shortlog 则将输出从标准输入读取的日志摘要,而不参考当前存储库。

您没有向我们展示utils.MustExecRtOut,但我们可能会猜测它提供了一个不是终端的标准输入。如果是这种情况,您将必须提供至少一个起始版本,例如分支名称。

或者,git shortlog在您遇到不希望的行为的情况下,您可能在没有当前分支名称的情况下运行。这也将通过传递分支名称来解决。


推荐阅读