Skip to content
This repository was archived by the owner on Aug 24, 2024. It is now read-only.

Commit b46c794

Browse files
committed
Migrate legacyAuthor
This commit enables users to run the main file in the directory of a git repository with git-bug and migrates any legacyAuthors found to the new identity.Identity type. This allows for users to update to the latest version of git-bug, bridging over the breaking changes.
1 parent 19464cc commit b46c794

File tree

9 files changed

+105
-579
lines changed

9 files changed

+105
-579
lines changed

main.go

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1112
const rootCommandName = "git-bug-migration"
1213

13-
var repo repository.ClockedRepo
14+
var repo mg1r.ClockedRepo
1415

1516
func 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
}

migration1/bug/bug.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) {
243243
return &bug, nil
244244
}
245245

246+
func RemoveLocalBug(repo repository.ClockedRepo, id entity.Id) error {
247+
ref := bugsRefPattern + id.String()
248+
return repo.RemoveRef(ref)
249+
}
250+
246251
type StreamedBug struct {
247252
Bug *Bug
248253
Err error

migration1/bug/operation.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type Operation interface {
5050
AllMetadata() map[string]string
5151
// GetAuthor return the author identity
5252
GetAuthor() identity.Interface
53+
// GetAuthor sets the author identity
54+
SetAuthor(identity.Interface)
5355

5456
// sign-post method for gqlgen
5557
IsOperation()
@@ -217,3 +219,8 @@ func (op *OpBase) AllMetadata() map[string]string {
217219
func (op *OpBase) GetAuthor() identity.Interface {
218220
return op.Author
219221
}
222+
223+
// SetAuthor return author identity
224+
func (op *OpBase) SetAuthor(author identity.Interface) {
225+
op.Author = author
226+
}

migration1/repository/config_testing.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

migration1/repository/git.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ func (repo *GitRepo) UpdateRef(ref string, hash git.Hash) error {
283283
return err
284284
}
285285

286+
// RemoveRef will remove a Git reference
287+
func (repo *GitRepo) RemoveRef(ref string) error {
288+
_, err := repo.runGitCommand("update-ref", "-d", ref)
289+
290+
return err
291+
}
292+
286293
// ListRefs will return a list of Git ref matching the given refspec
287294
func (repo *GitRepo) ListRefs(refspec string) ([]string, error) {
288295
stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname)", refspec)

migration1/repository/git_testing.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)