diff --git a/cnb-builder-shim/cmd/dev-entrypoint/init.go b/cnb-builder-shim/cmd/dev-entrypoint/init.go index 5990e87d6e..e67613d7e8 100644 --- a/cnb-builder-shim/cmd/dev-entrypoint/init.go +++ b/cnb-builder-shim/cmd/dev-entrypoint/init.go @@ -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" ) @@ -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 } diff --git a/cnb-builder-shim/internal/devsandbox/filediffer/filediffer.go b/cnb-builder-shim/internal/devsandbox/filediffer/filediffer.go index f4ef0ef500..3ff7b333d3 100644 --- a/cnb-builder-shim/internal/devsandbox/filediffer/filediffer.go +++ b/cnb-builder-shim/internal/devsandbox/filediffer/filediffer.go @@ -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 准备步骤 @@ -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 " -m "init" + // 没有 .git 目录(使用子目录部署的情况),执行若干命令以初始化 if os.IsNotExist(err) { commands := [][]string{ {"init"}, {"add", "."}, - {"commit", "--author=bkpaas ", "-m", "init"}, + {"config", "user.name", "bkpaas"}, + {"config", "user.email", "bkpaas@example.com"}, + {"commit", "-m", "init"}, } for _, cmd := range commands { if _, err = d.runGitCommand(cmd...); err != nil { @@ -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 } @@ -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 + } +} diff --git a/cnb-builder-shim/internal/devsandbox/filediffer/filediffer_test.go b/cnb-builder-shim/internal/devsandbox/filediffer/filediffer_test.go index 3dfcd2de06..e887f2652f 100644 --- a/cnb-builder-shim/internal/devsandbox/filediffer/filediffer_test.go +++ b/cnb-builder-shim/internal/devsandbox/filediffer/filediffer_test.go @@ -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()) } @@ -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 ", "-m", "init")).To(BeNil()) + Expect(runGitCommand(tmpDir, "config", "user.name", "bkpaas")).To(BeNil()) + Expect(runGitCommand(tmpDir, "config", "user.email", "bkpaas@example.com")).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() @@ -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() @@ -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") diff --git a/cnb-builder-shim/internal/devsandbox/webserver/server.go b/cnb-builder-shim/internal/devsandbox/webserver/server.go index eed9d260e8..6370db442e 100644 --- a/cnb-builder-shim/internal/devsandbox/webserver/server.go +++ b/cnb-builder-shim/internal/devsandbox/webserver/server.go @@ -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(