complete git checkout

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-30 16:13:29 +03:00
parent 924912951e
commit c0a0a8d4aa
3 changed files with 72 additions and 6 deletions

View File

@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"strings"
"github.com/unistack-org/micro/v3/logger"
)
func isGenerated(name string) bool {
@ -40,9 +42,11 @@ func cleanDir(dir string) error {
return err
}
if dir == "vendor" {
logger.Info("skip vendor dir")
return filepath.SkipDir
}
if isGenerated(path) {
logger.Infof("remove generated file: %s", path)
err = os.Remove(path)
}
return err

46
copy.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"io"
"os"
"path/filepath"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/unistack-org/micro/v3/logger"
)
func writeFile(file *object.File, dir string, flag int, mode os.FileMode) error {
path := filepath.Join(dir, file.Name)
if err := os.MkdirAll(filepath.Dir(path), os.FileMode(0755)); err != nil {
return err
}
w, err := os.OpenFile(path, flag, mode)
if err != nil {
return err
}
defer func() {
if err := w.Close(); err != nil {
logger.Errorf("failed to close file: %v", err)
}
}()
r, err := file.Reader()
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
logger.Errorf("failed to close file: %v", err)
}
}()
if _, err = io.Copy(w, r); err != nil {
return err
}
return nil
}

28
main.go
View File

@ -6,23 +6,23 @@ import (
"fmt"
"go/types"
"os"
"path/filepath"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/unistack-org/micro/v3/logger"
)
var (
flagDir string
flagUrl string
flagForce bool
flagDir string
flagUrl string
)
func init() {
flag.StringVar(&flagDir, "dstdir", "", "place for generated files")
flag.StringVar(&flagUrl, "url", "", "repo url path")
flag.BoolVar(&flagForce, "force", false, "owerwrite files")
}
func main() {
@ -82,9 +82,25 @@ func main() {
err = tree.Files().ForEach(func(file *object.File) error {
if file == nil {
return types.Error{Msg: "File pointer is empty"}
return types.Error{Msg: "file pointer is empty"}
}
fmt.Printf("%#+v\n", file)
fmode, err := file.Mode.ToOSFileMode()
if err != nil {
return err
}
switch file.Mode {
case filemode.Executable:
return writeFile(file, flagDir, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fmode)
case filemode.Dir:
return os.MkdirAll(filepath.Join(flagDir, file.Name), fmode)
case filemode.Regular:
return writeFile(file, flagDir, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fmode)
default:
return fmt.Errorf("unsupported filetype %v for %s", file.Mode, file.Name)
}
return nil
})