-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor,fix: add rename lfs objects migration
Rename lfs objects from OID[:2]/OID[2:4]/OID[4:] to OID[:2]/OID[2:4]/OID to follow specs https://github.com/git-lfs/git-lfs/blob/main/docs/spec.md#intercepting-git
- Loading branch information
1 parent
90e0c74
commit 3920cfe
Showing
8 changed files
with
124 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package migrate | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/charmbracelet/log" | ||
"github.com/charmbracelet/soft-serve/pkg/config" | ||
"github.com/charmbracelet/soft-serve/pkg/db" | ||
"github.com/charmbracelet/soft-serve/pkg/db/models" | ||
) | ||
|
||
const ( | ||
migrateLfsObjectsName = "migrate_lfs_objects" | ||
migrateLfsObjectsVersion = 3 | ||
) | ||
|
||
// Correct LFS objects relative path. | ||
// From OID[:2]/OID[2:4]/OID[4:] to OID[:2]/OID[2:4]/OID | ||
// See: https://github.com/git-lfs/git-lfs/blob/main/docs/spec.md#intercepting-git | ||
var migrateLfsObjects = Migration{ | ||
Name: migrateLfsObjectsName, | ||
Version: migrateLfsObjectsVersion, | ||
Migrate: func(ctx context.Context, tx *db.Tx) error { | ||
cfg := config.FromContext(ctx) | ||
logger := log.FromContext(ctx).WithPrefix("migrate_lfs_objects") | ||
|
||
var repoIds []int64 | ||
if err := tx.Select(&repoIds, "SELECT id FROM repos"); err != nil { | ||
return err | ||
Check failure on line 32 in pkg/db/migrate/0003_migrate_lfs_objects.go GitHub Actions / lint-soft
|
||
} | ||
for _, r := range repoIds { | ||
var objs []models.LFSObject | ||
if err := tx.Select(&objs, "SELECT * FROM lfs_objects WHERE repo_id = ?", r); err != nil { | ||
return err | ||
Check failure on line 37 in pkg/db/migrate/0003_migrate_lfs_objects.go GitHub Actions / lint-soft
|
||
} | ||
objsp := filepath.Join(cfg.DataPath, "lfs", strconv.FormatInt(r, 10), "objects") | ||
for _, obj := range objs { | ||
oldpath := filepath.Join(objsp, badRelativePath(obj.Oid)) | ||
newpath := filepath.Join(objsp, goodRelativePath(obj.Oid)) | ||
if _, err := os.Stat(oldpath); err == nil { | ||
if err := os.Rename(oldpath, newpath); err != nil { | ||
logger.Error("rename lfs object", "oldpath", oldpath, "newpath", newpath, "err", err) | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
}, | ||
Rollback: func(ctx context.Context, tx *db.Tx) error { | ||
return nil | ||
}, | ||
} | ||
|
||
func goodRelativePath(oid string) string { | ||
if len(oid) < 5 { | ||
return oid | ||
} | ||
return filepath.Join(oid[:2], oid[2:4], oid) | ||
} | ||
|
||
func badRelativePath(oid string) string { | ||
if len(oid) < 5 { | ||
return oid | ||
} | ||
return filepath.Join(oid[:2], oid[2:4], oid[4:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package git | ||
|
||
import ( | ||
"github.com/charmbracelet/git-lfs-transfer/transfer" | ||
"github.com/charmbracelet/log" | ||
) | ||
|
||
type lfsLogger struct { | ||
l *log.Logger | ||
} | ||
|
||
var _ transfer.Logger = &lfsLogger{} | ||
|
||
// Log implements transfer.Logger. | ||
func (l *lfsLogger) Log(msg string, kv ...interface{}) { | ||
l.l.Debug(msg, kv...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters