Skip to content

Commit

Permalink
fix: FileDiffer add WithContent option
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Dec 8, 2024
1 parent 82e5811 commit 59b9e98
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
6 changes: 6 additions & 0 deletions cnb-builder-shim/cmd/dev-entrypoint/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pkg/errors"

"github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/config"
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/filediffer"
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/fetcher/http"
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/utils"
)
Expand Down Expand Up @@ -168,6 +169,11 @@ func initializeSourceCode() error {
case config.GIT:
return fmt.Errorf("TODO: clone git from revision")
}

// 初始化文件对比器
if err = filediffer.New().Prepare(workspace); err != nil {
return errors.Wrap(err, "file differ preparing")
}
return nil
}

Expand Down
32 changes: 23 additions & 9 deletions cnb-builder-shim/internal/devsandbox/filediffer/filediffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ import (

// FileDiffer 文件变更对比器
type FileDiffer struct {
srcPath string
srcPath string
withContent bool
}

// New ...
func New() *FileDiffer {
return &FileDiffer{}
func New(opts ...Option) *FileDiffer {
differ := &FileDiffer{}
for _, opt := range opts {
opt(differ)
}
return differ
}

// Prepare 准备步骤
Expand All @@ -48,15 +53,14 @@ func (d *FileDiffer) Prepare(srcPath string) error {
return nil
}

// 没有 .git 目录(使用子目录部署的情况),执行以下命令以初始化
// 1. git init
// 2. git add .
// 3. git commit --author="bkpaas <[email protected]>" -m "init"
// 没有 .git 目录(使用子目录部署的情况),执行若干命令以初始化
if os.IsNotExist(err) {
commands := [][]string{
{"init"},
{"add", "."},
{"commit", "--author=bkpaas <[email protected]>", "-m", "init"},
{"config", "user.name", "bkpaas"},
{"config", "user.email", "[email protected]"},
{"commit", "-m", "init"},
}
for _, cmd := range commands {
if _, err = d.runGitCommand(cmd...); err != nil {
Expand Down Expand Up @@ -108,7 +112,7 @@ func (d *FileDiffer) Diff() ([]File, error) {

var content string
// 如果是删除操作,不加载文件
if action != FileActionDeleted {
if d.withContent && action != FileActionDeleted {
if content, err = d.loadFileContent(fields[1]); err != nil {
return nil, err
}
Expand Down Expand Up @@ -157,3 +161,13 @@ func (d *FileDiffer) mustIgnoreFile(filepath string) bool {
}
return false
}

// Option Differ 选项
type Option func(*FileDiffer)

// WithContent Diff 时是否加载文件内容
func WithContent() Option {
return func(d *FileDiffer) {
d.withContent = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var _ = Describe("Test Differ", func() {

// 初始化临时目录 & 文件
BeforeEach(func() {
tmpDir, _ = os.MkdirTemp("", "filediffer")
tmpDir, _ = os.MkdirTemp("", "file-differ")
for _, filename := range []string{"example.txt", "example.py", "example.go"} {
Expect(initFile(tmpDir, filename, path.Ext(filename))).To(BeNil())
}
Expand All @@ -80,9 +80,11 @@ var _ = Describe("Test Differ", func() {
It("with .git", func() {
Expect(runGitCommand(tmpDir, "init")).To(BeNil())
Expect(runGitCommand(tmpDir, "add", ".")).To(BeNil())
Expect(runGitCommand(tmpDir, "commit", "--author=bkpaas <[email protected]>", "-m", "init")).To(BeNil())
Expect(runGitCommand(tmpDir, "config", "user.name", "bkpaas")).To(BeNil())
Expect(runGitCommand(tmpDir, "config", "user.email", "[email protected]")).To(BeNil())
Expect(runGitCommand(tmpDir, "commit", "-m", "init")).To(BeNil())

differ := New()
differ := New(WithContent())
Expect(differ.Prepare(tmpDir)).To(BeNil())

files, err := differ.Diff()
Expand Down Expand Up @@ -119,7 +121,7 @@ var _ = Describe("Test Differ", func() {
})

It("without .git", func() {
differ := New()
differ := New(WithContent())
Expect(differ.Prepare(tmpDir)).To(BeNil())

files, err := differ.Diff()
Expand Down Expand Up @@ -148,7 +150,7 @@ var _ = Describe("Test Differ", func() {
})

It("with ignore", func() {
differ := New()
differ := New(WithContent())
Expect(differ.Prepare(tmpDir)).To(BeNil())

_ = initFile(path.Join(tmpDir, "webfe/templates"), "example.html", ".html")
Expand Down
2 changes: 1 addition & 1 deletion cnb-builder-shim/internal/devsandbox/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func DiffsHandler() gin.HandlerFunc {
return
}

differ := filediffer.New()
differ := filediffer.New(filediffer.WithContent())
// 初始化
if err := differ.Prepare(config.G.SourceCode.Workspace); err != nil {
c.JSON(
Expand Down

0 comments on commit 59b9e98

Please sign in to comment.