forked from Gitlink/gitlink-cli
173 lines
5.3 KiB
Go
173 lines
5.3 KiB
Go
package commit
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
||
)
|
||
|
||
func Shortcuts() []*common.Shortcut {
|
||
return []*common.Shortcut{
|
||
// +list:列出仓库提交记录
|
||
{
|
||
Name: "list",
|
||
Description: "列出仓库提交记录",
|
||
Long: `List repository commits.
|
||
|
||
Shows commit SHA, author, message, and date.
|
||
Use --page and --limit for pagination.
|
||
Filter by path, date range, or branch with respective flags.`,
|
||
Example: ` # List recent commits
|
||
gitlink commit +list
|
||
|
||
# List commits on a specific branch
|
||
gitlink commit +list --sha develop
|
||
|
||
# List commits touching a specific file
|
||
gitlink commit +list --path src/main.go`,
|
||
Flags: []common.Flag{
|
||
{Name: "sha", Short: "s", Usage: "Branch or SHA to list commits from"},
|
||
{Name: "path", Short: "p", Usage: "Filter commits by file path"},
|
||
{Name: "page", Usage: "Page number", Default: "1"},
|
||
{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
|
||
{Name: "since", Usage: "Only commits after this date (ISO 8601)"},
|
||
{Name: "until", Usage: "Only commits before this date (ISO 8601)"},
|
||
},
|
||
Run: func(ctx *common.RuntimeContext) error {
|
||
if err := ctx.ResolveOwnerRepo(); err != nil {
|
||
return err
|
||
}
|
||
path := fmt.Sprintf("/v1/%s/%s/commits", ctx.Owner, ctx.Repo)
|
||
env, err := ctx.CallAPIWithQuery("GET", path, nil)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return ctx.Output(env)
|
||
},
|
||
},
|
||
// +view:查看提交详情(含变更文件)
|
||
{
|
||
Name: "view",
|
||
Description: "查看提交详情(含变更文件)",
|
||
Long: `View commit details including changed files.
|
||
|
||
Shows the full commit information and the list of files changed
|
||
in the specified commit.`,
|
||
Example: ` # View a commit
|
||
gitlink commit +view --sha abc123def456`,
|
||
Flags: []common.Flag{
|
||
{Name: "sha", Short: "s", Usage: "Commit SHA", Required: true},
|
||
},
|
||
Run: func(ctx *common.RuntimeContext) error {
|
||
if err := ctx.ResolveOwnerRepo(); err != nil {
|
||
return err
|
||
}
|
||
sha, err := ctx.RequireArg("sha")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
env, err := ctx.CallAPI("GET", fmt.Sprintf("/v1/%s/%s/commits/%s/files", ctx.Owner, ctx.Repo, sha), nil)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return ctx.Output(env)
|
||
},
|
||
},
|
||
// +diff:查看提交的 diff 内容
|
||
{
|
||
Name: "diff",
|
||
Description: "查看提交的 diff 内容",
|
||
Long: `View the diff of a commit.
|
||
|
||
Returns the patch/diff content for the specified commit,
|
||
showing exact line-level additions and deletions.`,
|
||
Example: ` # View the diff of a commit
|
||
gitlink commit +diff --sha abc123def456`,
|
||
Flags: []common.Flag{
|
||
{Name: "sha", Short: "s", Usage: "Commit SHA", Required: true},
|
||
},
|
||
Run: func(ctx *common.RuntimeContext) error {
|
||
if err := ctx.ResolveOwnerRepo(); err != nil {
|
||
return err
|
||
}
|
||
sha, err := ctx.RequireArg("sha")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
env, err := ctx.CallAPI("GET", fmt.Sprintf("/v1/%s/%s/commits/%s/diff", ctx.Owner, ctx.Repo, sha), nil)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return ctx.Output(env)
|
||
},
|
||
},
|
||
// +blame:获取文件的逐行提交追溯信息
|
||
{
|
||
Name: "blame",
|
||
Description: "获取文件的逐行提交追溯信息",
|
||
Long: `Get blame information for a file.
|
||
|
||
Shows which commit and author last modified each line of a file.
|
||
Use --page and --limit for pagination.`,
|
||
Example: ` # Blame a file on the default branch
|
||
gitlink commit +blame --path src/main.go
|
||
|
||
# Blame a file at a specific commit
|
||
gitlink commit +blame --path src/main.go --sha abc123def`,
|
||
Flags: []common.Flag{
|
||
{Name: "path", Short: "p", Usage: "File path", Required: true},
|
||
{Name: "sha", Short: "s", Usage: "Commit SHA or branch"},
|
||
{Name: "page", Usage: "Page number", Default: "1"},
|
||
{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
|
||
},
|
||
Run: func(ctx *common.RuntimeContext) error {
|
||
if err := ctx.ResolveOwnerRepo(); err != nil {
|
||
return err
|
||
}
|
||
apiPath := fmt.Sprintf("/v1/%s/%s/blame", ctx.Owner, ctx.Repo)
|
||
env, err := ctx.CallAPIWithQuery("GET", apiPath, nil)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return ctx.Output(env)
|
||
},
|
||
},
|
||
// +compare:比较两个引用(分支、标签或 SHA)
|
||
{
|
||
Name: "compare",
|
||
Description: "比较两个引用(分支、标签或 SHA)",
|
||
Long: `Compare two refs (branches, tags, or SHAs).
|
||
|
||
Returns the commits and file changes between a base and head ref.
|
||
Both --base and --head are required.`,
|
||
Example: ` # Compare two branches
|
||
gitlink commit +compare --base main --head develop
|
||
|
||
# Compare two commit SHAs
|
||
gitlink commit +compare --base abc123 --head def456`,
|
||
Flags: []common.Flag{
|
||
{Name: "head", Short: "H", Usage: "Head ref", Required: true},
|
||
{Name: "base", Short: "B", Usage: "Base ref", Required: true},
|
||
},
|
||
Run: func(ctx *common.RuntimeContext) error {
|
||
if err := ctx.ResolveOwnerRepo(); err != nil {
|
||
return err
|
||
}
|
||
head, err := ctx.RequireArg("head")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
base, err := ctx.RequireArg("base")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
env, err := ctx.CallAPI("GET", fmt.Sprintf("/v1/%s/%s/compare/%s...%s", ctx.Owner, ctx.Repo, base, head), nil)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return ctx.Output(env)
|
||
},
|
||
},
|
||
}
|
||
}
|