Skip to content

Commit d22c66e

Browse files
committed
feat: add api for revert changes
1 parent 33e21ca commit d22c66e

File tree

11 files changed

+254
-174
lines changed

11 files changed

+254
-174
lines changed

api/jiaozifs.gen.go

Lines changed: 88 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,11 +1051,16 @@ paths:
10511051
required: true
10521052
schema:
10531053
type: string
1054+
- in: query
1055+
name: pathPrefix
1056+
description: prefix of path
1057+
schema:
1058+
type: string
10541059
post:
10551060
tags:
10561061
- wip
1057-
operationId: revertWip
1058-
summary: revert working in process
1062+
operationId: revertWipChanges
1063+
summary: revert changes in working in process, empty path will revert all
10591064
responses:
10601065
200:
10611066
description: success to revert wip

controller/commit_ctl.go

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -154,35 +154,13 @@ func (commitCtl CommitController) CompareCommit(ctx context.Context, w *api.Jiao
154154
return
155155
}
156156

157-
changes, err := workRepo.DiffCommit(ctx, toCommitHash)
157+
changes, err := workRepo.DiffCommit(ctx, toCommitHash, utils.StringValue(params.Path))
158158
if err != nil {
159159
w.Error(err)
160160
return
161161
}
162162

163-
path := versionmgr.CleanPath(utils.StringValue(params.Path))
164-
var changesResp []api.Change
165-
err = changes.ForEach(func(change versionmgr.IChange) error {
166-
action, err := change.Action()
167-
if err != nil {
168-
return err
169-
}
170-
fullPath := change.Path()
171-
if strings.HasPrefix(fullPath, path) {
172-
apiChange := api.Change{
173-
Action: api.ChangeAction(action),
174-
Path: fullPath,
175-
}
176-
if change.From() != nil {
177-
apiChange.BaseHash = utils.String(hex.EncodeToString(change.From().Hash()))
178-
}
179-
if change.To() != nil {
180-
apiChange.ToHash = utils.String(hex.EncodeToString(change.To().Hash()))
181-
}
182-
changesResp = append(changesResp, apiChange)
183-
}
184-
return nil
185-
})
163+
changesResp, err := changesToDTO(changes)
186164
if err != nil {
187165
w.Error(err)
188166
return
@@ -226,35 +204,13 @@ func (commitCtl CommitController) GetCommitChanges(ctx context.Context, w *api.J
226204
return
227205
}
228206

229-
changes, err := workRepo.GetCommitChanges(ctx)
207+
changes, err := workRepo.GetCommitChanges(ctx, utils.StringValue(params.Path))
230208
if err != nil {
231209
w.Error(err)
232210
return
233211
}
234212

235-
path := versionmgr.CleanPath(utils.StringValue(params.Path))
236-
var changesResp []api.Change
237-
err = changes.ForEach(func(change versionmgr.IChange) error {
238-
action, err := change.Action()
239-
if err != nil {
240-
return err
241-
}
242-
fullPath := change.Path()
243-
if strings.HasPrefix(fullPath, path) {
244-
apiChange := api.Change{
245-
Action: api.ChangeAction(action),
246-
Path: fullPath,
247-
}
248-
if change.From() != nil {
249-
apiChange.BaseHash = utils.String(hex.EncodeToString(change.From().Hash()))
250-
}
251-
if change.To() != nil {
252-
apiChange.ToHash = utils.String(hex.EncodeToString(change.To().Hash()))
253-
}
254-
changesResp = append(changesResp, apiChange)
255-
}
256-
return nil
257-
})
213+
changesResp, err := changesToDTO(changes)
258214
if err != nil {
259215
w.Error(err)
260216
return

controller/helper.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package controller
2+
3+
import (
4+
"encoding/hex"
5+
6+
"github.com/jiaozifs/jiaozifs/api"
7+
"github.com/jiaozifs/jiaozifs/utils"
8+
"github.com/jiaozifs/jiaozifs/versionmgr"
9+
)
10+
11+
func changesToDTO(changes *versionmgr.Changes) ([]api.Change, error) {
12+
changesResp := make([]api.Change, 0)
13+
err := changes.ForEach(func(change versionmgr.IChange) error {
14+
action, err := change.Action()
15+
if err != nil {
16+
return err
17+
}
18+
fullPath := change.Path()
19+
apiChange := api.Change{
20+
Action: api.ChangeAction(action),
21+
Path: fullPath,
22+
}
23+
if change.From() != nil {
24+
apiChange.BaseHash = utils.String(hex.EncodeToString(change.From().Hash()))
25+
}
26+
if change.To() != nil {
27+
apiChange.ToHash = utils.String(hex.EncodeToString(change.To().Hash()))
28+
}
29+
changesResp = append(changesResp, apiChange)
30+
return nil
31+
})
32+
if err != nil {
33+
return nil, err
34+
}
35+
return changesResp, nil
36+
}

controller/wip_ctl.go

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package controller
33
import (
44
"bytes"
55
"context"
6-
"encoding/hex"
76
"net/http"
8-
"strings"
97

108
"github.com/jiaozifs/jiaozifs/api"
119
"github.com/jiaozifs/jiaozifs/auth"
@@ -255,46 +253,22 @@ func (wipCtl WipController) GetWipChanges(ctx context.Context, w *api.JiaozifsRe
255253
return
256254
}
257255

258-
changes, err := workTree.Diff(ctx, wip.CurrentTree)
256+
changes, err := workTree.Diff(ctx, wip.CurrentTree, utils.StringValue(params.Path))
259257
if err != nil {
260258
w.Error(err)
261259
return
262260
}
263261

264-
path := versionmgr.CleanPath(utils.StringValue(params.Path))
265-
266-
var changesResp []api.Change
267-
err = changes.ForEach(func(change versionmgr.IChange) error {
268-
action, err := change.Action()
269-
if err != nil {
270-
return err
271-
}
272-
fullPath := change.Path()
273-
if strings.HasPrefix(fullPath, path) {
274-
apiChange := api.Change{
275-
Action: api.ChangeAction(action),
276-
Path: fullPath,
277-
}
278-
if change.From() != nil {
279-
apiChange.BaseHash = utils.String(hex.EncodeToString(change.From().Hash()))
280-
}
281-
if change.To() != nil {
282-
apiChange.ToHash = utils.String(hex.EncodeToString(change.To().Hash()))
283-
}
284-
changesResp = append(changesResp, apiChange)
285-
}
286-
return nil
287-
})
262+
changesResp, err := changesToDTO(changes)
288263
if err != nil {
289264
w.Error(err)
290265
return
291266
}
292-
293267
w.JSON(changesResp)
294268
}
295269

296-
// RevertWip revert wip changes to base commit
297-
func (wipCtl WipController) RevertWip(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.RevertWipParams) {
270+
// RevertWipChanges revert wip changes, if path is empty, revert all
271+
func (wipCtl WipController) RevertWipChanges(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.RevertWipChangesParams) {
298272
operator, err := auth.GetOperator(ctx)
299273
if err != nil {
300274
w.Error(err)
@@ -330,7 +304,7 @@ func (wipCtl WipController) RevertWip(ctx context.Context, w *api.JiaozifsRespon
330304
return
331305
}
332306

333-
err = workRepo.RevertWip(ctx)
307+
err = workRepo.Revert(ctx, utils.StringValue(params.PathPrefix))
334308
if err != nil {
335309
w.Error(err)
336310
return

makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ SWAGGER_ARG=
2323
swagger-srv:
2424
swagger serve $(SWAGGER_ARG) -F swagger ./api/swagger.yml
2525

26+
test: gen-api
27+
go test -timeout=30m -parallel=4 -v ./...
2628
build:gen-api
2729
go build $(GOFLAGS) -o jzfs

versionmgr/changes.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,7 @@ func (c *Change) To() noder.Path {
4444

4545
// Path return change path
4646
func (c *Change) Path() string {
47-
action, err := c.Action()
48-
if err != nil {
49-
panic(err)
50-
}
51-
52-
var path string
53-
if action == merkletrie.Delete {
54-
path = c.Change.From.String()
55-
} else {
56-
path = c.Change.To.String()
57-
}
58-
59-
return path
47+
return c.Change.Path()
6048
}
6149

6250
// Changes used to recored changes between commit, also provider iter function

versionmgr/merkletrie/change.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func (c *Change) Action() (Action, error) {
5656
return Modify, nil
5757
}
5858

59+
// Path returns the path of the change.
60+
func (c *Change) Path() string {
61+
if c.From != nil {
62+
return c.From.String()
63+
}
64+
return c.To.String()
65+
}
66+
5967
// NewInsert returns a new Change representing the insertion of n.
6068
func NewInsert(n noder.Path) Change { return Change{To: n} }
6169

0 commit comments

Comments
 (0)