@@ -11,35 +11,13 @@ import (
1111 "code.gitea.io/gitea/modules/util"
1212)
1313
14- // Copy copies file from source to target path.
15- func Copy (src , dest string ) error {
16- // Gather file information to set back later.
17- si , err := os .Lstat (src )
18- if err != nil {
19- return err
20- }
21-
22- // Handle symbolic link.
23- if si .Mode ()& os .ModeSymlink != 0 {
24- target , err := os .Readlink (src )
25- if err != nil {
26- return err
27- }
28- // NOTE: os.Chmod and os.Chtimes don't recognize symbolic link,
29- // which will lead "no such file or directory" error.
30- return os .Symlink (target , dest )
31- }
32-
33- return util .CopyFile (src , dest )
34- }
35-
36- // Sync synchronizes the two files. This is skipped if both files
14+ // SyncFile synchronizes the two files. This is skipped if both files
3715// exist and the size, modtime, and mode match.
38- func Sync (srcPath , destPath string ) error {
16+ func SyncFile (srcPath , destPath string ) error {
3917 dest , err := os .Stat (destPath )
4018 if err != nil {
4119 if os .IsNotExist (err ) {
42- return Copy (srcPath , destPath )
20+ return util . CopyFile (srcPath , destPath )
4321 }
4422 return err
4523 }
@@ -55,7 +33,7 @@ func Sync(srcPath, destPath string) error {
5533 return nil
5634 }
5735
58- return Copy (srcPath , destPath )
36+ return util . CopyFile (srcPath , destPath )
5937}
6038
6139// SyncDirs synchronizes files recursively from source to target directory.
@@ -66,23 +44,31 @@ func SyncDirs(srcPath, destPath string) error {
6644 return err
6745 }
6846
47+ // the keep file is used to keep the directory in a git repository, it doesn't need to be synced
48+ // and go-git doesn't work with the ".keep" file (it would report errors like "ref is empty")
49+ const keepFile = ".keep"
50+
6951 // find and delete all untracked files
7052 destFiles , err := util .ListDirRecursively (destPath , & util.ListDirOptions {IncludeDir : true })
7153 if err != nil {
7254 return err
7355 }
7456 for _ , destFile := range destFiles {
7557 destFilePath := filepath .Join (destPath , destFile )
58+ shouldRemove := filepath .Base (destFilePath ) == keepFile
7659 if _ , err = os .Stat (filepath .Join (srcPath , destFile )); err != nil {
7760 if os .IsNotExist (err ) {
78- // if src file does not exist, remove dest file
79- if err = os .RemoveAll (destFilePath ); err != nil {
80- return err
81- }
61+ shouldRemove = true
8262 } else {
8363 return err
8464 }
8565 }
66+ // if src file does not exist, remove dest file
67+ if shouldRemove {
68+ if err = os .RemoveAll (destFilePath ); err != nil {
69+ return err
70+ }
71+ }
8672 }
8773
8874 // sync src files to dest
@@ -95,8 +81,8 @@ func SyncDirs(srcPath, destPath string) error {
9581 // util.ListDirRecursively appends a slash to the directory name
9682 if strings .HasSuffix (srcFile , "/" ) {
9783 err = os .MkdirAll (destFilePath , os .ModePerm )
98- } else {
99- err = Sync (filepath .Join (srcPath , srcFile ), destFilePath )
84+ } else if filepath . Base ( destFilePath ) != keepFile {
85+ err = SyncFile (filepath .Join (srcPath , srcFile ), destFilePath )
10086 }
10187 if err != nil {
10288 return err
0 commit comments