Skip to content

Commit 33e21ca

Browse files
committed
feat: add api to get commit changes
1 parent 35112af commit 33e21ca

File tree

14 files changed

+758
-198
lines changed

14 files changed

+758
-198
lines changed

README.md

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

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

api/jiaozifs.gen.go

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

api/swagger.yml

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,8 +1265,8 @@ paths:
12651265
get:
12661266
tags:
12671267
- commit
1268-
operationId: getCommitDiff
1269-
summary: get commit differences
1268+
operationId: compareCommit
1269+
summary: compare two commit
12701270
parameters:
12711271
- in: query
12721272
name: path
@@ -1290,6 +1290,50 @@ paths:
12901290
503:
12911291
description: server internal error
12921292

1293+
/repos/{owner}/{repository}/changes/{commit_id}:
1294+
parameters:
1295+
- in: path
1296+
name: owner
1297+
required: true
1298+
schema:
1299+
type: string
1300+
- in: path
1301+
name: repository
1302+
required: true
1303+
schema:
1304+
type: string
1305+
- in: path
1306+
name: commit_id
1307+
required: true
1308+
schema:
1309+
type: string
1310+
get:
1311+
tags:
1312+
- commit
1313+
operationId: getCommitChanges
1314+
summary: get changes in commit
1315+
parameters:
1316+
- in: query
1317+
name: path
1318+
description: specific path, if not specific return entries in root
1319+
required: false
1320+
schema:
1321+
type: string
1322+
responses:
1323+
200:
1324+
description: commit diff
1325+
content:
1326+
application/json:
1327+
schema:
1328+
type: array
1329+
items:
1330+
$ref: "#/components/schemas/Change"
1331+
400:
1332+
description: ValidationError
1333+
401:
1334+
description: Unauthorized
1335+
503:
1336+
description: server internal error
12931337
/repos/{owner}/{repository}/commits:
12941338
parameters:
12951339
- in: path

controller/branch_ctl.go

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"net/http"
88
"regexp"
99
"strings"
10-
"time"
10+
11+
"github.com/jiaozifs/jiaozifs/block/params"
12+
13+
"github.com/jiaozifs/jiaozifs/versionmgr"
1114

1215
"github.com/jiaozifs/jiaozifs/api"
1316
"github.com/jiaozifs/jiaozifs/auth"
1417
"github.com/jiaozifs/jiaozifs/models"
1518
"github.com/jiaozifs/jiaozifs/utils"
16-
"github.com/jiaozifs/jiaozifs/utils/hash"
1719
"go.uber.org/fx"
1820
)
1921

@@ -50,7 +52,8 @@ func CheckBranchName(name string) error {
5052
type BranchController struct {
5153
fx.In
5254

53-
Repo models.IRepo
55+
Repo models.IRepo
56+
PublicStorageConfig params.AdapterConfig
5457
}
5558

5659
func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.ListBranchesParams) {
@@ -153,42 +156,31 @@ func (bct BranchController) CreateBranch(ctx context.Context, w *api.JiaozifsRes
153156
return
154157
}
155158

156-
//check exit
157-
_, err = bct.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetName(body.Name).SetRepositoryID(repository.ID))
158-
if err == nil {
159-
w.BadRequest(fmt.Sprintf("%s already exit", body.Name))
160-
return
161-
}
162-
if err != nil && !errors.Is(err, models.ErrNotFound) {
163-
w.Error(err)
164-
return
165-
}
166159
//get source branch
167160
sourceBranch, err := bct.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetName(body.Source).SetRepositoryID(repository.ID))
168161
if err != nil && !errors.Is(err, models.ErrNotFound) {
169162
w.Error(err)
170163
return
171164
}
172165

173-
commitHash := hash.EmptyHash
174-
if sourceBranch != nil {
175-
commitHash = sourceBranch.CommitHash
166+
workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, bct.Repo, bct.PublicStorageConfig)
167+
if err != nil {
168+
w.Error(err)
169+
return
176170
}
177171

