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

Check if name or email are present on global level before adding to git locally #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions make.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,26 @@ func runGitCommandIn(dir string, arg ...string) error {
return cmd.Run()
}

func execGitConfig(args ...string) (string, error) {
gitArgs := append([]string{"config", "--get", "--null"}, args...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment as to why --null is required here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will. Per git-config manpage, it "allows for secure parsing of the output without getting confused e.g. by values that contain line breaks." Thank you!

var stdout bytes.Buffer
cmd := exec.Command("git", gitArgs...)
cmd.Stdout = &stdout
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of dealing with the buffer manually, use cmd.Output() instead of cmd.Run() below.

cmd.Stderr = ioutil.Discard
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems redundant — not setting cmd.Stderr has the same effect, no?


err := cmd.Run()
if exitError, ok := err.(*exec.ExitError); ok {
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
if waitStatus.ExitStatus() == 1 {
return "", &ErrNotFound{Key: args[len(args)-1]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don’t have the ErrNotFound type, so just return err here

}
}
return "", err
}

return strings.TrimRight(stdout.String(), "\000"), nil
}

func createGitRepository(debsrc, gopkg, orig string) (string, error) {
wd, err := os.Getwd()
if err != nil {
Expand All @@ -293,13 +313,23 @@ func createGitRepository(debsrc, gopkg, orig string) (string, error) {
return dir, err
}

if debianName := getDebianName(); debianName != "TODO" {
gitName, err := execGitConfig("--global", "user.name")
if err {
return dir, err
}

if debianName := getDebianName(); debianName != "TODO" && gitName != debianName {
if err := runGitCommandIn(dir, "config", "user.name", debianName); err != nil {
return dir, err
}
}

if debianEmail := getDebianEmail(); debianEmail != "TODO" {
gitEmail, err := execGitConfig("--global", "user.email")
if err {
return dir, err
}

if debianEmail := getDebianEmail(); debianEmail != "TODO" && gitEmail != debianEmail {
if err := runGitCommandIn(dir, "config", "user.email", debianEmail); err != nil {
return dir, err
}
Expand Down