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
0 commit comments