Skip to content

Commit e305a62

Browse files
Raj Nandan SharmaRaj Nandan Sharma
Raj Nandan Sharma
authored and
Raj Nandan Sharma
committed
fix: added okgit to root folder
1 parent 6da2df6 commit e305a62

File tree

5 files changed

+93
-60
lines changed

5 files changed

+93
-60
lines changed

cmd/commit.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"errors"
5-
"fmt"
65
"strings"
76

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

4039
myCommit := models.Commit{}
4140

41+
branchFile, err := utils.StartOkgit()
42+
if err != nil {
43+
utils.LogFatal(err)
44+
}
45+
4246
//Get the last commit if available
4347

4448
gitBranch := models.AllCommands["gitBranch"]
@@ -47,7 +51,7 @@ var commitCmd = &cobra.Command{
4751
if cmdErr != nil {
4852
utils.LogFatal(cmdErr)
4953
}
50-
if storedCommit, err := utils.GetLastCommitForBranchFromFile(branch); err == nil {
54+
if storedCommit, err := utils.GetLastCommitForBranchFromFile(branch, branchFile); err == nil {
5155
myCommit = *storedCommit
5256
}
5357
if myCommit.Type == "" {
@@ -156,19 +160,11 @@ var commitCmd = &cobra.Command{
156160

157161
commit := generateCommit(myCommit)
158162

159-
utils.CreateDirectoryAndFileIfNotExist()
160-
err := utils.AddCommitToBranchFile(string(branch), myCommit)
161-
if err != nil {
162-
utils.LogFatal(errors.New("Error adding commit to branch file:"))
163-
}
164-
fmt.Println("--------------")
165-
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.")
166-
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.")
167-
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.")
168-
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.")
169-
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.")
170-
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.")
171-
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.")
163+
// utils.CreateDirectoryAndFileIfNotExist()
164+
errWr := utils.AddCommitToBranchFile(string(branch), myCommit, branchFile)
165+
if errWr != nil {
166+
utils.LogFatal(errWr)
167+
}
172168
gitCommit := models.AllCommands["gitCommit"]
173169
cmdOutCommit, cmdErr := utils.RunCommand(gitCommit.Name, gitCommit.Arguments, commit)
174170
if cmdErr != nil {

cmd/init.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
11
package cmd
22

33
import (
4-
"io/ioutil"
5-
"path/filepath"
6-
"strings"
7-
84
"github.com/rajnandan1/okgit/utils"
95
"github.com/spf13/cobra"
106
)
117

128
var initCmd = &cobra.Command{
139
Use: "init",
14-
Short: "Add .okgit/ to the .gitignore file",
10+
Short: "Add .okgit/ to home folder",
1511
Run: func(cmd *cobra.Command, args []string) {
16-
gitignorePath := filepath.Join(".", ".gitignore")
17-
okgitPath := ".okgit/"
18-
19-
// Read the contents of the .gitignore file
20-
data, err := ioutil.ReadFile(gitignorePath)
12+
_, err := utils.StartOkgit()
2113
if err != nil {
2214
utils.LogFatal(err)
2315
}
16+
utils.LogOutput(".okgit/ added to home folder")
2417

25-
// Append .okgit/ to the .gitignore file if it's not already present
26-
contents := string(data)
27-
if !strings.Contains(contents, okgitPath) {
28-
contents += okgitPath + "\n"
29-
30-
// Write the updated contents back to the .gitignore file
31-
err = ioutil.WriteFile(gitignorePath, []byte(contents), 0644)
32-
if err != nil {
33-
utils.LogFatal(err)
34-
}
35-
36-
utils.LogOutput(".okgit/ added to .gitignore file")
37-
} else {
38-
utils.LogOutput(".okgit/ already present in .gitignore file")
39-
}
4018
},
4119
}
4220

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{
1717
// Uncomment the following line if your bare application
1818
// has an action associated with it:
1919
// Run: func(cmd *cobra.Command, args []string) { },
20-
Version: "1.0.12",
20+
Version: "1.0.13",
2121
}
2222

2323
// Execute adds all child commands to the root command and sets flags appropriately.

models/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@ var AllCommands = map[string]ShellCommands{
7575
Name: "go",
7676
Arguments: []string{"install", "-v", "github.com/rajnandan1/okgit@latest"},
7777
},
78+
"remoteOriginUrl": {
79+
Name: "git",
80+
Arguments: []string{"config", "--get", "remote.origin.url"},
81+
},
7882
}

utils/fs.go

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"bufio"
55
"bytes"
6+
"crypto/md5"
67
"encoding/json"
78
"fmt"
89
"io/ioutil"
@@ -52,14 +53,7 @@ func CreateDirectoryAndFileIfNotExist() error {
5253
return nil
5354
}
5455

55-
func ReadBranchesFile() (map[string][]models.Commit, error) {
56-
57-
currentDir, err := os.Getwd()
58-
if err != nil {
59-
return nil, err
60-
}
61-
62-
filePath := filepath.Join(currentDir, directoryName, fileName)
56+
func ReadBranchesFile(filePath string) (map[string][]models.Commit, error) {
6357

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

88-
func AddCommitToBranchFile(branchName string, cmt models.Commit) error {
89-
branches, err := ReadBranchesFile()
82+
func AddCommitToBranchFile(branchName string, cmt models.Commit, filePath string) error {
83+
branches, err := ReadBranchesFile(filePath)
9084
if err != nil {
9185
return err
9286
}
@@ -105,13 +99,6 @@ func AddCommitToBranchFile(branchName string, cmt models.Commit) error {
10599
return err
106100
}
107101

108-
currentDir, err := os.Getwd()
109-
if err != nil {
110-
return err
111-
}
112-
113-
filePath := filepath.Join(currentDir, directoryName, fileName)
114-
115102
// Write the data to the file
116103
err = os.WriteFile(filePath, data, 0644)
117104
if err != nil {
@@ -121,8 +108,8 @@ func AddCommitToBranchFile(branchName string, cmt models.Commit) error {
121108
return nil
122109
}
123110

124-
func GetLastCommitForBranchFromFile(branchName string) (*models.Commit, error) {
125-
branches, err := ReadBranchesFile()
111+
func GetLastCommitForBranchFromFile(branchName string, filepath string) (*models.Commit, error) {
112+
branches, err := ReadBranchesFile(filepath)
126113
if err != nil {
127114
return nil, err
128115
}
@@ -194,3 +181,71 @@ func LogOutput(output string) {
194181
fmt.Println(green(output))
195182
}
196183
}
184+
185+
func GetHomeDir() (string, error) {
186+
homeDir, err := os.UserHomeDir()
187+
if err != nil {
188+
return "", err
189+
}
190+
return homeDir, nil
191+
}
192+
193+
func CreateDirectory(directoryPath string) error {
194+
err := os.MkdirAll(directoryPath, 0755)
195+
if err != nil {
196+
return err
197+
}
198+
return nil
199+
}
200+
201+
func GetRepoNameHash() string {
202+
remoteOriginUrlCmd := models.AllCommands["remoteOriginUrl"]
203+
remoteOriginUrl, err := RunCommand(remoteOriginUrlCmd.Name, remoteOriginUrlCmd.Arguments, "")
204+
if err != nil {
205+
remoteOriginUrl = "okgit"
206+
}
207+
//generate md5 hash of the remote origin url
208+
return fmt.Sprintf("%x", md5.Sum([]byte(remoteOriginUrl)))
209+
}
210+
211+
func StartOkgit() (string, error) {
212+
homeDir, err := GetHomeDir()
213+
if err != nil {
214+
return "", err
215+
}
216+
217+
directoryPath := filepath.Join(homeDir, "okgit")
218+
//filePath := filepath.Join(directoryPath, "branches.json")
219+
220+
// Check if the directory already exists
221+
if _, err := ioutil.ReadDir(directoryPath); err != nil {
222+
// Directory does not exist, create it
223+
err := CreateDirectory(directoryPath)
224+
if err != nil {
225+
return "", err
226+
}
227+
}
228+
229+
repoFolder := GetRepoNameHash()
230+
repoFolderPath := filepath.Join(directoryPath, repoFolder)
231+
232+
// Check if the directory already exists
233+
if _, err := ioutil.ReadDir(repoFolderPath); err != nil {
234+
// Directory does not exist, create it
235+
err := CreateDirectory(repoFolderPath)
236+
if err != nil {
237+
return "", err
238+
}
239+
}
240+
241+
fileName := "branches.json"
242+
filePath := filepath.Join(repoFolderPath, fileName)
243+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
244+
err = ioutil.WriteFile(filePath, []byte("{}"), 0644)
245+
if err != nil {
246+
return "", err
247+
}
248+
}
249+
250+
return filePath, nil
251+
}

0 commit comments

Comments
 (0)