seafile_recovery/commit.go

75 lines
1.9 KiB
Go
Raw Normal View History

2021-04-21 12:44:49 +00:00
package main
import (
"log"
2021-04-21 13:54:50 +00:00
"io/ioutil"
"path/filepath"
2021-04-21 12:44:49 +00:00
)
2021-04-21 13:54:50 +00:00
type RepoCommits struct {
Config configCollect
CommitDesc map[string]string
CommitContent map[string]Commit
}
2021-04-21 12:44:49 +00:00
type Commit struct {
CommitId string `json:"commit_id"`
RootId string `json:"root_id"`
RepoId string `json:"repo_id"`
CreatorName string `json:"creator_name"`
Creator string `json:"creator"`
Description string `json:"description"`
Ctime uint64 `json:"ctime"`
ParentId string `json:"parent_id"`
SecondParentId string `json:"second_parent_id"`
RepoName string `json:"repo_name"`
RepoDesc string `json:"repo_desc"`
RepoCategory string `json:"repo_category"`
NoLocalHistory int `json:"no_local_history"`
Version int `json:"version"`
}
func cmdCommit(config configCollect) {
2021-04-21 13:54:50 +00:00
rc := NewRepoCommits(config)
rc.CollectDescs()
rc.PrintDescs()
}
func NewRepoCommits (config configCollect) *RepoCommits {
rc := new(RepoCommits)
rc.Config = config
rc.CommitDesc = make(map[string]string)
rc.CommitContent = make(map[string]Commit)
return rc
}
func (rc* RepoCommits) CollectDescs() {
baseFolder := filepath.Join(rc.Config.Storage, "commits", rc.Config.RepoId)
log.Println(baseFolder)
layer1, err := ioutil.ReadDir(baseFolder)
if err != nil { log.Fatal(err) }
for _, l1 := range layer1 {
intFolder := filepath.Join(baseFolder, l1.Name())
layer2, err := ioutil.ReadDir(intFolder)
if err != nil { log.Fatal(err) }
for _, l2 := range layer2 {
commitId := l1.Name() + l2.Name()
commitPath := filepath.Join(intFolder, l2.Name())
rc.CommitDesc[commitId] = commitPath
}
}
}
func (rc* RepoCommits) PrintDescs() {
limit := 10
for id, path := range rc.CommitDesc {
log.Println(id, path)
limit = limit - 1
if limit < 0 {
log.Println("Too many commits, output has been truncated")
break
}
}
log.Println("Repo", rc.Config.RepoId, "contains", len(rc.CommitDesc), "commits")
2021-04-21 12:44:49 +00:00
}