From 8c046073a891e3a192794869628763ef072002eb Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Tue, 1 Sep 2015 19:07:02 -0400
Subject: work on PR conversation

---
 modules/git/repo_pull.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
 create mode 100644 modules/git/repo_pull.go

(limited to 'modules/git/repo_pull.go')

diff --git a/modules/git/repo_pull.go b/modules/git/repo_pull.go
new file mode 100644
index 00000000..a179e528
--- /dev/null
+++ b/modules/git/repo_pull.go
@@ -0,0 +1,86 @@
+// Copyright 2015 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package git
+
+import (
+	"container/list"
+	"fmt"
+	"strings"
+	"time"
+
+	"github.com/Unknwon/com"
+)
+
+type PullRequestInfo struct {
+	MergeBase string
+	Commits   *list.List
+	// Diff      *Diff
+	NumFiles int
+}
+
+// GetPullRequestInfo generates and returns pull request information
+// between base and head branches of repositories.
+func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (*PullRequestInfo, error) {
+	// Add a temporary remote.
+	tmpRemote := com.ToStr(time.Now().UnixNano())
+	_, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "add", "-f", tmpRemote, basePath)
+	if err != nil {
+		return nil, fmt.Errorf("add base as remote: %v", concatenateError(err, stderr))
+	}
+	defer func() {
+		com.ExecCmdDir(repo.Path, "git", "remote", "remove", tmpRemote)
+	}()
+
+	prInfo := new(PullRequestInfo)
+
+	var stdout string
+	remoteBranch := "remotes/" + tmpRemote + "/" + baseBranch
+	// Get merge base commit.
+	stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "merge-base", remoteBranch, headBranch)
+	if err != nil {
+		return nil, fmt.Errorf("get merge base: %v", concatenateError(err, stderr))
+	}
+	prInfo.MergeBase = strings.TrimSpace(stdout)
+
+	stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "log", remoteBranch+"..."+headBranch, prettyLogFormat)
+	if err != nil {
+		return nil, fmt.Errorf("list diff logs: %v", concatenateError(err, stderr))
+	}
+	prInfo.Commits, err = parsePrettyFormatLog(repo, []byte(stdout))
+	if err != nil {
+		return nil, fmt.Errorf("parsePrettyFormatLog: %v", err)
+	}
+
+	// Count number of changed files.
+	stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "diff", "--name-only", remoteBranch+"..."+headBranch)
+	if err != nil {
+		return nil, fmt.Errorf("list changed files: %v", concatenateError(err, stderr))
+	}
+	prInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1
+
+	return prInfo, nil
+}
+
+// GetPatch generates and returns patch data between given branches.
+func (repo *Repository) GetPatch(basePath, baseBranch, headBranch string) ([]byte, error) {
+	// Add a temporary remote.
+	tmpRemote := com.ToStr(time.Now().UnixNano())
+	_, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "remote", "add", "-f", tmpRemote, basePath)
+	if err != nil {
+		return nil, fmt.Errorf("add base as remote: %v", concatenateError(err, string(stderr)))
+	}
+	defer func() {
+		com.ExecCmdDir(repo.Path, "git", "remote", "remove", tmpRemote)
+	}()
+
+	var stdout []byte
+	remoteBranch := "remotes/" + tmpRemote + "/" + baseBranch
+	stdout, stderr, err = com.ExecCmdDirBytes(repo.Path, "git", "diff", "-p", remoteBranch, headBranch)
+	if err != nil {
+		return nil, concatenateError(err, string(stderr))
+	}
+
+	return stdout, nil
+}
-- 
cgit v1.2.3