@@ -4,36 +4,110 @@ import (
44 "fmt"
55 "os"
66
7- "github.com/MichaelMure/git-bug-migration/migration1/bug"
8- "github.com/MichaelMure/git-bug-migration/migration1/repository"
7+ mg1b "github.com/MichaelMure/git-bug-migration/migration1/bug"
8+ mg1i "github.com/MichaelMure/git-bug-migration/migration1/identity"
9+ mg1r "github.com/MichaelMure/git-bug-migration/migration1/repository"
910)
1011
1112const rootCommandName = "git-bug-migration"
1213
13- var repo repository .ClockedRepo
14+ var repo mg1r .ClockedRepo
1415
1516func main () {
1617 cwd , err := os .Getwd ()
1718 if err != nil {
1819 panic (fmt .Errorf ("unable to get the current working directory: %q" , err ))
1920 }
2021
21- repo , err = repository .NewGitRepo (cwd , []repository .ClockLoader {bug .ClockLoader })
22- if err == repository .ErrNotARepo {
22+ repo , err = mg1r .NewGitRepo (cwd , []mg1r .ClockLoader {mg1b .ClockLoader })
23+ if err == mg1r .ErrNotARepo {
2324 panic (fmt .Errorf ("%s must be run from within a git repo" , rootCommandName ))
2425 }
2526
27+ identities := []* mg1i.Identity {}
28+ for streamedIdentity := range mg1i .ReadAllLocalIdentities (repo ) {
29+ if streamedIdentity .Err != nil {
30+ fmt .Print (fmt .Errorf ("Got error when reading identity: %q\n " , err ))
31+ continue
32+ }
33+ identities = append (identities , streamedIdentity .Identity )
34+ }
35+
2636 if err != nil {
2737 panic (err )
2838 }
2939
30- for streamedBug := range bug .ReadAllLocalBugs (repo ) {
40+ // Iterating through all the bugs in the repo
41+ for streamedBug := range mg1b .ReadAllLocalBugs (repo ) {
3142 if streamedBug .Err != nil {
32- fmt .Print (fmt .Errorf ("Got error when reading bug: %q" , err ))
43+ fmt .Print (fmt .Errorf ("Got error when reading bug: %q\n " , err ))
3344 continue
3445 }
3546
36- //switch streamedBug.Bug
37- fmt .Print (streamedBug .Bug .FirstOp ().GetAuthor ())
47+ // Getting the old bug
48+ oldBug := streamedBug .Bug
49+
50+ b := mg1b .NewBug ()
51+ bugChange := false
52+
53+ // Iterating over each operation in the bug
54+ it := mg1b .NewOperationIterator (oldBug )
55+ for it .Next () {
56+ operation := it .Value ()
57+ oldAuthor := operation .GetAuthor ()
58+
59+ // Checking if the author is of the legacy (bare) type
60+ switch oldAuthor .(type ) {
61+ case * mg1i.Bare :
62+ fmt .Print ("Detected legacyAuthor!\n " )
63+
64+ bugChange = true
65+
66+ // Search existing identities for any traces of this old identity
67+ var newAuthor * mg1i.Identity = nil
68+ for _ , identity := range identities {
69+ if oldAuthor .Name () == identity .Name () {
70+ newAuthor = identity
71+ }
72+ }
73+
74+ // If no existing identity is found, create a new one
75+ if newAuthor == nil {
76+ newAuthor = mg1i .NewIdentityFull (
77+ oldAuthor .Name (),
78+ oldAuthor .Email (),
79+ oldAuthor .Login (),
80+ oldAuthor .AvatarUrl (),
81+ )
82+ }
83+
84+ // Set the author of the operation to the new identity
85+ operation .SetAuthor (newAuthor )
86+ b .Append (operation )
87+ continue
88+
89+ // If the author's identity is a new identity type, its fine. Just append it to the cache
90+ case * mg1i.Identity :
91+ b .Append (operation )
92+ continue
93+
94+ // This should not be reached
95+ default :
96+ fmt .Printf ("Unknown author type: %T\n " , operation .GetAuthor ())
97+ }
98+ }
99+
100+ // If the bug has been changed, remove the old bug and commit the new one
101+ if bugChange {
102+ err = b .Commit (repo )
103+ if err != nil {
104+ fmt .Printf ("Got error when attempting to commit new bug: %q\n " , err )
105+ }
106+
107+ err = mg1b .RemoveLocalBug (repo , oldBug .Id ())
108+ if err != nil {
109+ fmt .Printf ("Got error when attempting to remove bug: %q\n " , err )
110+ }
111+ }
38112 }
39113}
0 commit comments