diff options
author | Runxi Yu <me@runxiyu.org> | 2025-04-06 08:35:58 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-04-06 08:35:58 +0800 |
commit | 7daf45b862b8f53ecabc969615437994edb49fe8 (patch) | |
tree | 1cc369adae791ecb5130d8d9cd2307ebb639cc34 /internal/git2c/cmd_index.go | |
parent | Makefile: Silence that bunch of mkdir and cp (diff) | |
download | forge-0.1.31.tar.gz forge-0.1.31.tar.zst forge-0.1.31.zip |
git2c, git2d: Rename cmd1 and cmd2 descriptivelyv0.1.31
Diffstat (limited to 'internal/git2c/cmd_index.go')
-rw-r--r-- | internal/git2c/cmd_index.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/internal/git2c/cmd_index.go b/internal/git2c/cmd_index.go new file mode 100644 index 0000000..a705a63 --- /dev/null +++ b/internal/git2c/cmd_index.go @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> + +package git2c + +import ( + "encoding/hex" + "errors" + "fmt" + "io" +) + +func (c *Client) CmdIndex(repoPath string) ([]Commit, *FilenameContents, error) { + if err := c.writer.WriteData([]byte(repoPath)); err != nil { + return nil, nil, fmt.Errorf("sending repo path failed: %w", err) + } + if err := c.writer.WriteUint(1); err != nil { + return nil, nil, fmt.Errorf("sending command failed: %w", err) + } + + status, err := c.reader.ReadUint() + if err != nil { + return nil, nil, fmt.Errorf("reading status failed: %w", err) + } + if status != 0 { + return nil, nil, fmt.Errorf("git2d error: %d", status) + } + + // README + readmeRaw, err := c.reader.ReadData() + if err != nil { + readmeRaw = nil + } + + readmeFilename := "README.md" // TODO + readme := &FilenameContents{Filename: readmeFilename, Content: readmeRaw} + + // Commits + var commits []Commit + for { + id, err := c.reader.ReadData() + if err != nil { + if errors.Is(err, io.EOF) { + break + } + return nil, nil, fmt.Errorf("reading commit ID failed: %w", err) + } + title, _ := c.reader.ReadData() + authorName, _ := c.reader.ReadData() + authorEmail, _ := c.reader.ReadData() + authorDate, _ := c.reader.ReadData() + + commits = append(commits, Commit{ + Hash: hex.EncodeToString(id), + Author: string(authorName), + Email: string(authorEmail), + Date: string(authorDate), + Message: string(title), + }) + } + + return commits, readme, nil +} |