Skip to content

Commit

Permalink
Merge pull request #1 from rajnandan1/optimize
Browse files Browse the repository at this point in the history
fix: added okgit to root folder
  • Loading branch information
rajnandan1 authored May 29, 2024
2 parents 6da2df6 + e305a62 commit 44590b6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 60 deletions.
26 changes: 11 additions & 15 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"errors"
"fmt"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -39,6 +38,11 @@ var commitCmd = &cobra.Command{

myCommit := models.Commit{}

branchFile, err := utils.StartOkgit()
if err != nil {
utils.LogFatal(err)
}

//Get the last commit if available

gitBranch := models.AllCommands["gitBranch"]
Expand All @@ -47,7 +51,7 @@ var commitCmd = &cobra.Command{
if cmdErr != nil {
utils.LogFatal(cmdErr)
}
if storedCommit, err := utils.GetLastCommitForBranchFromFile(branch); err == nil {
if storedCommit, err := utils.GetLastCommitForBranchFromFile(branch, branchFile); err == nil {
myCommit = *storedCommit
}
if myCommit.Type == "" {
Expand Down Expand Up @@ -156,19 +160,11 @@ var commitCmd = &cobra.Command{

commit := generateCommit(myCommit)

utils.CreateDirectoryAndFileIfNotExist()
err := utils.AddCommitToBranchFile(string(branch), myCommit)
if err != nil {
utils.LogFatal(errors.New("Error adding commit to branch file:"))
}
fmt.Println("--------------")
fmt.Println("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
fmt.Println("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
fmt.Println("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
fmt.Println("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
fmt.Println("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
fmt.Println("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
fmt.Println("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
// utils.CreateDirectoryAndFileIfNotExist()
errWr := utils.AddCommitToBranchFile(string(branch), myCommit, branchFile)
if errWr != nil {
utils.LogFatal(errWr)
}
gitCommit := models.AllCommands["gitCommit"]
cmdOutCommit, cmdErr := utils.RunCommand(gitCommit.Name, gitCommit.Arguments, commit)
if cmdErr != nil {
Expand Down
28 changes: 3 additions & 25 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
package cmd

import (
"io/ioutil"
"path/filepath"
"strings"

"github.com/rajnandan1/okgit/utils"
"github.com/spf13/cobra"
)

var initCmd = &cobra.Command{
Use: "init",
Short: "Add .okgit/ to the .gitignore file",
Short: "Add .okgit/ to home folder",
Run: func(cmd *cobra.Command, args []string) {
gitignorePath := filepath.Join(".", ".gitignore")
okgitPath := ".okgit/"

// Read the contents of the .gitignore file
data, err := ioutil.ReadFile(gitignorePath)
_, err := utils.StartOkgit()
if err != nil {
utils.LogFatal(err)
}
utils.LogOutput(".okgit/ added to home folder")

// Append .okgit/ to the .gitignore file if it's not already present
contents := string(data)
if !strings.Contains(contents, okgitPath) {
contents += okgitPath + "\n"

// Write the updated contents back to the .gitignore file
err = ioutil.WriteFile(gitignorePath, []byte(contents), 0644)
if err != nil {
utils.LogFatal(err)
}

utils.LogOutput(".okgit/ added to .gitignore file")
} else {
utils.LogOutput(".okgit/ already present in .gitignore file")
}
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Version: "1.0.12",
Version: "1.0.13",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
4 changes: 4 additions & 0 deletions models/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ var AllCommands = map[string]ShellCommands{
Name: "go",
Arguments: []string{"install", "-v", "github.com/rajnandan1/okgit@latest"},
},
"remoteOriginUrl": {
Name: "git",
Arguments: []string{"config", "--get", "remote.origin.url"},
},
}
93 changes: 74 additions & 19 deletions utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -52,14 +53,7 @@ func CreateDirectoryAndFileIfNotExist() error {
return nil
}

func ReadBranchesFile() (map[string][]models.Commit, error) {

currentDir, err := os.Getwd()
if err != nil {
return nil, err
}

filePath := filepath.Join(currentDir, directoryName, fileName)
func ReadBranchesFile(filePath string) (map[string][]models.Commit, error) {

// Check if the file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
Expand All @@ -85,8 +79,8 @@ func ReadBranchesFile() (map[string][]models.Commit, error) {
return branches, nil
}

func AddCommitToBranchFile(branchName string, cmt models.Commit) error {
branches, err := ReadBranchesFile()
func AddCommitToBranchFile(branchName string, cmt models.Commit, filePath string) error {
branches, err := ReadBranchesFile(filePath)
if err != nil {
return err
}
Expand All @@ -105,13 +99,6 @@ func AddCommitToBranchFile(branchName string, cmt models.Commit) error {
return err
}

currentDir, err := os.Getwd()
if err != nil {
return err
}

filePath := filepath.Join(currentDir, directoryName, fileName)

// Write the data to the file
err = os.WriteFile(filePath, data, 0644)
if err != nil {
Expand All @@ -121,8 +108,8 @@ func AddCommitToBranchFile(branchName string, cmt models.Commit) error {
return nil
}

func GetLastCommitForBranchFromFile(branchName string) (*models.Commit, error) {
branches, err := ReadBranchesFile()
func GetLastCommitForBranchFromFile(branchName string, filepath string) (*models.Commit, error) {
branches, err := ReadBranchesFile(filepath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -194,3 +181,71 @@ func LogOutput(output string) {
fmt.Println(green(output))
}
}

func GetHomeDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return homeDir, nil
}

func CreateDirectory(directoryPath string) error {
err := os.MkdirAll(directoryPath, 0755)
if err != nil {
return err
}
return nil
}

func GetRepoNameHash() string {
remoteOriginUrlCmd := models.AllCommands["remoteOriginUrl"]
remoteOriginUrl, err := RunCommand(remoteOriginUrlCmd.Name, remoteOriginUrlCmd.Arguments, "")
if err != nil {
remoteOriginUrl = "okgit"
}
//generate md5 hash of the remote origin url
return fmt.Sprintf("%x", md5.Sum([]byte(remoteOriginUrl)))
}

func StartOkgit() (string, error) {
homeDir, err := GetHomeDir()
if err != nil {
return "", err
}

directoryPath := filepath.Join(homeDir, "okgit")
//filePath := filepath.Join(directoryPath, "branches.json")

// Check if the directory already exists
if _, err := ioutil.ReadDir(directoryPath); err != nil {
// Directory does not exist, create it
err := CreateDirectory(directoryPath)
if err != nil {
return "", err
}
}

repoFolder := GetRepoNameHash()
repoFolderPath := filepath.Join(directoryPath, repoFolder)

// Check if the directory already exists
if _, err := ioutil.ReadDir(repoFolderPath); err != nil {
// Directory does not exist, create it
err := CreateDirectory(repoFolderPath)
if err != nil {
return "", err
}
}

fileName := "branches.json"
filePath := filepath.Join(repoFolderPath, fileName)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
err = ioutil.WriteFile(filePath, []byte("{}"), 0644)
if err != nil {
return "", err
}
}

return filePath, nil
}

0 comments on commit 44590b6

Please sign in to comment.