-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: FileDiffer add WithContent option
- Loading branch information
Showing
4 changed files
with
37 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[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 { | ||
|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[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() | ||
|
@@ -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") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters