使用Golang实现`git log -G …`命令。

huangapple go评论76阅读模式
英文:

`git log -G ...` with golang

问题

我想编写一个类似于 git log -G 的脚本,使用 go-git 库。

这段代码打印出了仓库的所有提交记录,但是如何获取每个提交的添加/删除行呢?

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing/object"
)

func main() {
	if len(os.Args) != 2 {
		fmt.Println("参数过少,请指定包含 git 仓库的目录。")
		os.Exit(1)
	}
	err := SearchLog(os.Args[1])
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func SearchLog(dir string) error {
	files, err := os.ReadDir(dir)
	if err != nil {
		return err
	}
	for _, f := range files {
		if f.IsDir() {
			path := filepath.Join(dir, f.Name())
			r, err := git.PlainOpen(path)
			if err != nil {
				fmt.Printf("%s %v", f.Name(), err)
				continue
			}
			err = searchLogInRepo(r)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func searchLogInRepo(r *git.Repository) error {
	options := git.LogOptions{}
	cIter, err := r.Log(&options)
	if err != nil {
		return err
	}
	err = cIter.ForEach(func(c *object.Commit) error {
		fmt.Println(c)
		return nil
	})
	return err
}

以上是你要翻译的内容。

英文:

I want to write a script which is similar to git log -G with go-git

This code prints all commits of the repos, but how to get the added/removed lines of each commit?

package main
import (
"fmt"
"os"
"path/filepath"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("too few arguments. Please specify a directory containing git repos.")
os.Exit(1)
}
err := SearchLog(os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func SearchLog(dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() {
path := filepath.Join(dir, f.Name())
r, err := git.PlainOpen(path)
if err != nil {
fmt.Printf("%s %v", f.Name(), err)
continue
}
err = searchLogInRepo(r)
if err != nil {
return err
}
}
}
return nil
}
func searchLogInRepo(r *git.Repository) error {
options := git.LogOptions{}
cIter, err := r.Log(&options)
if err != nil {
return err
}
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c)
return nil
})
return err
}

答案1

得分: 1

如何获取每个提交的添加/删除行?

仅使用git log -G <regex>命令无法显示提交的内容

只有使用git log -p -G命令才能显示内容。并且您需要使用grep命令来搜索您要查找的表达式,以便隔离补丁文本中包含与<regex>匹配的添加/删除行的差异。

对于每个提交,您需要从其父提交中获取补丁(例如plumbing/object/commit_test.go),然后查找您的正则表达式。

英文:

> how to get the added/removed lines of each commit?

A git log -G &lt;regex&gt; alone would not show the commit content.

Only a, for instance, git log -p -G would.
And you would need to grep for your searched expression, in order to isolate the differences whose patch text contains added/removed lines that match &lt;regex&gt;.

For each commit, you would need to get the patch from its parent commit (as in plumbing/object/commit_test.go, and look for your regex.

huangapple
  • 本文由 发表于 2022年7月1日 15:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/72825734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定