178-
// Create branch
179-
newBranch := &models.Branch{
180-
RepositoryID: repository.ID,
181-
CommitHash: commitHash,
182-
Name: body.Name,
183-
CreatorID: operator.ID,
184-
CreatedAt: time.Now(),
185-
UpdatedAt: time.Now(),
172+
err = workRepo.CheckOut(ctx, versionmgr.InCommit, sourceBranch.CommitHash.Hex())
173+
if err != nil {
174+
w.Error(err)
175+
return
186176
}
187-
newBranch, err = bct.Repo.BranchRepo().Insert(ctx, newBranch)
177+
178+
newBranch, err := workRepo.CreateBranch(ctx, body.Name)
188179
if err != nil {
189180
w.Error(err)
190181
return
191182
}
183+
192184
w.JSON(api.Branch{
193185
CommitHash: newBranch.CommitHash.Hex(),
194186
CreatedAt: newBranch.CreatedAt,
@@ -226,14 +218,21 @@ func (bct BranchController) DeleteBranch(ctx context.Context, w *api.JiaozifsRes
226218
return
227219
}
228220

229-
// Delete branch
230-
affectedRows, err := bct.Repo.BranchRepo().Delete(ctx, models.NewDeleteBranchParams().SetName(params.RefName).SetRepositoryID(repository.ID))
221+
workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, bct.Repo, bct.PublicStorageConfig)
222+
if err != nil {
223+
w.Error(err)
224+
return
225+
}
226+
227+
err = workRepo.CheckOut(ctx, versionmgr.InBranch, params.RefName)
231228
if err != nil {
232229
w.Error(err)
233230
return
234231
}
235-
if affectedRows == 0 {
236-
w.NotFound()
232+
233+
err = workRepo.DeleteBranch(ctx)
234+
if err != nil {
235+
w.Error(err)
237236
return
238237
}
239238
w.OK()

controller/commit_ctl.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (commitCtl CommitController) GetEntriesInRef(ctx context.Context, w *api.Ji
106106
w.JSON(treeEntry)
107107
}
108108

109-
func (commitCtl CommitController) GetCommitDiff(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, basehead string, params api.GetCommitDiffParams) {
109+
func (commitCtl CommitController) CompareCommit(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, basehead string, params api.CompareCommitParams) {
110110
operator, err := auth.GetOperator(ctx)
111111
if err != nil {
112112
w.Error(err)
@@ -189,3 +189,75 @@ func (commitCtl CommitController) GetCommitDiff(ctx context.Context, w *api.Jiao
189189
}
190190
w.JSON(changesResp)
191191
}
192+
193+
func (commitCtl CommitController) GetCommitChanges(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, commitID string, params api.GetCommitChangesParams) {
194+
operator, err := auth.GetOperator(ctx)
195+
if err != nil {
196+
w.Error(err)
197+
return
198+
}
199+
200+
owner, err := commitCtl.Repo.UserRepo().Get(ctx, models.NewGetUserParams().SetName(ownerName))
201+
if err != nil {
202+
w.Error(err)
203+
return
204+
}
205+
206+
repository, err := commitCtl.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetName(repositoryName).SetOwnerID(owner.ID))
207+
if err != nil {
208+
w.Error(err)
209+
return
210+
}
211+
212+
if operator.ID != owner.ID { //todo check permission
213+
w.Forbidden()
214+
return
215+
}
216+
217+
workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, commitCtl.Repo, commitCtl.PublicStorageConfig)
218+
if err != nil {
219+
w.Error(err)
220+
return
221+
}
222+
223+
err = workRepo.CheckOut(ctx, versionmgr.InCommit, commitID)
224+
if err != nil {
225+
w.Error(err)
226+
return
227+
}
228+
229+
changes, err := workRepo.GetCommitChanges(ctx)
230+
if err != nil {
231+
w.Error(err)
232+
return
233+
}
234+
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+
})
258+
if err != nil {
259+
w.Error(err)
260+
return
261+
}
262+
w.JSON(changesResp)
263+
}

controller/wip_ctl.go

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"bytes"
55
"context"
66
"encoding/hex"
7-
"errors"
87
"net/http"
98
"strings"
10-
"time"
119

1210
"github.com/jiaozifs/jiaozifs/api"
1311
"github.com/jiaozifs/jiaozifs/auth"
@@ -51,50 +49,27 @@ func (wipCtl WipController) GetWip(ctx context.Context, w *api.JiaozifsResponse,
5149
return
5250
}
5351

54-
ref, err := wipCtl.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetRepositoryID(repository.ID).SetName(params.RefName))
52+
workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, wipCtl.Repo, wipCtl.PublicStorageConfig)
5553
if err != nil {
5654
w.Error(err)
5755
return
5856
}
5957

