forked from siyuan-note/dejavu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref.go
100 lines (87 loc) · 2.43 KB
/
ref.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// DejaVu - Data snapshot and sync.
// Copyright (c) 2022-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package dejavu
import (
"errors"
"os"
"path/filepath"
"github.com/88250/gulu"
"github.com/siyuan-note/dejavu/entity"
"github.com/siyuan-note/filelock"
)
var ErrNotFoundIndex = errors.New("not found index")
func (repo *Repo) Latest() (ret *entity.Index, err error) {
latest := filepath.Join(repo.Path, "refs", "latest")
if !gulu.File.IsExist(latest) {
err = ErrNotFoundIndex
return
}
data, err := filelock.ReadFile(latest)
if nil != err {
return
}
hash := string(data)
ret, err = repo.store.GetIndex(hash)
return
}
func (repo *Repo) UpdateLatest(id string) (err error) {
refs := filepath.Join(repo.Path, "refs")
err = os.MkdirAll(refs, 0755)
if nil != err {
return
}
err = gulu.File.WriteFileSafer(filepath.Join(refs, "latest"), []byte(id), 0644)
return
}
func (repo *Repo) GetTag(tag string) (id string, err error) {
if !gulu.File.IsValidFilename(tag) {
err = errors.New("invalid tag name")
}
tag = filepath.Join(repo.Path, "refs", "tags", tag)
if !gulu.File.IsExist(tag) {
err = errors.New("tag not found")
}
data, err := filelock.ReadFile(tag)
if nil != err {
return
}
id = string(data)
return
}
func (repo *Repo) AddTag(id, tag string) (err error) {
if !gulu.File.IsValidFilename(tag) {
return errors.New("invalid tag name")
}
_, err = repo.store.GetIndex(id)
if nil != err {
return
}
tags := filepath.Join(repo.Path, "refs", "tags")
if err = os.MkdirAll(tags, 0755); nil != err {
return
}
tag = filepath.Join(tags, tag)
err = gulu.File.WriteFileSafer(tag, []byte(id), 0644)
return
}
func (repo *Repo) RemoveTag(tag string) (err error) {
tag = filepath.Join(repo.Path, "refs", "tags", tag)
if !gulu.File.IsExist(tag) {
return
}
err = os.Remove(tag)
return
}