Skip to content

Commit 30fc7bd

Browse files
committed
sync: refactor sync process
Previously, the sync process would attempt to reverse the changes to be applied to the local directory and diff it against the cloned directory. If they were the same, the local directory would be reverted using git. If they were different, the cloned directory would have the opposite changes applied and then be copied to the local directory. This relied on the local directory already being in git and would cause errors when it was not tracked in git. This PR changes this process to apply the desired updates to the cloned directory, diff it against the local, and overwrite the local if there is a difference. This does not rely on git to revert the changes and should halve the number of changes to files.
1 parent 8eb6220 commit 30fc7bd

File tree

1 file changed

+25
-101
lines changed

1 file changed

+25
-101
lines changed

internal/sync/sync.go

Lines changed: 25 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ func syncRemoteRepo(repo *repo) {
5858
projectLocalDirectory := path.Join("./pkg", repo.LocalAPIDirectory)
5959

6060
gitClone(basePath, repo)
61+
excludeAndRefactor(projectClonedDirectory, projectLocalDirectory, repo)
6162

6263
glog.V(100).Infof("Comparing local %s and cloned %s api directories for repo %s",
6364
projectLocalDirectory, projectClonedDirectory, repo.Name)
6465

65-
if repoSynced(projectClonedDirectory, projectLocalDirectory, repo) {
66-
gitReset(repo.LocalAPIDirectory)
67-
} else {
68-
syncDirectories(projectClonedDirectory, projectLocalDirectory, repo)
66+
err := execCmd("", "diff", []string{projectClonedDirectory, projectLocalDirectory})
67+
if err != nil {
68+
glog.V(100).Infof("Repos not synced. Copying cloned repo %s to %s", projectClonedDirectory, projectLocalDirectory)
69+
70+
copyClonedToLocal(projectClonedDirectory, projectLocalDirectory)
6971
}
7072

7173
glog.V(100).Infof("Remove cloned directory from filesystem: %s", path.Join(basePath, repo.Name))
@@ -81,21 +83,13 @@ func syncRemoteRepo(repo *repo) {
8183
}
8284
}
8385

84-
func repoSynced(clonedDir, localDir string, repo *repo) bool {
85-
glog.V(100).Infof("Verifying destination directory %s exists", localDir)
86-
87-
if _, err := os.Stat(localDir); os.IsNotExist(err) {
88-
glog.V(100).Infof("Destination api directory %s doesn't exist creating directory", localDir)
89-
90-
if os.MkdirAll(localDir, 0777) != nil {
91-
glog.V(100).Infof("Failed to create api directory. Exit with error code 1")
92-
os.Exit(1)
93-
}
94-
}
86+
// excludeAndRefactor excludes and refactors files in the clonedDir to prepare them for being compared or copied to the
87+
// localDir.
88+
func excludeAndRefactor(clonedDir, localDir string, repo *repo) {
89+
glog.V(100).Infof("Updating %s to match expected state of %s", clonedDir, localDir)
9590

9691
if len(repo.Excludes) > 0 {
97-
glog.V(100).Infof("Remove excluded files under %s",
98-
path.Base(clonedDir))
92+
glog.V(100).Infof("Remove excluded files under %s", path.Base(clonedDir))
9993

10094
err := excludeFiles(clonedDir, repo.Excludes...)
10195
if err != nil {
@@ -108,115 +102,45 @@ func repoSynced(clonedDir, localDir string, repo *repo) bool {
108102
path.Base(clonedDir), path.Base(localDir))
109103

110104
err := refactor(
111-
fmt.Sprintf("package %s", path.Base(localDir)),
112105
fmt.Sprintf("package %s", path.Base(clonedDir)),
113-
fmt.Sprintf("./%s", localDir), "*.go")
106+
fmt.Sprintf("package %s", path.Base(localDir)),
107+
clonedDir, "*.go")
114108

115109
if err != nil {
116-
glog.V(100).Infof("Failed to refactor file before sync due to %w. Exit with error 1", err)
110+
glog.V(100).Infof("Failed to replace package names due to %w. Exit with error 1", err)
117111
os.Exit(1)
118112
}
119113

120-
glog.V(100).Infof("Replace cloned package imports")
121-
122114
for _, importMap := range repo.ReplaceImports {
123-
err = refactor(
124-
importMap["new"],
125-
importMap["old"],
126-
localDir,
127-
"*.go")
128-
115+
err = refactor(importMap["old"], importMap["new"], clonedDir, "*.go")
129116
if err != nil {
130-
glog.V(100).Infof("Failed to refactor file. Exit with error 1")
117+
glog.V(100).Infof("Failed to refactor files due to %w. Exit with error 1", err)
131118
os.Exit(1)
132119
}
133120
}
134-
135-
err = execCmd("", "diff", []string{clonedDir, localDir})
136-
137-
if err == nil {
138-
glog.V(100).Infof("Repo synced. Revert local files to original state")
139-
140-
return true
141-
}
142-
143-
return false
144121
}
145122

146-
func syncDirectories(clonedDir, localDir string, repo *repo) {
147-
glog.V(100).Infof("Repos are not synced. Cleaning local directory: %s", localDir)
148-
149-
if os.RemoveAll(localDir) != nil {
150-
glog.V(100).Infof("Failed to remove local api directory. Exit with error code 1")
151-
os.Exit(1)
152-
}
153-
154-
glog.V(100).Infof("Create new local directory: %s", localDir)
123+
func copyClonedToLocal(clonedDir, localDir string) {
124+
glog.V(100).Infof("Create path to new local directory: %s", localDir)
155125

126+
// We use MkdirAll to make sure the path leading up to localDir exists.
156127
if os.MkdirAll(localDir, 0750) != nil {
157-
glog.V(100).Infof("Failed to recreate api directory. Exit with error code 1")
128+
glog.V(100).Info("Failed to create local directory. Exit with error code 1")
158129
os.Exit(1)
159130
}
160131

161-
if len(repo.Excludes) > 0 {
162-
glog.V(100).Infof("Remove excluded files under %s",
163-
path.Base(clonedDir))
164-
165-
err := excludeFiles(clonedDir, repo.Excludes...)
166-
if err != nil {
167-
glog.V(100).Infof("Failed to remove excluded files due to %w. Exit with error 1", err)
168-
os.Exit(1)
169-
}
170-
}
171-
172-
glog.V(100).Infof("Copy api filed from cloned directory to local api directory")
173-
174-
err := execCmd(
175-
"",
176-
"cp",
177-
[]string{"-a", fmt.Sprintf("%s/.", clonedDir), fmt.Sprintf("%s/", localDir)})
178-
132+
// We use RemoveAll to delete just localDir but not the path leading to it.
133+
err := os.RemoveAll(localDir)
179134
if err != nil {
180-
glog.Infof("Failed to sync directories. Exit with error code 1")
135+
glog.V(100).Infof("Failed to remove old local directory %s due to %w. Exit with error 1", localDir, err)
181136
os.Exit(1)
182137
}
183138

184-
glog.V(100).Infof("Fix packages names")
185-
186-
err = refactor(
187-
fmt.Sprintf("package %s", path.Base(clonedDir)),
188-
fmt.Sprintf("package %s", path.Base(localDir)),
189-
localDir,
190-
"*.go")
191-
139+
err = execCmd("", "cp", []string{"-a", clonedDir, localDir})
192140
if err != nil {
193-
glog.V(100).Infof("Failed to refactor file. Exit with error 1")
141+
glog.V(100).Infof("Failed to sync directories due to %w. Exit with error 1", err)
194142
os.Exit(1)
195143
}
196-
197-
for _, importMap := range repo.ReplaceImports {
198-
err = refactor(importMap["old"], importMap["new"], localDir, "*.go")
199-
200-
if err != nil {
201-
glog.V(100).Infof("Failed to refactor file. Exit with error 1")
202-
os.Exit(1)
203-
}
204-
}
205-
}
206-
207-
func gitReset(packageName string) {
208-
for _, cmdToRun := range [][]string{
209-
{"reset", "--", fmt.Sprintf("./pkg/%s", packageName)},
210-
{"checkout", "--", fmt.Sprintf("./pkg/%s", packageName)},
211-
{"clean", "-d", "-f", fmt.Sprintf("./pkg/%s", packageName)},
212-
} {
213-
err := execCmd("", "git", cmdToRun)
214-
215-
if err != nil {
216-
glog.Infof("Failed to reset project to it's original state. Exit with error 1")
217-
os.Exit(1)
218-
}
219-
}
220144
}
221145

222146
func gitClone(localPath string, repo *repo) {

0 commit comments

Comments
 (0)