Skip to content

Commit

Permalink
initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Nov 11, 2023
1 parent d7d9261 commit 70dc852
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 46 deletions.
56 changes: 55 additions & 1 deletion app/helpers/git-helpers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package githelpers
package helpers

import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"strings"

"github.com/permafrost-dev/git-ninja/app/utils"
)

func RunCommandOnStdout(command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return err
}

return nil
}

func GetLastCheckedoutBranchName() (string, error) {
var out bytes.Buffer

Expand Down Expand Up @@ -37,3 +52,42 @@ func GetLastCheckedoutBranchName() (string, error) {

return "", fmt.Errorf("no checkout entries found")
}

func GetCurrentBranchName() (string, error) {
result, err := utils.RunCommand("git", "branch", "--show-current")
result = strings.TrimSpace(result)

if strings.Contains(result, " ") {
return "", fmt.Errorf(result)
}

return result, err
}

func BranchExists(name string) (bool, error) {
var out bytes.Buffer

cmd := exec.Command("git", "branch", "--list")
cmd.Stdout = &out

if err := cmd.Run(); err != nil {
return false, err
}

// Scan through each line of output
scanner := bufio.NewScanner(strings.NewReader(out.String()))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
line = strings.TrimPrefix(line, "* ")

if strings.EqualFold(line, name) {
return true, nil
}
}

if err := scanner.Err(); err != nil {
return false, err
}

return false, nil
}
16 changes: 16 additions & 0 deletions app/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import (
"os/exec"
"strings"
)

func RunCommand(command string, args ...string) (string, error) {
cmd := exec.Command(command, args...)
result, err := cmd.Output()
if err != nil {
return "", err
}

return strings.TrimSpace(string(result)), nil
}
39 changes: 39 additions & 0 deletions cmd/branchCurrent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"fmt"

"github.com/permafrost-dev/git-ninja/app/helpers"
"github.com/spf13/cobra"
)

func init() {
pushFlag := false
pullFlag := false

cmd := &cobra.Command{
Use: "branch:current",
Short: "Work with the current branch or return the current branch name",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
branchName, _ := helpers.GetCurrentBranchName()

if pushFlag {
helpers.RunCommandOnStdout("git", "push", "origin", branchName)
return
}

if pullFlag {
helpers.RunCommandOnStdout("git", "pull", "origin", branchName, "--rebase")
return
}

fmt.Println(branchName)
},
}

cmd.Flags().BoolVarP(&pushFlag, "push", "p", false, "push the current branch to origin")
cmd.Flags().BoolVarP(&pullFlag, "pull", "u", false, "pull the current branch from origin and rebase")

rootCmd.AddCommand(cmd)
}
28 changes: 28 additions & 0 deletions cmd/branchExists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"
"os"

"github.com/permafrost-dev/git-ninja/app/helpers"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(&cobra.Command{
Use: "branch:exists [name]",
Short: "Check if the given branch name exists",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("error: branch name required")
return
}

if exists, _ := helpers.BranchExists(args[0]); !exists {
os.Exit(1)
}

os.Exit(0)
},
})
}
19 changes: 19 additions & 0 deletions cmd/branchLast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"fmt"

"github.com/permafrost-dev/git-ninja/app/helpers"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(&cobra.Command{
Use: "branch:last",
Short: "Show the last checked out branch name",
Run: func(cmd *cobra.Command, args []string) {
branchName, _ := helpers.GetLastCheckedoutBranchName()
fmt.Println(branchName)
},
})
}
26 changes: 26 additions & 0 deletions cmd/pushCUrrentBranch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"fmt"
"os/exec"

"github.com/permafrost-dev/git-ninja/app/helpers"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(&cobra.Command{
Use: "branch:current:push",
Short: "Push the current branch to origin",
Run: func(c *cobra.Command, args []string) {
branch, err := helpers.GetCurrentBranchName()

if err != nil {
fmt.Println(err)
return
}

exec.Command("git", "push", "origin", branch).Run()
},
})
}
40 changes: 0 additions & 40 deletions cmd/showLastBranch.go

This file was deleted.

8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/vendor-name/git-ninja
module github.com/permafrost-dev/git-ninja

go 1.19

require github.com/blang/semver v3.5.1+incompatible
require (
github.com/blang/semver v3.5.1+incompatible
github.com/spf13/cobra v1.8.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main

import "github.com/vendor-name/git-ninja/cmd"
import "github.com/permafrost-dev/git-ninja/cmd"

func main() {
cmd.Execute()
Expand Down

0 comments on commit 70dc852

Please sign in to comment.