@@ -11,7 +11,6 @@ import (
11
11
"github.com/MichaelMure/git-bug-migration/migration1/after/entity"
12
12
"github.com/MichaelMure/git-bug-migration/migration1/after/identity"
13
13
"github.com/MichaelMure/git-bug-migration/migration1/after/repository"
14
- "github.com/MichaelMure/git-bug-migration/migration1/after/util/git"
15
14
"github.com/MichaelMure/git-bug-migration/migration1/after/util/lamport"
16
15
)
17
16
@@ -57,14 +56,14 @@ type Bug struct {
57
56
// Id used as unique identifier
58
57
id entity.Id
59
58
60
- lastCommit git .Hash
61
- rootPack git .Hash
59
+ lastCommit repository .Hash
60
+ rootPack repository .Hash
62
61
63
62
// all the committed operations
64
- packs []OperationPack
63
+ Packs []OperationPack
65
64
66
65
// a temporary pack of operations used for convenience to pile up new operations
67
- // before a commit
66
+ // before_test a commit
68
67
staging OperationPack
69
68
}
70
69
@@ -75,48 +74,32 @@ func NewBug() *Bug {
75
74
return & Bug {}
76
75
}
77
76
78
- // FindLocalBug find an existing Bug matching a prefix
79
- func FindLocalBug (repo repository.ClockedRepo , prefix string ) (* Bug , error ) {
80
- ids , err := ListLocalIds (repo )
81
-
82
- if err != nil {
83
- return nil , err
84
- }
85
-
86
- // preallocate but empty
87
- matching := make ([]entity.Id , 0 , 5 )
88
-
89
- for _ , id := range ids {
90
- if id .HasPrefix (prefix ) {
91
- matching = append (matching , id )
92
- }
93
- }
94
-
95
- if len (matching ) == 0 {
96
- return nil , errors .New ("no matching bug found." )
97
- }
98
-
99
- if len (matching ) > 1 {
100
- return nil , NewErrMultipleMatchBug (matching )
101
- }
102
-
103
- return ReadLocalBug (repo , matching [0 ])
77
+ // ReadLocal will read a local bug from its hash
78
+ func ReadLocal (repo repository.ClockedRepo , id entity.Id ) (* Bug , error ) {
79
+ ref := bugsRefPattern + id .String ()
80
+ return read (repo , identity .NewSimpleResolver (repo ), ref )
104
81
}
105
82
106
- // ReadLocalBug will read a local bug from its hash
107
- func ReadLocalBug (repo repository.ClockedRepo , id entity.Id ) (* Bug , error ) {
83
+ // ReadLocalWithResolver will read a local bug from its hash
84
+ func ReadLocalWithResolver (repo repository.ClockedRepo , identityResolver identity. Resolver , id entity.Id ) (* Bug , error ) {
108
85
ref := bugsRefPattern + id .String ()
109
- return readBug (repo , ref )
86
+ return read (repo , identityResolver , ref )
87
+ }
88
+
89
+ // ReadRemote will read a remote bug from its hash
90
+ func ReadRemote (repo repository.ClockedRepo , remote string , id entity.Id ) (* Bug , error ) {
91
+ ref := fmt .Sprintf (bugsRemoteRefPattern , remote ) + id .String ()
92
+ return read (repo , identity .NewSimpleResolver (repo ), ref )
110
93
}
111
94
112
- // ReadRemoteBug will read a remote bug from its hash
113
- func ReadRemoteBug (repo repository.ClockedRepo , remote string , id string ) (* Bug , error ) {
114
- ref := fmt .Sprintf (bugsRemoteRefPattern , remote ) + id
115
- return readBug (repo , ref )
95
+ // ReadRemoteWithResolver will read a remote bug from its hash
96
+ func ReadRemoteWithResolver (repo repository.ClockedRepo , identityResolver identity. Resolver , remote string , id entity. Id ) (* Bug , error ) {
97
+ ref := fmt .Sprintf (bugsRemoteRefPattern , remote ) + id . String ()
98
+ return read (repo , identityResolver , ref )
116
99
}
117
100
118
- // readBug will read and parse a Bug from git
119
- func readBug (repo repository.ClockedRepo , ref string ) (* Bug , error ) {
101
+ // read will read and parse a Bug from git
102
+ func read (repo repository.ClockedRepo , identityResolver identity. Resolver , ref string ) (* Bug , error ) {
120
103
refSplit := strings .Split (ref , "/" )
121
104
id := entity .Id (refSplit [len (refSplit )- 1 ])
122
105
@@ -224,48 +207,103 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) {
224
207
err = json .Unmarshal (data , & opp )
225
208
226
209
if err != nil {
227
- return nil , errors . Wrap ( err , "failed to decode OperationPack json" )
210
+ return nil , err
228
211
}
229
212
230
213
// tag the pack with the commit hash
231
214
opp .commitHash = hash
232
215
233
- bug .packs = append (bug .packs , * opp )
216
+ bug .Packs = append (bug .Packs , * opp )
234
217
}
235
218
236
219
// Make sure that the identities are properly loaded
237
- resolver := identity .NewSimpleResolver (repo )
238
- err = bug .EnsureIdentities (resolver )
220
+ err = bug .EnsureIdentities (identityResolver )
239
221
if err != nil {
240
222
return nil , err
241
223
}
242
224
243
225
return & bug , nil
244
226
}
245
227
246
- func RemoveLocalBug (repo repository.ClockedRepo , id entity.Id ) error {
247
- ref := bugsRefPattern + id .String ()
248
- return repo .RemoveRef (ref )
228
+ // RemoveBug will remove a local bug from its entity.Id
229
+ func RemoveBug (repo repository.ClockedRepo , id entity.Id ) error {
230
+ var fullMatches []string
231
+
232
+ refs , err := repo .ListRefs (bugsRefPattern + id .String ())
233
+ if err != nil {
234
+ return err
235
+ }
236
+ if len (refs ) > 1 {
237
+ return NewErrMultipleMatchBug (refsToIds (refs ))
238
+ }
239
+ if len (refs ) == 1 {
240
+ // we have the bug locally
241
+ fullMatches = append (fullMatches , refs [0 ])
242
+ }
243
+
244
+ remotes , err := repo .GetRemotes ()
245
+ if err != nil {
246
+ return err
247
+ }
248
+
249
+ for remote := range remotes {
250
+ remotePrefix := fmt .Sprintf (bugsRemoteRefPattern + id .String (), remote )
251
+ remoteRefs , err := repo .ListRefs (remotePrefix )
252
+ if err != nil {
253
+ return err
254
+ }
255
+ if len (remoteRefs ) > 1 {
256
+ return NewErrMultipleMatchBug (refsToIds (refs ))
257
+ }
258
+ if len (remoteRefs ) == 1 {
259
+ // found the bug in a remote
260
+ fullMatches = append (fullMatches , remoteRefs [0 ])
261
+ }
262
+ }
263
+
264
+ if len (fullMatches ) == 0 {
265
+ return ErrBugNotExist
266
+ }
267
+
268
+ for _ , ref := range fullMatches {
269
+ err = repo .RemoveRef (ref )
270
+ if err != nil {
271
+ return err
272
+ }
273
+ }
274
+
275
+ return nil
249
276
}
250
277
251
278
type StreamedBug struct {
252
279
Bug * Bug
253
280
Err error
254
281
}
255
282
256
- // ReadAllLocalBugs read and parse all local bugs
257
- func ReadAllLocalBugs (repo repository.ClockedRepo ) <- chan StreamedBug {
258
- return readAllBugs (repo , bugsRefPattern )
283
+ // ReadAllLocal read and parse all local bugs
284
+ func ReadAllLocal (repo repository.ClockedRepo ) <- chan StreamedBug {
285
+ return readAll (repo , identity .NewSimpleResolver (repo ), bugsRefPattern )
286
+ }
287
+
288
+ // ReadAllLocalWithResolver read and parse all local bugs
289
+ func ReadAllLocalWithResolver (repo repository.ClockedRepo , identityResolver identity.Resolver ) <- chan StreamedBug {
290
+ return readAll (repo , identityResolver , bugsRefPattern )
291
+ }
292
+
293
+ // ReadAllRemote read and parse all remote bugs for a given remote
294
+ func ReadAllRemote (repo repository.ClockedRepo , remote string ) <- chan StreamedBug {
295
+ refPrefix := fmt .Sprintf (bugsRemoteRefPattern , remote )
296
+ return readAll (repo , identity .NewSimpleResolver (repo ), refPrefix )
259
297
}
260
298
261
- // ReadAllRemoteBugs read and parse all remote bugs for a given remote
262
- func ReadAllRemoteBugs (repo repository.ClockedRepo , remote string ) <- chan StreamedBug {
299
+ // ReadAllRemoteWithResolver read and parse all remote bugs for a given remote
300
+ func ReadAllRemoteWithResolver (repo repository.ClockedRepo , identityResolver identity. Resolver , remote string ) <- chan StreamedBug {
263
301
refPrefix := fmt .Sprintf (bugsRemoteRefPattern , remote )
264
- return readAllBugs (repo , refPrefix )
302
+ return readAll (repo , identityResolver , refPrefix )
265
303
}
266
304
267
305
// Read and parse all available bug with a given ref prefix
268
- func readAllBugs (repo repository.ClockedRepo , refPrefix string ) <- chan StreamedBug {
306
+ func readAll (repo repository.ClockedRepo , identityResolver identity. Resolver , refPrefix string ) <- chan StreamedBug {
269
307
out := make (chan StreamedBug )
270
308
271
309
go func () {
@@ -278,7 +316,7 @@ func readAllBugs(repo repository.ClockedRepo, refPrefix string) <-chan StreamedB
278
316
}
279
317
280
318
for _ , ref := range refs {
281
- b , err := readBug (repo , ref )
319
+ b , err := read (repo , identityResolver , ref )
282
320
283
321
if err != nil {
284
322
out <- StreamedBug {Err : err }
@@ -306,22 +344,26 @@ func refsToIds(refs []string) []entity.Id {
306
344
ids := make ([]entity.Id , len (refs ))
307
345
308
346
for i , ref := range refs {
309
- split := strings .Split (ref , "/" )
310
- ids [i ] = entity .Id (split [len (split )- 1 ])
347
+ ids [i ] = refToId (ref )
311
348
}
312
349
313
350
return ids
314
351
}
315
352
353
+ func refToId (ref string ) entity.Id {
354
+ split := strings .Split (ref , "/" )
355
+ return entity .Id (split [len (split )- 1 ])
356
+ }
357
+
316
358
// Validate check if the Bug data is valid
317
359
func (bug * Bug ) Validate () error {
318
360
// non-empty
319
- if len (bug .packs ) == 0 && bug .staging .IsEmpty () {
361
+ if len (bug .Packs ) == 0 && bug .staging .IsEmpty () {
320
362
return fmt .Errorf ("bug has no operations" )
321
363
}
322
364
323
365
// check if each pack and operations are valid
324
- for _ , pack := range bug .packs {
366
+ for _ , pack := range bug .Packs {
325
367
if err := pack .Validate (); err != nil {
326
368
return err
327
369
}
@@ -341,7 +383,7 @@ func (bug *Bug) Validate() error {
341
383
}
342
384
343
385
// The bug Id should be the hash of the first commit
344
- if len (bug .packs ) > 0 && string (bug .packs [0 ].commitHash ) != bug .id .String () {
386
+ if len (bug .Packs ) > 0 && string (bug .Packs [0 ].commitHash ) != bug .id .String () {
345
387
return fmt .Errorf ("bug id should be the first commit hash" )
346
388
}
347
389
@@ -372,7 +414,7 @@ func (bug *Bug) Append(op Operation) {
372
414
bug .staging .Append (op )
373
415
}
374
416
375
- // Commit write the staging area in Git and move the operations to the packs
417
+ // Commit write the staging area in Git and move the operations to the Packs
376
418
func (bug * Bug ) Commit (repo repository.ClockedRepo ) error {
377
419
378
420
if ! bug .NeedCommit () {
@@ -494,7 +536,7 @@ func (bug *Bug) Commit(repo repository.ClockedRepo) error {
494
536
}
495
537
496
538
bug .staging .commitHash = hash
497
- bug .packs = append (bug .packs , bug .staging )
539
+ bug .Packs = append (bug .Packs , bug .staging )
498
540
bug .staging = OperationPack {}
499
541
500
542
return nil
@@ -514,7 +556,7 @@ func (bug *Bug) NeedCommit() bool {
514
556
func makeMediaTree (pack OperationPack ) []repository.TreeEntry {
515
557
var tree []repository.TreeEntry
516
558
counter := 0
517
- added := make (map [git .Hash ]interface {})
559
+ added := make (map [repository .Hash ]interface {})
518
560
519
561
for _ , ops := range pack .Operations {
520
562
for _ , file := range ops .GetFiles () {
@@ -564,10 +606,10 @@ func (bug *Bug) Merge(repo repository.Repo, other Interface) (bool, error) {
564
606
}
565
607
566
608
ancestorIndex := 0
567
- newPacks := make ([]OperationPack , 0 , len (bug .packs ))
609
+ newPacks := make ([]OperationPack , 0 , len (bug .Packs ))
568
610
569
611
// Find the root of the rebase
570
- for i , pack := range bug .packs {
612
+ for i , pack := range bug .Packs {
571
613
newPacks = append (newPacks , pack )
572
614
573
615
if pack .commitHash == ancestor {
@@ -576,23 +618,23 @@ func (bug *Bug) Merge(repo repository.Repo, other Interface) (bool, error) {
576
618
}
577
619
}
578
620
579
- if len (otherBug .packs ) == ancestorIndex + 1 {
621
+ if len (otherBug .Packs ) == ancestorIndex + 1 {
580
622
// Nothing to rebase, return early
581
623
return false , nil
582
624
}
583
625
584
- // get other bug's extra packs
585
- for i := ancestorIndex + 1 ; i < len (otherBug .packs ); i ++ {
626
+ // get other bug's extra Packs
627
+ for i := ancestorIndex + 1 ; i < len (otherBug .Packs ); i ++ {
586
628
// clone is probably not necessary
587
- newPack := otherBug .packs [i ].Clone ()
629
+ newPack := otherBug .Packs [i ].Clone ()
588
630
589
631
newPacks = append (newPacks , newPack )
590
632
bug .lastCommit = newPack .commitHash
591
633
}
592
634
593
- // rebase our extra packs
594
- for i := ancestorIndex + 1 ; i < len (bug .packs ); i ++ {
595
- pack := bug .packs [i ]
635
+ // rebase our extra Packs
636
+ for i := ancestorIndex + 1 ; i < len (bug .Packs ); i ++ {
637
+ pack := bug .Packs [i ]
596
638
597
639
// get the referenced git tree
598
640
treeHash , err := repo .GetTreeHash (pack .commitHash )
@@ -617,7 +659,7 @@ func (bug *Bug) Merge(repo repository.Repo, other Interface) (bool, error) {
617
659
bug .lastCommit = hash
618
660
}
619
661
620
- bug .packs = newPacks
662
+ bug .Packs = newPacks
621
663
622
664
// Update the git ref
623
665
err = repo .UpdateRef (bugsRefPattern + bug .id .String (), bug .lastCommit )
@@ -651,7 +693,7 @@ func (bug *Bug) EditLamportTime() lamport.Time {
651
693
// Lookup for the very first operation of the bug.
652
694
// For a valid Bug, this operation should be a CreateOp
653
695
func (bug * Bug ) FirstOp () Operation {
654
- for _ , pack := range bug .packs {
696
+ for _ , pack := range bug .Packs {
655
697
for _ , op := range pack .Operations {
656
698
return op
657
699
}
@@ -671,11 +713,11 @@ func (bug *Bug) LastOp() Operation {
671
713
return bug .staging .Operations [len (bug .staging .Operations )- 1 ]
672
714
}
673
715
674
- if len (bug .packs ) == 0 {
716
+ if len (bug .Packs ) == 0 {
675
717
return nil
676
718
}
677
719
678
- lastPack := bug .packs [len (bug .packs )- 1 ]
720
+ lastPack := bug .Packs [len (bug .Packs )- 1 ]
679
721
680
722
if len (lastPack .Operations ) == 0 {
681
723
return nil
0 commit comments