Skip to content

Commit

Permalink
Merge pull request #107 from JunNishimura/#106
Browse files Browse the repository at this point in the history
refactoring around branch
  • Loading branch information
JunNishimura committed Jun 8, 2023
2 parents ecdc2db + c96c53a commit 8d1f43c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 45 deletions.
18 changes: 15 additions & 3 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,31 @@ var addCmd = &cobra.Command{
for _, arg := range args {
if _, err := os.Stat(arg); os.IsNotExist(err) {
// If the file does not exist but is registered in the index, delete it from the index
// but not delete here, just check it
cleanedArg := filepath.Clean(arg)
cleanedArg = strings.ReplaceAll(cleanedArg, `\`, "/")
_, _, isEntryFound := client.Idx.GetEntry([]byte(cleanedArg))
if !isEntryFound {
return fmt.Errorf(`path "%s" did not match any files`, arg)
}
}
}

for _, arg := range args {
// If the file does not exist but is registered in the index, delete it from the index
if _, err := os.Stat(arg); os.IsNotExist(err) {
cleanedArg := filepath.Clean(arg)
cleanedArg = strings.ReplaceAll(cleanedArg, `\`, "/")
_, entry, isEntryFound := client.Idx.GetEntry([]byte(cleanedArg))
if !isEntryFound {
return fmt.Errorf(`path "%s" did not match any files`, arg)
}
if err := client.Idx.DeleteEntry(client.RootGoitPath, entry); err != nil {
return fmt.Errorf("fail to delete '%s' from index: %w", cleanedArg, err)
return fmt.Errorf("fail to delete untracked file %s: %w", cleanedArg, err)
}
continue
}
}

for _, arg := range args {
path, err := filepath.Abs(arg)
if err != nil {
return fmt.Errorf("fail to convert abs path: %s", arg)
Expand Down
7 changes: 1 addition & 6 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func commit() error {

// make and write commit object
var data []byte
branchPath := filepath.Join(client.RootGoitPath, "refs", "heads", "main")
branchPath := filepath.Join(client.RootGoitPath, "refs", "heads", client.Head.Reference)
branchBytes, err := os.ReadFile(branchPath)
author := object.NewSign(client.Conf.GetUserName(), client.Conf.GetEmail())
committer := author
Expand Down Expand Up @@ -182,11 +182,6 @@ var commitCmd = &cobra.Command{
return err
}
} else {
// check if files are deleted
if err := client.Idx.DeleteUntrackedFiles(client.RootGoitPath); err != nil {
return fmt.Errorf("fail to delete untracked files: %w", err)
}

// compare last commit with index
isCommitNecessary, err := isCommitNecessary(client.Head.Commit)
if err != nil {
Expand Down
14 changes: 1 addition & 13 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,8 @@ var logCmd = &cobra.Command{
return fmt.Errorf("fatal: your current branch 'main' does not have any commits yet")
}

// get last commit hash
branchPath := filepath.Join(dirName, "main")
lastCommitHashBytes, err := os.ReadFile(branchPath)
if err != nil {
return fmt.Errorf("%w: %s", ErrIOHandling, branchPath)
}
lastCommitHashString := string(lastCommitHashBytes)
lastCommitHash, err := sha.ReadHash(lastCommitHashString)
if err != nil {
return fmt.Errorf("fail to read hash: %w", err)
}

// print log
if err := walkHistory(lastCommitHash, func(commit *object.Commit) error {
if err := walkHistory(client.Head.Commit.Hash, func(commit *object.Commit) error {
fmt.Println(commit)
return nil
}); err != nil {
Expand Down
23 changes: 0 additions & 23 deletions internal/store/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,6 @@ func (idx *Index) DeleteEntry(rootGoitPath string, entry *Entry) error {
return nil
}

func (idx *Index) DeleteUntrackedFiles(rootGoitPath string) error {
var trackedEntries []*Entry
for _, entry := range idx.Entries {
if _, err := os.Stat(string(entry.Path)); !os.IsNotExist(err) {
trackedEntries = append(trackedEntries, entry)
}
}

// no need to delete
if len(trackedEntries) == int(idx.EntryNum) {
return nil
}

// need to update index
idx.Entries = trackedEntries
idx.EntryNum = uint32(len(idx.Entries))
if err := idx.write(rootGoitPath); err != nil {
return err
}

return nil
}

func (idx *Index) read(rootGoitPath string) error {
// read index
indexPath := filepath.Join(rootGoitPath, "index")
Expand Down

0 comments on commit 8d1f43c

Please sign in to comment.