From 0a4295662443f7d3cad1a1ccfae045b29606383d Mon Sep 17 00:00:00 2001 From: Yu-Heng Lin Date: Fri, 18 Aug 2023 03:51:42 -0700 Subject: [PATCH] ../ error solved --- Makefile | 2 +- README.md | 41 ++++++++++++------------- internal/handler.go | 13 ++++++-- internal/manager_test.go | 55 ---------------------------------- internal/utils/fileHashIO.go | 4 +-- internal/utils/restore.go | 26 ++++++++-------- internal/utils/restore_test.go | 2 +- internal/utils/stage.go | 6 +++- 8 files changed, 53 insertions(+), 96 deletions(-) delete mode 100644 internal/manager_test.go diff --git a/Makefile b/Makefile index 95c36f5..7c6f378 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ build: test: ## utils, manager, then models - cd internal/utils && go test -v && cd .. && go test -v && cd models && go test -v + cd internal/utils && go test -v && cd .. && cd models && go test -v ifeq ($(OS),Windows_NT) clean: diff --git a/README.md b/README.md index f5555c4..5a19d6c 100644 --- a/README.md +++ b/README.md @@ -14,26 +14,27 @@ make sure to have go install on your machine ## Example Usage Go to the directory you want to use LVCS -* LVCS init -* LVCS stage add "file 0" -* LVCS commit -* LVCS commit tree -* LVCS stage add "file 1" "file 2" -* LVCS commit fresh -* LVCS stage -* LVCS stage add "file 1" "file 2" -* LVCS stage untrack "file 2" -* LVCS commit current -* LVCS commit switch v0 -* LVCS stage -* LVCS commit -* LVCS commit tree -* LVCS branch -* LVCS log -* LVCS commit lca v1 v2 -* LVCS restore v0 -* LVCS dump - +``` +LVCS init +LVCS stage add "file 0" +LVCS commit +LVCS commit tree +LVCS stage add "file 1" "file 2" +LVCS commit fresh +LVCS stage +LVCS stage add "file 1" "file 2" +LVCS stage untrack "file 2" +LVCS commit current +LVCS commit switch v0 +LVCS stage +LVCS commit +LVCS commit tree +LVCS branch +LVCS log +LVCS commit lca v1 v2 +LVCS restore v0 +LVCS dump +``` ## Uninstall * windows: del %GOPATH%\bin\LVCS.exe diff --git a/internal/handler.go b/internal/handler.go index 8bd9cff..c6f33fc 100644 --- a/internal/handler.go +++ b/internal/handler.go @@ -3,6 +3,7 @@ package internal import ( "errors" "strconv" + "strings" "github.com/Serenity0204/LVCS/internal/utils" ) @@ -19,6 +20,9 @@ func (lvcsManager *LVCSManager) hashObjectHandler(subcommands []string) (string, } oids := "List of hashed OIDs:\n" for _, file := range subcommands { + if strings.Contains(file, "../") { + return "", errors.New("cannot hashObject files that's above the root of this directory") + } oid, err := fileHashIOMan.HashObject(file) if err != nil { return "", err @@ -258,6 +262,10 @@ func (lvcsManager *LVCSManager) stageHandler(subcommands []string) (string, erro continue } if subcommands[0] == "add" { + // if file includes ../ then fatal + if strings.Contains(file, "../") { + return "", errors.New("cannot add files that's above the root of this directory") + } err := stageMan.Add(file) if err != nil { return "", err @@ -324,7 +332,6 @@ func (lvcsManager *LVCSManager) logHandler(subcommands []string) (string, error) } func (lvcsManager *LVCSManager) restoreHandler(subcommands []string) (string, error) { - // restoreMan, ok := lvcsManager.lvcsMan["restore"].(*utils.LVCSRestoreManager) if !ok { return "", errors.New("failed to execute restore") @@ -332,9 +339,9 @@ func (lvcsManager *LVCSManager) restoreHandler(subcommands []string) (string, er if len(subcommands) != 1 { return "", errors.New("invalid number of arguments") } - err := restoreMan.Restore(subcommands[0]) + branchName, err := restoreMan.Restore(subcommands[0]) if err != nil { return "", err } - return string("Restored " + subcommands[0] + " as " + subcommands[0] + " success"), nil + return string("Restored " + subcommands[0] + " as " + branchName + "_" + subcommands[0] + ".zip success"), nil } diff --git a/internal/manager_test.go b/internal/manager_test.go deleted file mode 100644 index 2d04a35..0000000 --- a/internal/manager_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package internal_test - -import ( - "os" - "testing" - - "github.com/Serenity0204/LVCS/internal" -) - -const lvcsManTestDir string = "../.lvcs_test" - -func removeLVCSTestDir() error { - err := os.RemoveAll(lvcsManTestDir) - if err != nil { - return err - } - return nil -} - -func TestLVCSManager(t *testing.T) { - err := removeLVCSTestDir() - if err != nil { - t.Errorf("failed to delete existing dir: %s", err.Error()) // Convert error to string - } - - lvcsMan := internal.NewLVCSManager(lvcsManTestDir) - _, err = lvcsMan.Execute("init", []string{}) - if err != nil { - t.Errorf("create .lvcs_test DIR failed: %s", err.Error()) // Convert error to string - } - - _, err = lvcsMan.Execute("hashObject", []string{"../test_data/a.txt"}) - if err != nil { - t.Errorf("hash-object failed: %s", err.Error()) // Convert error to string - } - - content, err := lvcsMan.Execute("catFile", []string{"84e3a8e13916a5e48349e49fe16cfab6a384b4a9"}) - expectedContent := "To implement functionality similar to cat-file in Git, where you convert an object ID (OID) back to its corresponding string content" - if err != nil || content != expectedContent { - t.Errorf("cat-file failed: %s", err.Error()) // Convert error to string - } - - _, err = lvcsMan.Execute("stage", []string{"add", "../test_data/a.txt", "../test_data/b.txt", "../test_data/ok/abc.txt"}) - if err != nil { - t.Errorf("add failed: %s", err.Error()) // Convert error to string - } - _, err = lvcsMan.Execute("commit", []string{}) - if err != nil { - t.Errorf(err.Error()) - } - err = lvcsMan.Dump() - if err != nil { - t.Errorf(err.Error()) - } -} diff --git a/internal/utils/fileHashIO.go b/internal/utils/fileHashIO.go index 0abe218..9032731 100644 --- a/internal/utils/fileHashIO.go +++ b/internal/utils/fileHashIO.go @@ -42,10 +42,10 @@ func (lvcsFileHashIO *LVCSFileHashIOManager) HashObject(file string) (string, er } if info.IsDir() { - return "", errors.New("cannot add a directory") + return "", errors.New("cannot hashobject a directory") } if lvcsFileHashIO.ignoreOrAbsPath(file) { - return "", errors.New("cannot hash .lvcs element or absolute path") + return "", errors.New("cannot hashobject .lvcs element or absolute path") } content, err := os.ReadFile(file) diff --git a/internal/utils/restore.go b/internal/utils/restore.go index d05da81..1f87243 100644 --- a/internal/utils/restore.go +++ b/internal/utils/restore.go @@ -21,64 +21,64 @@ func NewLVCSRestoreManager(lvcsPath string) *LVCSRestoreManager { } } -// restore data by commit version under current branch -func (lvcsRestore *LVCSRestoreManager) Restore(version string) error { +// restore data by commit version under current branch, return the branch name as well +func (lvcsRestore *LVCSRestoreManager) Restore(version string) (string, error) { // check if version exists first lvcsBranch := NewLVCSBranchManager(lvcsRestore.lvcsPath) curBranch, err := lvcsBranch.GetCurrentBranch() if err != nil { - return err + return "", err } lvcsCommit := NewLVCSCommitManager(lvcsRestore.lvcsPath) exist, err := lvcsCommit.versionExists(curBranch, version) if err != nil { - return err + return "", err } if !exist { - return errors.New("version:" + version + " does not exist") + return "", errors.New("version:" + version + " does not exist") } // read it versionPath := lvcsRestore.lvcsCommitPath + "/" + curBranch + "/" + version + ".txt" toBeCreated, oids, err := lvcsRestore.getFilesAndOIDs(versionPath) if err != nil { - return err + return "", err } // create temp folder err = lvcsRestore.createTempFolder() if err != nil { - return err + return "", err } wd, err := os.Getwd() if err != nil { - return err + return "", err } tempDir := wd + "/temp" err = lvcsRestore.createDirectoriesAndFiles(toBeCreated, tempDir) if err != nil { - return err + return "", err } // write oid content err = lvcsRestore.writeOIDContent(toBeCreated, oids) if err != nil { - return err + return "", err } // zip it err = lvcsRestore.createZipFile(tempDir, version, curBranch) if err != nil { - return err + return "", err } // remove temp err = os.RemoveAll(tempDir) if err != nil { - return err + return "", err } - return nil + return curBranch, nil } func (lvcsRestore *LVCSRestoreManager) createTempFolder() error { diff --git a/internal/utils/restore_test.go b/internal/utils/restore_test.go index f74af94..b7f218d 100644 --- a/internal/utils/restore_test.go +++ b/internal/utils/restore_test.go @@ -45,7 +45,7 @@ func TestRestore(t *testing.T) { // after add and commit, restore lvcsRestore := utils.NewLVCSRestoreManager(lvcsTestDir) - err = lvcsRestore.Restore("v0") + _, err = lvcsRestore.Restore("v0") if err != nil { t.Errorf(err.Error()) } diff --git a/internal/utils/stage.go b/internal/utils/stage.go index 5c689f0..d87cc19 100644 --- a/internal/utils/stage.go +++ b/internal/utils/stage.go @@ -105,8 +105,12 @@ func (lvcsStage *LVCSStageManager) Add(file string) error { } defer stageFile.Close() + // Extract the part of the path after the last "../", this is only for testing, in real production path that includes .. will fatal + parts := strings.Split(file, "../") + fileName := parts[len(parts)-1] + // Append absolute file path, oid, filename into stage.txt - content := absPath + " " + string(oid) + " " + file + "\n" + content := absPath + " " + string(oid) + " " + fileName + "\n" _, err = stageFile.WriteString(content) if err != nil { return err