Skip to content

Commit

Permalink
refactor: rename FileDiffer to VersionController
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Dec 11, 2024
1 parent bb76dca commit 92a78e4
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 117 deletions.
8 changes: 4 additions & 4 deletions cnb-builder-shim/cmd/dev-entrypoint/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main

import (
"fmt"
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/vcs"
"os"
"path/filepath"
"strings"
Expand All @@ -29,7 +30,6 @@ 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 @@ -171,8 +171,8 @@ func initializeSourceCode() error {
}

// 初始化文件对比器
if err = filediffer.New().Prepare(workspace); err != nil {
return errors.Wrap(err, "file differ preparing")
if err = vcs.New().Prepare(workspace); err != nil {
return errors.Wrap(err, "version controller preparing")
}
return nil
}
Expand All @@ -183,7 +183,7 @@ func ensureWorkspace(workspace string) (err error) {
if _, err = os.Stat(workspace); os.IsNotExist(err) {
// 文件夹不存在,创建文件夹
logger.Info("create workspace directory")
if err := os.MkdirAll(workspace, 0750); err != nil {
if err = os.MkdirAll(workspace, 0750); err != nil {
return errors.Wrap(err, "create workspace directory")
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion cnb-builder-shim/cmd/dev-entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
package main

import (
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/webserver"
"os"

"github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox"
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/config"
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/webserver"
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/logging"
)

Expand Down

This file was deleted.

94 changes: 94 additions & 0 deletions cnb-builder-shim/internal/devsandbox/vcs/types.go
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
}
83 changes: 83 additions & 0 deletions cnb-builder-shim/internal/devsandbox/vcs/types_test.go
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))
})
})
})
Loading

0 comments on commit 92a78e4

Please sign in to comment.