From 6696610aea48482b6c2f258cc43f595d550e7477 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 28 May 2014 22:15:15 -0400 Subject: [PATCH] Fix zombie --- CONTRIBUTING.md | 2 -- models/git_diff.go | 41 +++++++++++++++++++---------------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 86f9b8e9b..0a8b26f1c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,8 +2,6 @@ > Thanks [drone](https://github.com/drone/drone) because this guidelines sheet is forked from its [CONTRIBUTING.md](https://github.com/drone/drone/blob/master/CONTRIBUTING.md). -**This document is pre^2 release, we're not ready for receiving contribution until v0.5.0 release.** - Want to hack on Gogs? Awesome! Here are instructions to get you started. They are probably not perfect, please let us know if anything feels wrong or incomplete. ## Contribution guidelines diff --git a/models/git_diff.go b/models/git_diff.go index 8dd5a8c88..5b5a46a12 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -67,7 +67,7 @@ func (diff *Diff) NumFiles() int { const DIFF_HEAD = "diff --git " -func ParsePatch(reader io.Reader) (*Diff, error) { +func ParsePatch(cmd *exec.Cmd, reader io.Reader) (*Diff, error) { scanner := bufio.NewScanner(reader) var ( curFile *DiffFile @@ -168,6 +168,13 @@ func ParsePatch(reader io.Reader) (*Diff, error) { } } + // In case process became zombie. + if !cmd.ProcessState.Exited() { + log.Debug("git_diff.ParsePatch: process doesn't exit and now will be killed") + if err := cmd.Process.Kill(); err != nil { + log.Error("git_diff.ParsePatch: fail to kill zombie process: %v", err) + } + } return diff, nil } @@ -182,33 +189,23 @@ func GetDiff(repoPath, commitid string) (*Diff, error) { return nil, err } + rd, wr := io.Pipe() + var cmd *exec.Cmd // First commit of repository. if commit.ParentCount() == 0 { - rd, wr := io.Pipe() - go func() { - cmd := exec.Command("git", "show", commitid) - cmd.Dir = repoPath - cmd.Stdout = wr - cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr - cmd.Run() - wr.Close() - }() - defer rd.Close() - return ParsePatch(rd) + cmd = exec.Command("git", "show", commitid) + } else { + c, _ := commit.Parent(0) + cmd = exec.Command("git", "diff", c.Id.String(), commitid) } - - rd, wr := io.Pipe() + cmd.Dir = repoPath + cmd.Stdout = wr + cmd.Stdin = os.Stdin + cmd.Stderr = os.Stderr go func() { - c, _ := commit.Parent(0) - cmd := exec.Command("git", "diff", c.Id.String(), commitid) - cmd.Dir = repoPath - cmd.Stdout = wr - cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr cmd.Run() wr.Close() }() defer rd.Close() - return ParsePatch(rd) + return ParsePatch(cmd, rd) }