Skip to content

Commit

Permalink
feat: add api to fetch change in wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hunjixin committed Dec 13, 2023
1 parent da65f86 commit 167a5dc
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 110 deletions.
314 changes: 253 additions & 61 deletions api/jiaozifs.gen.go

Large diffs are not rendered by default.

47 changes: 40 additions & 7 deletions api/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ components:
type: string
CurrentTree:
type: string
BaseTree:
BaseCommit:
type: string
RepositoryID:
type: string
Expand Down Expand Up @@ -693,12 +693,6 @@ paths:
required: true
schema:
type: string
- in: query
name: baseCommitID
description: base commit to create wip
required: true
schema:
type: string
responses:
201:
description: working in process created
Expand All @@ -715,6 +709,45 @@ paths:
502:
description: internal server error

/wip/{repository}/changes:
parameters:
- in: path
name: repository
required: true
schema:
type: string
get:
tags:
- wip
operationId: getWipChanges
summary: get working in process changes
parameters:
- in: query
name: refName
description: ref name
required: true
schema:
type: string
- in: query
name: path
description: path
required: false
schema:
type: string
responses:
200:
description: working in process changes
content:
application/json:
schema:
$ref: "#/components/schemas/Change"
400:
description: ValidationError
401:
description: Unauthorized
403:
description: Forbidden

/wip/{repository}/commit:
parameters:
- in: path
Expand Down
1 change: 1 addition & 0 deletions controller/commit_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (commitCtl CommitController) GetCommitDiff(ctx context.Context, w *api.Jiao
if change.To() != nil {
apiChange.ToHash = utils.String(hex.EncodeToString(change.To().Hash()))
}
changesResp = append(changesResp, apiChange)
}
return nil
})
Expand Down
91 changes: 85 additions & 6 deletions controller/wip_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"fmt"
"net/http"
"strings"
"time"

"github.com/jiaozifs/jiaozifs/auth"
Expand Down Expand Up @@ -45,15 +46,15 @@ func (wipCtl WipController) CreateWip(ctx context.Context, w *api.JiaozifsRespon
return
}

baseCommitID, err := hex.DecodeString(params.BaseCommitID)
baseCommit, err := wipCtl.Repo.ObjectRepo().Commit(ctx, ref.CommitHash)
if err != nil {
w.Error(err)
return
}

wip := &models.WorkingInProcess{
CurrentTree: baseCommitID,
BaseTree: baseCommitID,
CurrentTree: baseCommit.TreeHash,
BaseCommit: ref.CommitHash,
RepositoryID: repo.ID,
RefID: ref.ID,
State: 0,
Expand Down Expand Up @@ -155,7 +156,7 @@ func (wipCtl WipController) CommitWip(ctx context.Context, w *api.JiaozifsRespon
return
}

if !bytes.Equal(commit.TreeHash, wip.BaseTree) {
if !bytes.Equal(commit.Hash, wip.BaseCommit) {
w.Error(fmt.Errorf("base commit not equal with branch, please update wip"))
return
}
Expand All @@ -172,8 +173,8 @@ func (wipCtl WipController) CommitWip(ctx context.Context, w *api.JiaozifsRespon
return err
}

wip.BaseTree = commit.Commit().TreeHash //set for response
err = repo.WipRepo().UpdateByID(ctx, models.NewUpdateWipParams(wip.ID).SetBaseTree(commit.Commit().TreeHash))
wip.BaseCommit = commit.Commit().Hash //set for response
err = repo.WipRepo().UpdateByID(ctx, models.NewUpdateWipParams(wip.ID).SetBaseCommit(wip.BaseCommit))
if err != nil {
return err
}
Expand Down Expand Up @@ -221,3 +222,81 @@ func (wipCtl WipController) DeleteWip(ctx context.Context, w *api.JiaozifsRespon

w.OK()
}

func (wipCtl WipController) GetWipChanges(ctx context.Context, w *api.JiaozifsResponse, r *http.Request, repositoryName string, params api.GetWipChangesParams) {
user, err := auth.GetUser(ctx)
if err != nil {
w.Error(err)
return
}

repository, err := wipCtl.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetName(repositoryName))
if err != nil {
w.Error(err)
return
}

ref, err := wipCtl.Repo.RefRepo().Get(ctx, models.NewGetRefParams().SetName(params.RefName))
if err != nil {
w.Error(err)
return
}

wip, err := wipCtl.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetCreatorID(user.ID).SetRepositoryID(repository.ID).SetRefID(ref.ID))
if err != nil {
w.Error(err)
return
}

commit, err := wipCtl.Repo.ObjectRepo().Commit(ctx, wip.BaseCommit)
if err != nil {
w.Error(err)
return
}

workTree, err := versionmgr.NewWorkTree(ctx, wipCtl.Repo.ObjectRepo(), models.NewRootTreeEntry(commit.TreeHash))
if err != nil {
w.Error(err)
return
}

changes, err := workTree.Diff(ctx, wip.CurrentTree)
if err != nil {
w.Error(err)
return
}

var path string
if params.Path != nil {
path = *params.Path
}

var changesResp []api.Change
err = changes.ForEach(func(change versionmgr.IChange) error {
action, err := change.Action()
if err != nil {
return err
}
fullPath := change.Path()
if strings.HasPrefix(fullPath, path) {
apiChange := api.Change{
Action: int(action),
Path: fullPath,
}
if change.From() != nil {
apiChange.BaseHash = utils.String(hex.EncodeToString(change.From().Hash()))
}
if change.To() != nil {
apiChange.ToHash = utils.String(hex.EncodeToString(change.To().Hash()))
}
changesResp = append(changesResp, apiChange)
}
return nil
})
if err != nil {
w.Error(err)
return
}

w.JSON(changes)
}
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/jiaozifs/jiaozifs

