-
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.
refactor: rename FileDiffer to VersionController
- Loading branch information
Showing
9 changed files
with
310 additions
and
117 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
13 changes: 0 additions & 13 deletions
13
cnb-builder-shim/internal/devsandbox/filediffer/filediffer_suite_test.go
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* TencentBlueKing is pleased to support the open source community by making | ||
* 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/MIT | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* We undertake not to change the open source license (MIT license) applicable | ||
* to the current version of the project delivered to anyone in the future. | ||
*/ | ||
|
||
// Package vcs 版本控制系统(VersionControlSystem) | ||
package vcs | ||
|
||
import ( | ||
"slices" | ||
"strings" | ||
) | ||
|
||
// 强制忽略的文件路径前缀 | ||
var forceIgnoreFilePathPrefixes = []string{"v3logs"} | ||
|
||
// FileAction 文件操作类型 | ||
type FileAction string | ||
|
||
const ( | ||
// FileActionAdded 文件增加 | ||
FileActionAdded FileAction = "added" | ||
// FileActionModified 文件变更 | ||
FileActionModified FileAction = "modified" | ||
// FileActionDeleted 文件删除 | ||
FileActionDeleted FileAction = "deleted" | ||
) | ||
|
||
// File 文件详情 | ||
type File struct { | ||
Action FileAction `json:"action"` | ||
Path string `json:"path"` | ||
Content string `json:"content"` | ||
} | ||
|
||
// Files 文件列表 | ||
type Files []File | ||
|
||
// DirTree 目录树 | ||
type DirTree struct { | ||
Name string `json:"name"` | ||
Dirs []*DirTree `json:"dirs"` | ||
Files Files `json:"files"` | ||
} | ||
|
||
// AsTree 转换成目录树形式 | ||
func (files Files) AsTree() DirTree { | ||
// 按照文件路径排序 | ||
slices.SortFunc(files, func(a, b File) int { | ||
return strings.Compare(a.Path, b.Path) | ||
}) | ||
|
||
root := DirTree{Name: "/"} | ||
var cur *DirTree | ||
for _, f := range files { | ||
parts := strings.Split(f.Path, "/") | ||
cur = &root | ||
// 循环创建目录树 | ||
for _, part := range parts[:len(parts)-1] { | ||
if part == "" { | ||
continue | ||
} | ||
exists := false | ||
for _, dir := range cur.Dirs { | ||
if dir.Name == part { | ||
cur = dir | ||
exists = true | ||
break | ||
} | ||
} | ||
if !exists { | ||
cur.Dirs = append(cur.Dirs, &DirTree{Name: part}) | ||
cur = cur.Dirs[len(cur.Dirs)-1] | ||
} | ||
} | ||
// 目录树格式下,路径即为文件名 | ||
f.Path = parts[len(parts)-1] | ||
cur.Files = append(cur.Files, f) | ||
} | ||
return root | ||
} |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* TencentBlueKing is pleased to support the open source community by making | ||
* 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/MIT | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* We undertake not to change the open source license (MIT license) applicable | ||
* to the current version of the project delivered to anyone in the future. | ||
*/ | ||
|
||
package vcs | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Test Types", func() { | ||
|
||
Context("Test Files", func() { | ||
It("AsTree", func() { | ||
files := Files{ | ||
{Action: FileActionAdded, Path: "README.md"}, | ||
{Action: FileActionAdded, Path: "webfe/static/example.css"}, | ||
{Action: FileActionAdded, Path: "webfe/static/example.js"}, | ||
{Action: FileActionDeleted, Path: "api/main.py"}, | ||
{Action: FileActionDeleted, Path: "webfe/templates/example.html"}, | ||
{Action: FileActionModified, Path: "backend/main.go"}, | ||
{Action: FileActionModified, Path: "backend/types.go"}, | ||
{Action: FileActionModified, Path: "docs/example.txt"}, | ||
} | ||
|
||
excepted := DirTree{ | ||
Name: "/", | ||
Dirs: []*DirTree{ | ||
{ | ||
Name: "api", Files: Files{ | ||
{Action: FileActionDeleted, Path: "main.py"}, | ||
}, | ||
}, | ||
{ | ||
Name: "backend", Files: Files{ | ||
{Action: FileActionModified, Path: "main.go"}, | ||
{Action: FileActionModified, Path: "types.go"}, | ||
}, | ||
}, | ||
{ | ||
Name: "docs", Files: Files{ | ||
{Action: FileActionModified, Path: "example.txt"}, | ||
}, | ||
}, | ||
{ | ||
Name: "webfe", Dirs: []*DirTree{ | ||
{ | ||
Name: "static", Files: Files{ | ||
{Action: FileActionAdded, Path: "example.css"}, | ||
{Action: FileActionAdded, Path: "example.js"}, | ||
}, | ||
}, | ||
{ | ||
Name: "templates", Files: Files{ | ||
{Action: FileActionDeleted, Path: "example.html"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Files: Files{ | ||
{Action: FileActionAdded, Path: "README.md"}, | ||
}, | ||
} | ||
Expect(files.AsTree()).To(Equal(excepted)) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.