|
| 1 | +package migration1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + mg1b "github.com/MichaelMure/git-bug-migration/migration1/bug" |
| 7 | + mg1i "github.com/MichaelMure/git-bug-migration/migration1/identity" |
| 8 | + mg1r "github.com/MichaelMure/git-bug-migration/migration1/repository" |
| 9 | +) |
| 10 | + |
| 11 | +var identities1 []*mg1i.Identity |
| 12 | + |
| 13 | +func readIdentities(repo mg1r.ClockedRepo) { |
| 14 | + for streamedIdentity := range mg1i.ReadAllLocalIdentities(repo) { |
| 15 | + if streamedIdentity.Err != nil { |
| 16 | + fmt.Printf("Got error when reading identity: %q", streamedIdentity.Err) |
| 17 | + continue |
| 18 | + } |
| 19 | + identities1 = append(identities1, streamedIdentity.Identity) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func Migrate01(repo mg1r.ClockedRepo) error { |
| 24 | + readIdentities(repo) |
| 25 | + |
| 26 | + // Iterating through all the bugs in the repo |
| 27 | + for streamedBug := range mg1b.ReadAllLocalBugs(repo) { |
| 28 | + if streamedBug.Err != nil { |
| 29 | + fmt.Printf("Got error when reading bug: %q\n", streamedBug.Err) |
| 30 | + continue |
| 31 | + } |
| 32 | + |
| 33 | + oldBug := streamedBug.Bug |
| 34 | + newBug, changed, err := migrateBug01(oldBug) |
| 35 | + if err != nil { |
| 36 | + fmt.Printf("Got error when parsing bug: %q", err) |
| 37 | + } |
| 38 | + |
| 39 | + // If the bug has been changed, remove the old bug and commit the new one |
| 40 | + if changed { |
| 41 | + err = newBug.Commit(repo) |
| 42 | + if err != nil { |
| 43 | + fmt.Printf("Got error when attempting to commit new bug: %q\n", err) |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + err = mg1b.RemoveLocalBug(repo, oldBug.Id()) |
| 48 | + if err != nil { |
| 49 | + fmt.Printf("Got error when attempting to remove bug: %q\n", err) |
| 50 | + continue |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func migrateBug01(oldBug *mg1b.Bug) (*mg1b.Bug, bool, error) { |
| 59 | + // Making a new bug |
| 60 | + newBug := mg1b.NewBug() |
| 61 | + bugChange := false |
| 62 | + |
| 63 | + // Iterating over each operation in the bug |
| 64 | + it := mg1b.NewOperationIterator(oldBug) |
| 65 | + for it.Next() { |
| 66 | + operation := it.Value() |
| 67 | + oldAuthor := operation.GetAuthor() |
| 68 | + |
| 69 | + // Checking if the author is of the legacy (bare) type |
| 70 | + switch oldAuthor.(type) { |
| 71 | + case *mg1i.Bare: |
| 72 | + bugChange = true |
| 73 | + |
| 74 | + // Search existing identities for any traces of this old identity |
| 75 | + var newAuthor *mg1i.Identity = nil |
| 76 | + for _, identity := range identities1 { |
| 77 | + if oldAuthor.Name() == identity.Name() { |
| 78 | + newAuthor = identity |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // If no existing identity is found, create a new one |
| 83 | + if newAuthor == nil { |
| 84 | + newAuthor = mg1i.NewIdentityFull( |
| 85 | + oldAuthor.Name(), |
| 86 | + oldAuthor.Email(), |
| 87 | + oldAuthor.Login(), |
| 88 | + oldAuthor.AvatarUrl(), |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + // Set the author of the operation to the new identity |
| 93 | + operation.SetAuthor(newAuthor) |
| 94 | + newBug.Append(operation) |
| 95 | + continue |
| 96 | + |
| 97 | + // If the author's identity is a new identity type, its fine. Just append it to the cache |
| 98 | + case *mg1i.Identity: |
| 99 | + newBug.Append(operation) |
| 100 | + continue |
| 101 | + |
| 102 | + // This should not be reached |
| 103 | + default: |
| 104 | + return newBug, false, fmt.Errorf("Unknown author type: %T\n", operation.GetAuthor()) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return newBug, bugChange, nil |
| 109 | +} |
0 commit comments