-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepoclone.go
166 lines (142 loc) · 4.66 KB
/
repoclone.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// This file is part of libraries-repository-engine.
//
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
//
// 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/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package libraries
import (
"os"
"path/filepath"
"github.com/arduino/go-paths-helper"
"github.com/arduino/libraries-repository-engine/internal/backup"
"github.com/arduino/libraries-repository-engine/internal/configuration"
"github.com/arduino/libraries-repository-engine/internal/feedback"
"github.com/arduino/libraries-repository-engine/internal/libraries/db"
"fmt"
"github.com/arduino/libraries-repository-engine/internal/libraries/metadata"
"github.com/go-git/go-git/v5"
)
// Repository represents a Git repository located on the filesystem.
type Repository struct {
Repository *git.Repository
FolderPath string
URL string
}
// CloneOrFetch returns a Repository object. If the repository is already present, it is opened. Otherwise, cloned.
func CloneOrFetch(repoMeta *Repo, folderName string) (*Repository, error) {
repo := Repository{
FolderPath: folderName,
URL: repoMeta.URL,
}
if _, err := os.Stat(folderName); os.IsNotExist(err) {
repo.Repository, err = git.PlainClone(folderName, false, &git.CloneOptions{URL: repoMeta.URL})
if err != nil {
return nil, err
}
} else {
repo.Repository, err = git.PlainOpen(folderName)
if err != nil {
return nil, err
}
}
tags, err := repo.Repository.Tags()
if err != nil {
return nil, err
}
for {
tag, err := tags.Next()
if err != nil {
// Reached end of tags
break
}
if err = repo.Repository.DeleteTag(tag.Name().Short()); err != nil {
return nil, err
}
}
if err = repo.Repository.Fetch(&git.FetchOptions{Tags: git.AllTags}); err != nil {
return nil, err
}
return &repo, err
}
// GenerateLibraryFromRepo parses a repository and returns the library metadata.
func GenerateLibraryFromRepo(repo *Repository) (*metadata.LibraryMetadata, error) {
bytes, err := os.ReadFile(filepath.Join(repo.FolderPath, "library.properties"))
if err != nil {
return nil, fmt.Errorf("can't read library.properties: %s", err)
}
library, err := metadata.Parse(bytes)
if err != nil {
return nil, err
}
return library, nil
}
// UpdateLibrary adds a release to the library database.
func UpdateLibrary(release *db.Release, repoURL string, libraryDb *db.DB) error {
var err error
if !libraryDb.HasLibrary(release.LibraryName) {
err = libraryDb.AddLibrary(&db.Library{
Name: release.LibraryName,
Repository: repoURL})
if err != nil {
return err
}
err = libraryDb.Commit()
if err != nil {
return err
}
}
if libraryDb.HasRelease(release) {
return nil
}
err = libraryDb.AddRelease(release, repoURL)
if err != nil {
return err
}
err = libraryDb.Commit()
if err != nil {
return err
}
return nil
}
// BackupAndDeleteGitClone backs up and then deletes the library's Git clone folder.
func BackupAndDeleteGitClone(config *configuration.Config, repoMeta *Repo) error {
gitCloneSubfolder, err := repoMeta.AsFolder()
if err != nil {
return err
}
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
// The library's clone folder may be removed by the sync process under expected circumstances.
// So its absence does not necessarily imply a problem.
gitClonePathExists, err := gitClonePath.ExistCheck()
if err != nil {
return err
}
if gitClonePathExists {
if err := backup.Backup(gitClonePath); err != nil {
return fmt.Errorf("backing up library's Git clone: %w", err)
}
if err := gitClonePath.RemoveAll(); err != nil {
return fmt.Errorf("removing library Git clone: %s", err)
}
} else {
feedback.Warningf("Library Git clone folder %s not present", gitClonePath)
}
return nil
}