60-
wip, err := wipCtl.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetRefID(ref.ID).SetCreatorID(operator.ID).SetRepositoryID(repository.ID))
61-
if err == nil {
62-
w.JSON(wip)
63-
return
64-
}
65-
66-
if err != nil && !errors.Is(err, models.ErrNotFound) {
58+
err = workRepo.CheckOut(ctx, versionmgr.InBranch, params.RefName)
59+
if err != nil {
6760
w.Error(err)
6861
return
6962
}
7063

71-
// if not found create a wip
72-
currentTreeHash := hash.EmptyHash
73-
if !ref.CommitHash.IsEmpty() {
74-
baseCommit, err := wipCtl.Repo.CommitRepo(repository.ID).Commit(ctx, ref.CommitHash)
75-
if err != nil {
76-
w.Error(err)
77-
return
78-
}
79-
currentTreeHash = baseCommit.TreeHash
80-
}
81-
82-
wip = &models.WorkingInProcess{
83-
CurrentTree: currentTreeHash,
84-
BaseCommit: ref.CommitHash,
85-
RepositoryID: repository.ID,
86-
RefID: ref.ID,
87-
State: 0,
88-
CreatorID: operator.ID,
89-
CreatedAt: time.Now(),
90-
UpdatedAt: time.Now(),
91-
}
92-
wip, err = wipCtl.Repo.WipRepo().Insert(ctx, wip)
64+
wip, isNew, err := workRepo.GetOrCreateWip(ctx)
9365
if err != nil {
9466
w.Error(err)
9567
return
9668
}
97-
w.JSON(wip, http.StatusCreated)
69+
if isNew {
70+
w.JSON(wip, http.StatusCreated)
71+
}
72+
w.JSON(wip)
9873
}
9974

10075
// ListWip return wips of branches, operator only see himself wips in specific repository
@@ -202,28 +177,23 @@ func (wipCtl WipController) DeleteWip(ctx context.Context, w *api.JiaozifsRespon
202177
return
203178
}
204179

205-
ref, err := wipCtl.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetRepositoryID(repository.ID).SetName(params.RefName))
180+
workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, wipCtl.Repo, wipCtl.PublicStorageConfig)
206181
if err != nil {
207182
w.Error(err)
208183
return
209184
}
210185

211-
deleteWipParams := models.NewDeleteWipParams().
212-
SetCreatorID(operator.ID). //todo admin delete
213-
SetRepositoryID(repository.ID).
214-
SetRefID(ref.ID)
215-
216-
affectedRaw, err := wipCtl.Repo.WipRepo().Delete(ctx, deleteWipParams)
186+
err = workRepo.CheckOut(ctx, versionmgr.InBranch, params.RefName)
217187
if err != nil {
218188
w.Error(err)
219189
return
220190
}
221191

222-
if affectedRaw == 0 {
223-
w.NotFound()
192+
err = workRepo.DeleteWip(ctx)
193+
if err != nil {
194+
w.Error(err)
224195
return
225196
}
226-
227197
w.OK()
228198
}
229199

@@ -323,8 +293,8 @@ func (wipCtl WipController) GetWipChanges(ctx context.Context, w *api.JiaozifsRe
323293
w.JSON(changesResp)
324294
}
325295

326-
// RevertWip
327-
func (wipCtl WipController) RevertWip(ctx context.Context, w *api.JiaozifsResponse, r *http.Request, ownerName string, repositoryName string, params api.RevertWipParams) {
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) {
328298
operator, err := auth.GetOperator(ctx)
329299
if err != nil {
330300
w.Error(err)

0 commit comments

Comments
 (0)