-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit with GIT_AUTHOR, GIT_COMMITTER env config, --message, -m (#23)
- Loading branch information
1 parent
e9ded3e
commit 588b3aa
Showing
4 changed files
with
120 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package config | ||
|
||
import "path/filepath" | ||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
const ( | ||
DefaultGitDirectory = ".git" | ||
|
@@ -11,6 +15,7 @@ const ( | |
DefaultRefsDirectory = "refs" | ||
DefaultRefsHeadsDirectory = "heads" | ||
DefaultBranch = "refs/heads/main" | ||
DefaultEditor = "vim" | ||
) | ||
|
||
var Config Cnf | ||
|
@@ -26,6 +31,8 @@ type ( | |
RefsHeadsDirectory string | ||
DefaultBranch string | ||
GitIgnore []string | ||
Editor string | ||
EditorArgs []string | ||
} | ||
Opt func(m *Cnf) error | ||
) | ||
|
@@ -58,6 +65,7 @@ func Configure(opts ...Opt) error { | |
RefsDirectory: DefaultRefsDirectory, | ||
RefsHeadsDirectory: DefaultRefsHeadsDirectory, | ||
DefaultBranch: DefaultBranch, | ||
Editor: DefaultEditor, | ||
GitIgnore: []string{ //@todo read from .gitignore | ||
".idea/", | ||
}, | ||
|
@@ -113,3 +121,39 @@ func GitHeadPath() string { | |
func Pager() (string, []string) { | ||
return "/usr/bin/less", []string{"-X", "-F"} | ||
} | ||
|
||
func Editor() (string, []string) { | ||
return Config.Editor, Config.EditorArgs | ||
} | ||
|
||
func EditorFile() string { | ||
return fmt.Sprintf("%s/COMMIT_EDITMSG", GitPath()) | ||
} | ||
|
||
func AuthorName() string { | ||
if v, ok := os.LookupEnv("GIT_AUTHOR_NAME"); ok { | ||
return v | ||
} | ||
return "default" | ||
} | ||
|
||
func AuthorEmail() string { | ||
if v, ok := os.LookupEnv("GIT_AUTHOR_EMAIL"); ok { | ||
return v | ||
} | ||
return "[email protected]" | ||
} | ||
|
||
func CommitterName() string { | ||
if v, ok := os.LookupEnv("GIT_COMMITTER_NAME"); ok { | ||
return v | ||
} | ||
return AuthorName() | ||
} | ||
|
||
func CommitterEmail() string { | ||
if v, ok := os.LookupEnv("GIT_COMMITTER_EMAIL"); ok { | ||
return v | ||
} | ||
return AuthorEmail() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
@@ -138,7 +139,7 @@ func LsFiles() ([]string, error) { | |
} | ||
|
||
// Commit writes a git commit object from the files in the index | ||
func Commit() ([]byte, error) { | ||
func Commit(message []byte) ([]byte, error) { | ||
idx, err := index.ReadIndex() | ||
if err != nil { | ||
return nil, err | ||
|
@@ -155,17 +156,40 @@ func Commit() ([]byte, error) { | |
// @todo error types to check for e.g no previous commits as source of error | ||
return nil, err | ||
} | ||
return objects.WriteCommit( | ||
&objects.Commit{ | ||
Tree: tree, | ||
Parents: previousCommits, | ||
Author: "Richard Jennings <[email protected]>", | ||
AuthoredTime: time.Now(), | ||
Committer: "Richard Jennings <[email protected]>", | ||
CommittedTime: time.Now(), | ||
Message: []byte("test"), | ||
}, | ||
) | ||
commit := &objects.Commit{ | ||
Tree: tree, | ||
Parents: previousCommits, | ||
Author: fmt.Sprintf("%s <%s>", config.AuthorName(), config.AuthorEmail()), | ||
AuthoredTime: time.Now(), | ||
Committer: fmt.Sprintf("%s <%s>", config.CommitterName(), config.CommitterEmail()), | ||
CommittedTime: time.Now(), | ||
} | ||
if message != nil { | ||
commit.Message = message | ||
} else { | ||
// empty commit file | ||
if err := os.WriteFile(config.EditorFile(), []byte{}, 0600); err != nil { | ||
log.Fatalln(err) | ||
} | ||
ed, args := config.Editor() | ||
args = append(args, config.EditorFile()) | ||
cmd := exec.Command(ed, args...) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
err = cmd.Run() | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
msg, err := os.ReadFile(args[0]) | ||
if err != nil { | ||
log.Fatalln(msg) | ||
} | ||
commit.Message = msg | ||
} | ||
if len(commit.Message) == 0 { | ||
return nil, errors.New("Aborting commit due to empty commit message.") | ||
} | ||
return objects.WriteCommit(commit) | ||
} | ||
|
||
// Status currently displays the file statuses comparing the working directory | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters