Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Nov 11, 2023
1 parent 7fb308f commit d7d9261
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .custom-hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
PROJECT_BASE_PATH=$(git rev-parse --show-toplevel)
cd $PROJECT_BASE_PATH

GO_FILES_STAGED=$(git diff --name-only --cached | grep .go | wc -l)
GO_FILES_STAGED=$(git diff --name-only --cached | egrep '\.go$' | wc -l)

echo "[pre-commit] GO_FILES_STAGED=$GO_FILES_STAGED"

Expand All @@ -12,7 +12,7 @@ if [ ! -z "$GO_FILES_STAGED" ] && [ "$GO_FILES_STAGED" != "0" ]; then

if [ ! -z "$GOLINT_BIN" ]; then
LAST_COMMIT=$(git rev-parse HEAD)
SRC_PATHS=$(find . -name "*.go" -printf '%h/**\n' | egrep -v '(\./\..+|tools|build|dist)') | tr '\n' ' '
SRC_PATHS=$(find . -name "*.go" -printf '%h/**\n' | egrep -v '(\./\..+|tools|build|dist)') | tr '\n' ' '

if [ -z "$SRC_PATHS" ]; then
SRC_PATHS="./app ./cmd"
Expand Down
39 changes: 39 additions & 0 deletions app/helpers/git-helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package githelpers

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

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

cmd := exec.Command("git", "reflog", "show", "--pretty=format:%gs", "--date=relative")
cmd.Stdout = &out

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

// Scan through each line of output
scanner := bufio.NewScanner(strings.NewReader(out.String()))
for scanner.Scan() {
line := scanner.Text()
// Check if the line contains 'checkout:'
if strings.Contains(line, "checkout:") {
fields := strings.Fields(line)
if len(fields) >= 4 {
return fields[3], nil // Return the fourth word (branch name)
}
}
}

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

return "", fmt.Errorf("no checkout entries found")
}
40 changes: 40 additions & 0 deletions cmd/showLastBranch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
githelpers "github.com/vendor-name/git-ninja/app/helpers"
)

// showLastBranchCmd represents the showLastBranch command
var showLastBranchCmd = &cobra.Command{
Use: "branch:show-last",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(githelpers.GetLastCheckedoutBranchName())
},
}

func init() {
rootCmd.AddCommand(showLastBranchCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// showLastBranchCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// showLastBranchCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

0 comments on commit d7d9261

Please sign in to comment.