Skip to content

Commit 73847b9

Browse files
committed
feat: many fix
1 parent be5983c commit 73847b9

File tree

15 files changed

+314
-28
lines changed

15 files changed

+314
-28
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ init and running
1515

1616
./jzfs daemon
1717
```
18+
19+
20+
revert object只revert一个对象

controller/branch_ctl.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ func CheckBranchName(name string) error {
3636
return fmt.Errorf("branch format must be <name> or <name>/<name>")
3737
}
3838

39-
if !branchNameRegex.Match([]byte(seg[0])) || !branchNameRegex.Match([]byte(seg[1])) {
39+
if !branchNameRegex.Match([]byte(seg[0])) {
4040
return fmt.Errorf("branch name must be combination of number and letter or combine with '/'")
4141
}
42+
if len(seg) > 2 {
43+
if !branchNameRegex.Match([]byte(seg[1])) {
44+
return fmt.Errorf("branch name must be combination of number and letter or combine with '/'")
45+
}
46+
}
4247
return nil
4348
}
4449

controller/repository_ctl.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,52 @@ func (repositoryCtl RepositoryController) DeleteRepository(ctx context.Context,
273273
return
274274
}
275275

276-
repo, err := repositoryCtl.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetName(repositoryName).SetOwnerID(owner.ID))
276+
repository, err := repositoryCtl.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetName(repositoryName).SetOwnerID(owner.ID))
277277
if err != nil {
278278
w.Error(err)
279279
return
280280
}
281281

282-
affectRows, err := repositoryCtl.Repo.RepositoryRepo().Delete(ctx, models.NewDeleteRepoParams().SetID(repo.ID))
282+
err = repositoryCtl.Repo.Transaction(ctx, func(repo models.IRepo) error {
283+
// delete repository
284+
affectRows, err := repositoryCtl.Repo.RepositoryRepo().Delete(ctx, models.NewDeleteRepoParams().SetID(repository.ID))
285+
if err != nil {
286+
return err
287+
}
288+
289+
if affectRows == 0 {
290+
return fmt.Errorf("repo not found %w", models.ErrNotFound)
291+
}
292+
293+
//delete branch
294+
_, err = repositoryCtl.Repo.BranchRepo().Delete(ctx, models.NewDeleteBranchParams().SetRepositoryID(repository.ID))
295+
if err != nil {
296+
return err
297+
}
298+
299+
//delete commit
300+
_, err = repositoryCtl.Repo.CommitRepo(repository.ID).Delete(ctx, models.NewDeleteParams())
301+
if err != nil {
302+
return err
303+
}
304+
305+
//delete tag
306+
_, err = repositoryCtl.Repo.TagRepo(repository.ID).Delete(ctx, models.NewDeleteParams())
307+
if err != nil {
308+
return err
309+
}
310+
// delete tree
311+
_, err = repositoryCtl.Repo.FileTreeRepo(repository.ID).Delete(ctx, models.NewDeleteTreeParams())
312+
if err != nil {
313+
return err
314+
}
315+
return err
316+
})
283317
if err != nil {
284318
w.Error(err)
285319
return
286320
}
287321

288-
if affectRows == 0 {
289-
w.NotFound()
290-
return
291-
}
292322
w.OK()
293323
}
294324

controller/wip_ctl.go

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"encoding/hex"
77
"errors"
8-
"fmt"
98
"net/http"
109
"strings"
1110
"time"
@@ -58,9 +57,9 @@ func (wipCtl WipController) CreateWip(ctx context.Context, w *api.JiaozifsRespon
5857
return
5958
}
6059

61-
_, err = wipCtl.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetCreatorID(operator.ID).SetRepositoryID(repository.ID).SetRefID(ref.ID))
60+
wip, err := wipCtl.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetCreatorID(operator.ID).SetRepositoryID(repository.ID).SetRefID(ref.ID))
6261
if err == nil {
63-
w.BadRequest(fmt.Sprintf("ref %s already in wip", params.RefName))
62+
w.JSON(wip)
6463
return
6564
}
6665
if err != nil && !errors.Is(err, models.ErrNotFound) {
@@ -78,7 +77,7 @@ func (wipCtl WipController) CreateWip(ctx context.Context, w *api.JiaozifsRespon
7877
currentTreeHash = baseCommit.TreeHash
7978
}
8079

81-
wip := &models.WorkingInProcess{
80+
wip = &models.WorkingInProcess{
8281
CurrentTree: currentTreeHash,
8382
BaseCommit: ref.CommitHash,
8483
RepositoryID: repository.ID,
@@ -128,12 +127,43 @@ func (wipCtl WipController) GetWip(ctx context.Context, w *api.JiaozifsResponse,
128127
}
129128

130129
wip, err := wipCtl.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetRefID(ref.ID).SetCreatorID(operator.ID).SetRepositoryID(repository.ID))
131-
if err != nil {
130+
if err == nil {
131+
w.JSON(wip)
132+
return
133+
}
134+
135+
if err != nil && !errors.Is(err, models.ErrNotFound) {
132136
w.Error(err)
133137
return
134138
}
135139

136-
w.JSON(wip)
140+
// if not found create a wip
141+
currentTreeHash := hash.EmptyHash
142+
if !ref.CommitHash.IsEmpty() {
143+
baseCommit, err := wipCtl.Repo.CommitRepo(repository.ID).Commit(ctx, ref.CommitHash)
144+
if err != nil {
145+
w.Error(err)
146+
return
147+
}
148+
currentTreeHash = baseCommit.TreeHash
149+
}
150+
151+
wip = &models.WorkingInProcess{
152+
CurrentTree: currentTreeHash,
153+
BaseCommit: ref.CommitHash,
154+
RepositoryID: repository.ID,
155+
RefID: ref.ID,
156+
State: 0,
157+
CreatorID: operator.ID,
158+
CreatedAt: time.Now(),
159+
UpdatedAt: time.Now(),
160+
}
161+
wip, err = wipCtl.Repo.WipRepo().Insert(ctx, wip)
162+
if err != nil {
163+
w.Error(err)
164+
return
165+
}
166+
w.JSON(wip, http.StatusCreated)
137167
}
138168

139169
// ListWip return wips of branches, operator only see himself wips in specific repository
@@ -303,20 +333,24 @@ func (wipCtl WipController) GetWipChanges(ctx context.Context, w *api.JiaozifsRe
303333
return
304334
}
305335

306-
commit, err := wipCtl.Repo.CommitRepo(repository.ID).Commit(ctx, wip.BaseCommit)
307-
if err != nil {
308-
w.Error(err)
309-
return
336+
treeHash := hash.EmptyHash
337+
if !wip.BaseCommit.IsEmpty() {
338+
commit, err := wipCtl.Repo.CommitRepo(repository.ID).Commit(ctx, wip.BaseCommit)
339+
if err != nil {
340+
w.Error(err)
341+
return
342+
}
343+
treeHash = commit.Hash
310344
}
311345

312-
workTree, err := versionmgr.NewWorkTree(ctx, wipCtl.Repo.FileTreeRepo(repository.ID), models.NewRootTreeEntry(commit.TreeHash))
313-
if err != nil {
314-
w.Error(err)
346+
if bytes.Equal(treeHash, wip.CurrentTree) {
347+
w.JSON([]api.Change{}) //no change return nothing
315348
return
316349
}
317350

318-
if bytes.Equal(commit.TreeHash, wip.CurrentTree) {
319-
w.JSON([]api.Change{}) //no change return nothing
351+
workTree, err := versionmgr.NewWorkTree(ctx, wipCtl.Repo.FileTreeRepo(repository.ID), models.NewRootTreeEntry(treeHash))
352+
if err != nil {
353+
w.Error(err)
320354
return
321355
}
322356

integrationtest/wip_object_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,24 @@ func WipObjectSpec(ctx context.Context, urlStr string) func(c convey.C) {
295295
uploadObject(ctx, c, client, "update f5 to test branch", userName, repoName, branchName, "b.dat")
296296
uploadObject(ctx, c, client, "update f6 to test branch", userName, repoName, branchName, "c.dat")
297297

298+
testBranchName := "test/empty_branch"
299+
createBranch(ctx, c, client, userName, repoName, "main", testBranchName)
300+
createWip(ctx, c, client, "create empty_branch wip", userName, repoName, testBranchName)
301+
302+
c.Convey("get wip success on init", func(c convey.C) {
303+
resp, err := client.GetWipChanges(ctx, userName, repoName, &api.GetWipChangesParams{
304+
RefName: testBranchName,
305+
})
306+
convey.So(err, convey.ShouldBeNil)
307+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
308+
309+
result, err := api.ParseGetWipChangesResponse(resp)
310+
convey.So(err, convey.ShouldBeNil)
311+
convey.So(*result.JSON200, convey.ShouldHaveLength, 0)
312+
})
313+
298314
c.Convey("get wip changes", func(c convey.C) {
315+
299316
c.Convey("no auth", func() {
300317
re := client.RequestEditors
301318
client.RequestEditors = nil

integrationtest/wip_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ func WipSpec(ctx context.Context, urlStr string) func(c convey.C) {
240240
}
241241
}
242242

243-
func createWip(ctx context.Context, c convey.C, client *api.Client, random string, user string, repoName string, refName string) {
244-
c.Convey("create wip "+random, func() {
243+
func createWip(ctx context.Context, c convey.C, client *api.Client, title string, user string, repoName string, refName string) {
244+
c.Convey("create wip "+title, func() {
245245
resp, err := client.CreateWip(ctx, user, repoName, &api.CreateWipParams{
246246
RefName: refName,
247247
})

models/commit.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,24 @@ func (commit *Commit) NumParents() int {
108108
return len(commit.ParentHashes)
109109
}
110110

111+
type DeleteParams struct {
112+
hash hash.Hash
113+
}
114+
115+
func NewDeleteParams() *DeleteParams {
116+
return &DeleteParams{}
117+
}
118+
119+
func (params *DeleteParams) SetHash(hash hash.Hash) *DeleteParams {
120+
params.hash = hash
121+
return params
122+
}
123+
111124
type ICommitRepo interface {
112125
RepositoryID() uuid.UUID
113126
Commit(ctx context.Context, hash hash.Hash) (*Commit, error)
114127
Insert(ctx context.Context, commit *Commit) (*Commit, error)
128+
Delete(ctx context.Context, params *DeleteParams) (int64, error)
115129
}
116130
type CommitRepo struct {
117131
db bun.IDB
@@ -150,3 +164,20 @@ func (cr CommitRepo) Insert(ctx context.Context, commit *Commit) (*Commit, error
150164
}
151165
return commit, nil
152166
}
167+
168+
func (cr CommitRepo) Delete(ctx context.Context, params *DeleteParams) (int64, error) {
169+
query := cr.db.NewDelete().Model((*Commit)(nil)).Where("repository_id = ?", cr.repositoryID)
170+
if params.hash != nil {
171+
query = query.Where("hash = ?", params.hash)
172+
}
173+
174+
sqlResult, err := query.Exec(ctx)
175+
if err != nil {
176+
return 0, err
177+
}
178+
affectedRows, err := sqlResult.RowsAffected()
179+
if err != nil {
180+
return 0, err
181+
}
182+
return affectedRows, err
183+
}

models/commit_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,42 @@ func TestCommitRepo(t *testing.T) {
3838
require.ErrorIs(t, err, models.ErrRepoIDMisMatch)
3939
})
4040
}
41+
42+
func TestDeleteCOmmit(t *testing.T) {
43+
ctx := context.Background()
44+
postgres, _, db := testhelper.SetupDatabase(ctx, t)
45+
defer postgres.Stop() //nolint
46+
t.Run("delete commit", func(t *testing.T) {
47+
repoID := uuid.New()
48+
commitRepo := models.NewCommitRepo(db, repoID)
49+
require.Equal(t, commitRepo.RepositoryID(), repoID)
50+
51+
toDeleteModel := &models.Commit{}
52+
require.NoError(t, gofakeit.Struct(toDeleteModel))
53+
toDeleteModel.RepositoryID = repoID
54+
toDeleteModel, err := commitRepo.Insert(ctx, toDeleteModel)
55+
require.NoError(t, err)
56+
57+
affectRows, err := commitRepo.Delete(ctx, models.NewDeleteParams().SetHash(toDeleteModel.Hash))
58+
require.NoError(t, err)
59+
require.Equal(t, int64(1), affectRows)
60+
})
61+
62+
t.Run("delete batch", func(t *testing.T) {
63+
repoID := uuid.New()
64+
commitRepo := models.NewCommitRepo(db, repoID)
65+
require.Equal(t, commitRepo.RepositoryID(), repoID)
66+
67+
for i := 0; i < 5; i++ {
68+
toDeleteModel := &models.Commit{}
69+
require.NoError(t, gofakeit.Struct(toDeleteModel))
70+
toDeleteModel.RepositoryID = repoID
71+
toDeleteModel, err := commitRepo.Insert(ctx, toDeleteModel)
72+
require.NoError(t, err)
73+
}
74+
75+
affectRows, err := commitRepo.Delete(ctx, models.NewDeleteParams())
76+
require.NoError(t, err)
77+
require.Equal(t, int64(5), affectRows)
78+
})
79+
}

models/models.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ func SetupDatabase(ctx context.Context, lc fx.Lifecycle, dbConfig *config.Databa
2121

2222
bunDB := bun.NewDB(sqlDB, pgdialect.New(), bun.WithDiscardUnknownColumns())
2323

24-
if dbConfig.Debug {
25-
bunDB.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
26-
}
24+
bunDB.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
2725

2826
lc.Append(fx.Hook{
2927
OnStop: func(ctx context.Context) error {

models/tag.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ITagRepo interface {
3434
RepositoryID() uuid.UUID
3535
Insert(ctx context.Context, tag *Tag) (*Tag, error)
3636
Tag(ctx context.Context, hash hash.Hash) (*Tag, error)
37+
Delete(ctx context.Context, params *DeleteParams) (int64, error)
3738
}
3839

3940
type TagRepo struct {
@@ -74,3 +75,20 @@ func (t *TagRepo) Tag(ctx context.Context, hash hash.Hash) (*Tag, error) {
7475
}
7576
return tag, nil
7677
}
78+
79+
func (t *TagRepo) Delete(ctx context.Context, params *DeleteParams) (int64, error) {
80+
query := t.db.NewDelete().Model((*Tag)(nil)).Where("repository_id = ?", t.repositoryID)
81+
if params.hash != nil {
82+
query = query.Where("hash = ?", params.hash)
83+
}
84+
85+
sqlResult, err := query.Exec(ctx)
86+
if err != nil {
87+
return 0, err
88+
}
89+
affectedRows, err := sqlResult.RowsAffected()
90+
if err != nil {
91+
return 0, err
92+
}
93+
return affectedRows, err
94+
}

0 commit comments

Comments
 (0)