Ejemplo n.º 1
0
Archivo: blob.go Proyecto: Janfred/gogs
func (b *Blob) Data() (io.Reader, error) {
	stdout, stderr, err := com.ExecCmdDirBytes(b.repo.Path, "git", "show", b.ID.String())
	if err != nil {
		return nil, errors.New(string(stderr))
	}
	return bytes.NewBuffer(stdout), nil
}
Ejemplo n.º 2
0
func (repo *Repository) commitsByRange(id sha1, page int) (*list.List, error) {
	stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", id.String(),
		"--skip="+com.ToStr((page-1)*50), "--max-count=50", prettyLogFormat)
	if err != nil {
		return nil, errors.New(string(stderr))
	}
	return parsePrettyFormatLog(repo, stdout)
}
Ejemplo n.º 3
0
func (repo *Repository) CommitsByFileAndRange(branch, file string, page int) (*list.List, error) {
	stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", branch,
		"--skip="+com.ToStr((page-1)*50), "--max-count=50", prettyLogFormat, "--", file)
	if err != nil {
		return nil, errors.New(string(stderr))
	}
	return parsePrettyFormatLog(repo, stdout)
}
Ejemplo n.º 4
0
// GetPatch generates and returns patch data between given branches.
func (repo *Repository) GetPatch(mergeBase, headBranch string) ([]byte, error) {
	stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "diff", "-p", mergeBase, headBranch)
	if err != nil {
		return nil, concatenateError(err, string(stderr))
	}

	return stdout, nil
}
Ejemplo n.º 5
0
func (repo *Repository) searchCommits(id sha1, keyword string) (*list.List, error) {
	stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", id.String(), "-100",
		"-i", "--grep="+keyword, prettyLogFormat)
	if err != nil {
		return nil, err
	} else if len(stderr) > 0 {
		return nil, errors.New(string(stderr))
	}
	return parsePrettyFormatLog(repo, stdout)
}
Ejemplo n.º 6
0
// 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
}
Ejemplo n.º 7
0
func (t *Tree) ListEntries(relpath string) (Entries, error) {
	if t.entriesParsed {
		return t.entries, nil
	}
	t.entriesParsed = true

	stdout, _, err := com.ExecCmdDirBytes(t.repo.Path,
		"git", "ls-tree", t.Id.String())
	if err != nil {
		return nil, err
	}
	t.entries, err = parseTreeData(t, stdout)
	return t.entries, err
}
Ejemplo n.º 8
0
func (repo *Repository) commitsCount(id sha1) (int, error) {
	if gitVer.LessThan(MustParseVersion("1.8.0")) {
		stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", "--pretty=format:''", id.String())
		if err != nil {
			return 0, errors.New(string(stderr))
		}
		return len(bytes.Split(stdout, []byte("\n"))), nil
	}

	stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "rev-list", "--count", id.String())
	if err != nil {
		return 0, errors.New(stderr)
	}
	return com.StrTo(strings.TrimSpace(stdout)).Int()
}
Ejemplo n.º 9
0
Archivo: tree.go Proyecto: noikiy/gogs
func (t *Tree) ListEntries(relpath string) (Entries, error) {
	if t.entriesParsed {
		return t.entries, nil
	}
	t.entriesParsed = true

	stdout, stderr, err := com.ExecCmdDirBytes(t.repo.Path,
		"git", "ls-tree", t.Id.String())
	if err != nil {
		if strings.Contains(err.Error(), "exit status 128") {
			return nil, errors.New(strings.TrimSpace(string(stderr)))
		}
		return nil, err
	}
	t.entries, err = parseTreeData(t, stdout)
	return t.entries, err
}
Ejemplo n.º 10
0
func (repo *Repository) getTag(id sha1) (*Tag, error) {
	if repo.tagCache != nil {
		if t, ok := repo.tagCache[id]; ok {
			return t, nil
		}
	} else {
		repo.tagCache = make(map[sha1]*Tag, 10)
	}

	// Get tag type.
	tp, stderr, err := com.ExecCmdDir(repo.Path, "git", "cat-file", "-t", id.String())
	if err != nil {
		return nil, errors.New(stderr)
	}
	tp = strings.TrimSpace(tp)

	// Tag is a commit.
	if ObjectType(tp) == COMMIT {
		tag := &Tag{
			Id:     id,
			Object: id,
			Type:   string(COMMIT),
			repo:   repo,
		}
		repo.tagCache[id] = tag
		return tag, nil
	}

	// Tag with message.
	data, bytErr, err := com.ExecCmdDirBytes(repo.Path, "git", "cat-file", "-p", id.String())
	if err != nil {
		return nil, errors.New(string(bytErr))
	}

	tag, err := parseTagData(data)
	if err != nil {
		return nil, err
	}

	tag.Id = id
	tag.repo = repo

	repo.tagCache[id] = tag
	return tag, nil
}
Ejemplo n.º 11
0
func (repo *Repository) getCommit(id sha1) (*Commit, error) {
	if repo.commitCache != nil {
		if c, ok := repo.commitCache[id]; ok {
			return c, nil
		}
	} else {
		repo.commitCache = make(map[sha1]*Commit, 10)
	}

	data, bytErr, err := com.ExecCmdDirBytes(repo.Path, "git", "cat-file", "-p", id.String())
	if err != nil {
		return nil, errors.New(err.Error() + ": " + string(bytErr))
	}

	commit, err := parseCommitData(data)
	if err != nil {
		return nil, err
	}
	commit.repo = repo
	commit.Id = id

	repo.commitCache[id] = commit
	return commit, nil
}