Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate random commit message #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Run this program.


For example, commit 10 commits max every day for two years,
and avoid committing on Saturday and Sunday:
and avoid committing on Saturday and Sunday, with the random commit message:

```
go run main.go -max=10 -skip-weekend=true
go run main.go -max=10 -skip-weekend=true -rand-msg
```
24 changes: 23 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,42 @@ package main
import (
"flag"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os/exec"
"time"
)

const WhatTheCommit = "https://whatthecommit.com/index.txt"

func init() {
rand.Seed(time.Now().UnixNano())
}

var (
max int
skipWeekend bool
randomMsg bool
years int
)

// getRandomMsg returns random commit message
func getRandomMsg() string {
resp, err := http.Get(WhatTheCommit)
if err != nil {
return "something wrong"
}

body, _ := io.ReadAll(resp.Body)
return string(body)
}

func main() {
flag.IntVar(&max, "max", 5, "Max number of commits per day")
flag.BoolVar(&skipWeekend, "skip-weekend", true, "Set false if you don't want to skip weekend")
flag.BoolVar(&randomMsg, "random-msg", false, "Set random commit message, it may slow down the program")
flag.IntVar(&years, "years", 2, "Number of years to commit")
flag.Parse()

Expand All @@ -39,12 +56,17 @@ func main() {
log.Printf("Committing %d times on %s", commits, d)

for i := 0; i < commits; i++ {
msg := fmt.Sprintf("Commit from %s: ", d.Format("2006-01-02"))
if randomMsg {
msg = getRandomMsg()
}

cmd := exec.Command(
"git",
"commit",
"--allow-empty",
"--date", d.Format("Mon Jan 02 15:04:05 -0700 2006"),
"-m", fmt.Sprintf("Commit from %s", d.Format("2006-01-02")))
"-m", msg)
if err := cmd.Run(); err != nil {
log.Printf("Error when committing: %v", err)
}
Expand Down