From d7d926118e06af45b648cb2348286bd7b783c091 Mon Sep 17 00:00:00 2001 From: Patrick Organ Date: Sat, 11 Nov 2023 10:58:04 -0500 Subject: [PATCH] wip --- .custom-hooks/pre-commit | 4 ++-- app/helpers/git-helpers.go | 39 +++++++++++++++++++++++++++++++++++++ cmd/showLastBranch.go | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 app/helpers/git-helpers.go create mode 100644 cmd/showLastBranch.go diff --git a/.custom-hooks/pre-commit b/.custom-hooks/pre-commit index a52b476..e0863a5 100755 --- a/.custom-hooks/pre-commit +++ b/.custom-hooks/pre-commit @@ -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" @@ -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" diff --git a/app/helpers/git-helpers.go b/app/helpers/git-helpers.go new file mode 100644 index 0000000..aee556d --- /dev/null +++ b/app/helpers/git-helpers.go @@ -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") +} diff --git a/cmd/showLastBranch.go b/cmd/showLastBranch.go new file mode 100644 index 0000000..ec1780f --- /dev/null +++ b/cmd/showLastBranch.go @@ -0,0 +1,40 @@ +/* +Copyright © 2023 NAME HERE +*/ +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") +}