Skip to content

Commit

Permalink
../ error solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Serenity0204 committed Aug 18, 2023
1 parent 78b41ff commit 0a42956
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions internal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"errors"
"strconv"
"strings"

"github.com/Serenity0204/LVCS/internal/utils"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -324,17 +332,16 @@ 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")
}
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
}
55 changes: 0 additions & 55 deletions internal/manager_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/utils/fileHashIO.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions internal/utils/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
6 changes: 5 additions & 1 deletion internal/utils/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0a42956

Please sign in to comment.