Skip to content

Commit

Permalink
Merge pull request #58 from Banh-Canh/feature/git-flags
Browse files Browse the repository at this point in the history
Feature/git flags --add and --amend
  • Loading branch information
muandane authored Jul 10, 2024
2 parents cdd2b48 + eaa597d commit 6a79fcb
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.test
goji
result
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![codecov](https://codecov.io/gh/muandane/goji/branch/main/graph/badge.svg?token=0PYU31AH2S)](https://codecov.io/gh/muandane/goji) [![Go Report Card](https://goreportcard.com/badge/github.com/muandane/goji)](https://goreportcard.com/report/github.com/muandane/goji)

# goji

<img align="right" src="public/go-gopher.gif">
Expand Down Expand Up @@ -62,6 +63,13 @@ mv goji /usr/local/bin
goji --version
```

**Build with Nix**

```sh
git clone https://github.com/muandane/goji.git && cd goji
nix-build -E 'with import <nixpkgs> {}; callPackage ./default.nix {}'
```

## Usage

Simply run `goji` in your terminal to start the interactive commit process:
Expand All @@ -88,10 +96,10 @@ To use the check command, add the following to your pre-commit hook in your git
- repo: https://github.com/muandane/goji
rev: v0.0.8
hooks:
- id: goji-check
name: goji check
description: >
Check whether the current commit message follows commiting rules. Allow empty commit messages by default, because they typically indicate to Git that the commit should be aborted.
- id: goji-check
name: goji check
description: >
Check whether the current commit message follows commiting rules. Allow empty commit messages by default, because they typically indicate to Git that the commit should be aborted.
```
## Customization
Expand Down Expand Up @@ -125,11 +133,7 @@ You can customize the `.goji.json` generated file to add or change the scopes, t
}
//***
],
"scopes": [
"home",
"accounts",
"ci"
],
"scopes": ["home", "accounts", "ci"],
"noemoji": false,
"skipquestions": [],
"subjectmaxlength": 50,
Expand All @@ -141,14 +145,14 @@ Only `"Scopes"` question can be skipped since it's optional according to the [Co

### Configuration options

| Option | Type | Description |
| ------ | ---- | ----------- |
| `types` | Array of objects | Types for the commit messages (emoji, code, description, name) |
| `scopes` | Array of strings | Optional scopes for the commit messages (you can auto-complete with ctrl+e) |
| `noemoji` | Boolean | Creates commit message with emojis in types |
| `subjectmaxlength` | Number | Maximum length for the description message |
| `signoff` | Boolean | Add a sign off to the end of the commit message |
| `skipquestions` | Array of strings | Skip prompting for these questions (Unimplemented)|
| Option | Type | Description |
| ------------------ | ---------------- | --------------------------------------------------------------------------- |
| `types` | Array of objects | Types for the commit messages (emoji, code, description, name) |
| `scopes` | Array of strings | Optional scopes for the commit messages (you can auto-complete with ctrl+e) |
| `noemoji` | Boolean | Creates commit message with emojis in types |
| `subjectmaxlength` | Number | Maximum length for the description message |
| `signoff` | Boolean | Add a sign off to the end of the commit message |
| `skipquestions` | Array of strings | Skip prompting for these questions (Unimplemented) |

## License

Expand Down
32 changes: 27 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"

"os"

"github.com/alessio/shellescape"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/log"
Expand All @@ -25,6 +24,8 @@ var (
typeFlag string
scopeFlag string
messageFlag string
addFlag bool
amendFlag bool
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -99,7 +100,18 @@ var rootCmd = &cobra.Command{
if noVerifyFlag {
extraArgs = append(extraArgs, "--no-verify")
}
command, commandString := buildCommitCommand(commitMessage, commitBody, signOff, extraArgs)
if addFlag { // Append -a flag if addFlag is true
extraArgs = append(extraArgs, "-a")
}
if amendFlag { // Append --amend flag if amendFlag is true
extraArgs = append(extraArgs, "--amend")
}
command, commandString := buildCommitCommand(
commitMessage,
commitBody,
signOff,
extraArgs,
)
fmt.Println("Executing command:", commandString)
if err := commit(command); err != nil {
log.Fatalf("Error committing changes: %v\n", err)
Expand Down Expand Up @@ -135,8 +147,13 @@ func init() {
rootCmd.Flags().StringVarP(&typeFlag, "type", "t", "", "Specify the type from the config file")
rootCmd.Flags().StringVarP(&scopeFlag, "scope", "s", "", "Specify a custom scope")
rootCmd.Flags().StringVarP(&messageFlag, "message", "m", "", "Specify a commit message")
rootCmd.Flags().BoolVarP(&noVerifyFlag, "no-verify", "n", false, "bypass pre-commit and commit-msg hooks")
rootCmd.Flags().
BoolVarP(&noVerifyFlag, "no-verify", "n", false, "bypass pre-commit and commit-msg hooks")
rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information")
rootCmd.Flags().
BoolVarP(&addFlag, "add", "a", false, "Automatically stage files that have been modified and deleted")
rootCmd.Flags().
BoolVar(&amendFlag, "amend", false, "Change last commit")
}

func Execute() {
Expand All @@ -155,7 +172,12 @@ func Execute() {
//
// Returns:
// - error: an error if the git commit execution fails.
func buildCommitCommand(message string, body string, sign bool, extraArgs []string) ([]string, string) {
func buildCommitCommand(
message string,
body string,
sign bool,
extraArgs []string,
) ([]string, string) {
if sign {
extraArgs = append(extraArgs, "--signoff")
}
Expand Down
34 changes: 34 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
lib,
pkgs,
}:
pkgs.buildGoModule rec {
pname = "goji";
version = "0.1.2";

src = lib.cleanSource ./.;
# pkgs.fetchFromGitHub {
# owner = "muandane";
# repo = "goji";
# rev = "v${version}";
# sha256 = "sha256-QFll5qr+b+bGl2QJ+rQ72FuETBSeqou/gvcvIY3oDIo=";
# };

vendorHash = "sha256-YKnIAviOlLVHaD3lQKhrDlLW1f0cEjY0Az4RyuNWmzg=";

subPackages = ["."];

ldflags = [
"-s"
"-w"
"-X goji/cmd.version=${version}"
];

meta = with lib; {
homepage = "https://github.com/muandane/goji";
description = " Commitizen-like Emoji Commit Tool written in Go (think cz-emoji and other commitizen adapters but in go) 🚀 ";
changelog = "https://github.com/muandane/goji/blob/v${version}/CHANGELOG.md";
license = "Apache 2.0 license Zine El Abidine Moualhi";
mainProgram = "goji";
};
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/muandane/goji

go 1.22.4
go 1.22.0

require (
github.com/alecthomas/assert/v2 v2.10.0
Expand Down

0 comments on commit 6a79fcb

Please sign in to comment.