go 1.20

replace github.com/flowchartsman/swaggerui => /home/hunjixin/code/swaggerui

require (
cloud.google.com/go/storage v1.33.0
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.0
Expand All @@ -25,7 +23,6 @@ require (
github.com/flowchartsman/swaggerui v0.0.0-20221017034628-909ed4f3701b
github.com/getkin/kin-openapi v0.118.0
github.com/go-chi/chi/v5 v5.0.10
github.com/go-chi/cors v1.2.1
github.com/go-git/go-git/v5 v5.10.1
github.com/go-openapi/swag v0.22.4
github.com/go-test/deep v1.1.0
Expand All @@ -46,6 +43,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
github.com/puzpuzpuz/xsync v1.5.2
github.com/rs/cors v1.10.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -150,7 +148,6 @@ require (
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ github.com/getkin/kin-openapi v0.118.0 h1:z43njxPmJ7TaPpMSCQb7PN0dEYno4tyBPQcrFd
github.com/getkin/kin-openapi v0.118.0/go.mod h1:l5e9PaFUo9fyLJCPGQeXI2ML8c3P8BHOEV2VaAVf/pc=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
github.com/go-git/go-git/v5 v5.10.1 h1:tu8/D8i+TWxgKpzQ3Vc43e+kkhXqtsZCKI/egajKnxk=
Expand Down
8 changes: 4 additions & 4 deletions models/wip.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type WorkingInProcess struct {
ID uuid.UUID `bun:"id,pk,type:uuid,default:uuid_generate_v4()"`
Name string `bun:"name,notnull"`
CurrentTree hash.Hash `bun:"current_tree,type:bytea,notnull"`
BaseTree hash.Hash `bun:"base_tree,type:bytea,notnull"`
BaseCommit hash.Hash `bun:"base_commit,type:bytea,notnull"`
RepositoryID uuid.UUID `bun:"repository_id,type:uuid,notnull"`
RefID uuid.UUID `bun:"ref_id,type:uuid,notnull"`
State WipState `bun:"state,notnull"`
Expand Down Expand Up @@ -120,7 +120,7 @@ type UpdateWipParams struct {
bun.BaseModel `bun:"table:wips"`
ID uuid.UUID `bun:"id,pk,type:uuid,default:uuid_generate_v4()"`
CurrentTree hash.Hash `bun:"current_tree,type:bytea,notnull"`
BaseTree hash.Hash `bun:"base_tree,type:bytea,notnull"`
BaseCommit hash.Hash `bun:"base_commit,type:bytea,notnull"`
State WipState `bun:"state,notnull"`
UpdatedAt time.Time `bun:"updated_at"`
}
Expand All @@ -134,8 +134,8 @@ func (up *UpdateWipParams) SetCurrentTree(currentTree hash.Hash) *UpdateWipParam
return up
}

func (up *UpdateWipParams) SetBaseTree(baseTree hash.Hash) *UpdateWipParams {
up.BaseTree = baseTree
func (up *UpdateWipParams) SetBaseCommit(commitHash hash.Hash) *UpdateWipParams {
up.BaseCommit = commitHash
return up
}

Expand Down
4 changes: 2 additions & 2 deletions models/wip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ func TestWipRepoUpdateByID(t *testing.T) {

updateModel := models.NewUpdateWipParams(newWipModel.ID).
SetState(models.Completed).
SetBaseTree(hash.Hash("mock base hash")).
SetBaseCommit(hash.Hash("mock base hash")).
SetCurrentTree(hash.Hash("mock hash"))

err = repo.UpdateByID(ctx, updateModel)
require.NoError(t, err)
updatedUser, err := repo.Get(ctx, models.NewGetWipParams().SetID(newWipModel.ID))
require.NoError(t, err)
require.Equal(t, models.Completed, updatedUser.State)
require.Equal(t, "mock base hash", string(updatedUser.BaseTree))
require.Equal(t, "mock base hash", string(updatedUser.BaseCommit))
require.Equal(t, "mock hash", string(updatedUser.CurrentTree))
}
27 changes: 2 additions & 25 deletions versionmgr/commit.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package versionmgr

import (
"bytes"
"context"
"fmt"
"time"

"github.com/go-git/go-git/v5/utils/merkletrie"
"github.com/go-git/go-git/v5/utils/merkletrie/noder"
"github.com/google/uuid"
logging "github.com/ipfs/go-log/v2"
"github.com/jiaozifs/jiaozifs/models"
"github.com/jiaozifs/jiaozifs/models/filemode"
"github.com/jiaozifs/jiaozifs/utils/hash"
)

Expand Down Expand Up @@ -100,35 +96,16 @@ func (commitOp *CommitOp) AddCommit(ctx context.Context, committer *models.User,

// DiffCommit find file changes in two commit
func (commitOp *CommitOp) DiffCommit(ctx context.Context, toCommitID hash.Hash) (*Changes, error) {
fromNode, err := NewTreeNode(ctx, models.TreeEntry{
Name: "",
Mode: filemode.Dir,
Hash: commitOp.commit.TreeHash,
}, commitOp.object)
workTree, err := NewWorkTree(ctx, commitOp.object, models.NewRootTreeEntry(commitOp.Commit().TreeHash))
if err != nil {
return nil, err
}

toCommit, err := commitOp.object.Commit(ctx, toCommitID)
if err != nil {
return nil, err
}
toNode, err := NewTreeNode(ctx, models.TreeEntry{
Name: "",
Mode: filemode.Dir,
Hash: toCommit.TreeHash,
}, commitOp.object)
if err != nil {
return nil, err
}

changes, err := merkletrie.DiffTreeContext(ctx, fromNode, toNode, func(a, b noder.Hasher) bool {
return bytes.Equal(a.Hash(), b.Hash())
})
if err != nil {
return nil, err
}
return newChanges(changes), nil
return workTree.Diff(ctx, toCommit.TreeHash)
}

// Merge implement merge like git, docs https://en.wikipedia.org/wiki/Merge_(version_control)
Expand Down
21 changes: 21 additions & 0 deletions versionmgr/worktree.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package versionmgr

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -10,6 +11,8 @@ import (
"strings"
"time"

"github.com/go-git/go-git/v5/utils/merkletrie/noder"

"github.com/jiaozifs/jiaozifs/utils/httputil"

"github.com/go-git/go-git/v5/utils/merkletrie"
Expand Down Expand Up @@ -564,3 +567,21 @@ func (workTree *WorkTree) ApplyOneChange(ctx context.Context, change IChange) er
}
return fmt.Errorf("unexpect change action: %s", action)
}

func (workTree *WorkTree) Diff(ctx context.Context, rootTreeHash hash.Hash) (*Changes, error) {
toNode, err := NewTreeNode(ctx, models.NewRootTreeEntry(rootTreeHash), workTree.object)
if err != nil {
return nil, err
}

changes, err := merkletrie.DiffTreeContext(ctx, workTree.Root(), toNode, func(a, b noder.Hasher) bool {
return bytes.Equal(a.Hash(), b.Hash())
})
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return newChanges(changes), nil
}

0 comments on commit 167a5dc

Please sign in to comment.