Skip to content

Commit

Permalink
cmd: merge kong-upstream and improve short-only
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Dec 26, 2024
1 parent 37c98d1 commit b852beb
Show file tree
Hide file tree
Showing 49 changed files with 582 additions and 330 deletions.
2 changes: 1 addition & 1 deletion cmd/zeta-mc/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type App struct {
Globals
From string `arg:"" name:"from" help:"Original repository remote URL (or filesystem path)" type:"string"`
Destination string `arg:"" optional:"" name:"destination" help:"Destination where the repository is migrated" type:"path"`
Values []string `short:"X" name:":config" help:"Override default configuration, format: <key>=<value>"`
Values []string `short:"X" shortonly:"" help:"Override default configuration, format: <key>=<value>"`
Squeeze bool `name:"squeeze" short:"s" help:"Squeeze mode, compressed metadata"`
LFS bool `name:"lfs" help:"Migrate all LFS objects to zeta"`
Quiet bool `name:"quiet" help:"Operate quietly. Progress is not reported to the standard error stream"`
Expand Down
131 changes: 131 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package cli

import (
"fmt"
"os"
"testing"

"github.com/antgroup/hugescm/pkg/kong"
"github.com/antgroup/hugescm/pkg/tr"
"github.com/antgroup/hugescm/pkg/version"
)

type Checkout struct {
UnresolvedArgs []string `arg:"" optional:""`
Branch string `name:"branch" short:"b" help:"Direct the new HEAD to the <name> branch after checkout"`
TagName string `name:"tag" short:"t" help:"Direct the new HEAD to the <name> tag's commit after checkout"`
Commit string `name:"commit" help:"Direct the new HEAD to the <commit> branch after checkout"`
Sparse []string `name:"sparse" short:"s" help:"A subset of repository files, all files are checked out by default" type:"string"`
Limit int64 `name:"limit" short:"L" help:"Omits blobs larger than n bytes or units. n may be zero. supported units: KB,MB,GB,K,M,G" default:"-1" type:"size"`
Batch bool `name:"batch" help:"Get and checkout files for each provided on stdin"`
Snapshot bool `name:"snapshot" help:"Checkout a non-editable snapshot"`
Depth int `name:"depth" help:"Create a shallow clone with a history truncated to the specified number of commits" default:"1"`
One bool `name:"one" help:"Checkout large files one after another"`
Quiet bool `name:"quiet" help:"Operate quietly. Progress is not reported to the standard error stream"`
passthroughArgs []string `kong:"-"`
}

func (c *Checkout) Passthrough(paths []string) {
c.passthroughArgs = append(c.passthroughArgs, paths...)
}

func (c *Checkout) Run() error {
fmt.Fprintf(os.Stderr, "unresolvedArgs: %v passthroughArgs: %v\n", c.UnresolvedArgs, c.passthroughArgs)
return nil
}

type Diff struct {
NoIndex bool `name:"no-index" help:"Compares two given paths on the filesystem"`
NameOnly bool `name:"name-only" help:"Show only names of changed files"`
NameStatus bool `name:"name-status" help:"Show names and status of changed files"`
Numstat bool `name:"numstat" help:"Show numeric diffstat instead of patch"`
Stat bool `name:"stat" help:"Show diffstat instead of patch"`
Shortstat bool `name:"shortstat" help:"Output only the last line of --stat format"`
Z bool `short:"z" shortonly:"" help:"Output diff-raw with lines terminated with NUL"`
Staged bool `name:"staged" help:"Compare the differences between the staging area and <revision>"`
Cached bool `name:"cached" help:"Compare the differences between the staging area and <revision>"`
Textconv bool `name:"textconv" help:"Converting text to Unicode"`
MergeBase string `name:"merge-base" help:"If --merge-base is given, use the common ancestor of <commit> and HEAD instead"`
Histogram bool `name:"histogram" help:"Generate a diff using the \"Histogram diff\" algorithm"`
ONP bool `name:"onp" help:"Generate a diff using the \"O(NP) diff\" algorithm"`
Myers bool `name:"myers" help:"Generate a diff using the \"Myers diff\" algorithm"`
Patience bool `name:"patience" help:"Generate a diff using the \"Patience diff\" algorithm"`
Minimal bool `name:"minimal" help:"Spend extra time to make sure the smallest possible diff is produced"`
DiffAlgorithm string `name:"diff-algorithm" help:"Choose a diff algorithm, supported: histogram|onp|myers|patience|minimal" placeholder:"<algorithm>"`
Output string `name:"output" help:"Output to a specific file instead of stdout" placeholder:"<file>"`
From string `arg:"" optional:"" name:"from" help:""`
To string `arg:"" optional:"" name:"to" help:""`
passthroughArgs []string `kong:"-"`
}

func (c *Diff) Passthrough(paths []string) {
c.passthroughArgs = append(c.passthroughArgs, paths...)
}

func (c *Diff) Run() error {
fmt.Fprintf(os.Stderr, "from %s to %s args: %v\n", c.From, c.To, c.passthroughArgs)
return nil
}

type App struct {
Checkout Checkout `cmd:"" name:"co" help:"checkout"`
Diff Diff `cmd:"" name:"diff" help:"diff"`
}

func TestCheckout(t *testing.T) {
parseArgs := func(args []string) {
var app App
ctx := kong.ParseArgs(&app, args,
kong.Name("zeta"),
kong.Description(tr.W("HugeSCM - A next generation cloud-based version control system")),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
NoExpandSubcommands: true,
}),
kong.Vars{
"version": version.GetVersionString(),
},
)
if err := ctx.Run(); err != nil {
return
}
}
argss := [][]string{
{"co", "--", "a.txt", "b.txt"},
{"co", "master", "--", "a.txt", "b.txt"},
{"co", ".", "--", "a.txt", "b.txt"},
{"co", ".", "--", "a.txt", "b.txt", "--"},
}
for _, args := range argss {
parseArgs(args)
}
}

func TestDiff(t *testing.T) {
parseArgs := func(args []string) {
var app App
ctx := kong.ParseArgs(&app, args,
kong.Name("zeta"),
kong.Description(tr.W("HugeSCM - A next generation cloud-based version control system")),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
NoExpandSubcommands: true,
}),
kong.Vars{
"version": version.GetVersionString(),
},
)
if err := ctx.Run(); err != nil {
return
}
}
argss := [][]string{
{"diff", "--", "a.txt", "b.txt"},
{"diff", "master", "--", "a.txt", "b.txt"},
}
for _, args := range argss {
parseArgs(args)
}
}
2 changes: 1 addition & 1 deletion modules/strengthen/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const (
)

var pool = sync.Pool{
New: func() interface{} {
New: func() any {
// Size of buffer chosen somewhat arbitrarily to accommodate a large number of path strings.
// MAX_PATH (260) + size of volume GUID prefix (49) + null terminator = 310.
b := make([]uint16, 310)
Expand Down
4 changes: 2 additions & 2 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
type Globals struct {
Verbose bool `short:"V" name:"verbose" help:"Make the operation more talkative"`
Version VersionFlag `short:"v" name:"version" help:"Show version number and quit"`
Values []string `short:"X" name:":config" help:"Override default configuration, format: <key>=<value>"`
CWD string `name:"cwd" help:"Set the path to the repository worktree"`
Values []string `short:"X" shortonly:"" help:"Override default configuration, format: <key>=<value>"`
CWD string `name:"cwd" help:"Set the path to the repository worktree" placeholder:"<worktree>"`
}

func (g *Globals) DbgPrint(format string, args ...any) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/command/command_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ type Branch struct {
ShowCurrent bool `name:"show-current" help:"Show current branch name"`
List bool `name:"list" short:"l" help:"List branches. With optional <pattern>..."`
Copy bool `name:"copy" short:"c" help:"Copy a branch and its reflog"`
ForceCopy bool `name:":force-copy" short:"C" help:"Copy a branch, even if target exists"`
ForceCopy bool `short:"C" shortonly:"" help:"Copy a branch, even if target exists"`
Delete bool `name:"delete" short:"d" help:"Delete fully merged branch"`
ForceDelete bool `name:":force-delete" short:"D" help:"Delete branch (even if not merged)"`
ForceDelete bool `short:"D" shortonly:"" help:"Delete branch (even if not merged)"`
Move bool `name:"move" short:"m" help:"Move/rename a branch and its reflog"`
ForceMove bool `name:":force-move" short:"M" help:"Move/rename a branch, even if target exists"`
ForceMove bool `short:"M" shortonly:"" help:"Move/rename a branch, even if target exists"`
Force bool `name:"force" short:"f" help:"Force creation, move/rename, deletion"`
Args []string `arg:"" optional:"" name:"args" help:"Branch args: <branchname>, <pattern>, <start-point>"`
Args []string `arg:"" optional:"" name:"args" help:""`
}

const (
Expand Down
22 changes: 11 additions & 11 deletions pkg/command/command_cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
)

type Cat struct {
Object string `arg:"" name:"object" help:"The name of the object to show"`
T bool `name:"type" short:"t" help:"Show object type"`
DisplaySize bool `name:":" short:"s" help:"Show object size"`
JSON bool `name:"json" short:"j" help:"Returns data as JSON; limited to commits, trees, fragments, and tags"`
Textconv bool `name:"textconv" help:"Converting text to Unicode"`
Direct bool `name:"direct" help:"View files directly"`
Verify bool `name:"verify" help:"Verify object hash"`
Limit int64 `name:"limit" short:"L" help:"Omits blobs larger than n bytes or units. n may be zero. supported units: KB,MB,GB,K,M,G" default:"-1" type:"size"`
Output string `name:"output" help:"Output to a specific file instead of stdout" placeholder:"<file>"`
Object string `arg:"" name:"object" help:"The name of the object to show"`
Type bool `name:"type" short:"t" help:"Show object type"`
Size bool `name:"size" short:"s" help:"Show object size"`
Verify bool `name:"verify" help:"Verify object hash"`
Textconv bool `name:"textconv" help:"Converting text to Unicode"`
JSON bool `name:"json" short:"j" help:"Returns data as JSON; limited to commits, trees, fragments, and tags"`
Direct bool `name:"direct" help:"View files directly"`
Limit int64 `name:"limit" short:"L" help:"Omits blobs larger than n bytes or units. n may be zero. supported units: KB,MB,GB,K,M,G" default:"-1" type:"size"`
Output string `name:"output" help:"Output to a specific file instead of stdout" placeholder:"<file>"`
}

func (c *Cat) Run(g *Globals) error {
Expand All @@ -34,8 +34,8 @@ func (c *Cat) Run(g *Globals) error {
return r.Cat(context.Background(), &zeta.CatOptions{
Object: c.Object,
Limit: c.Limit,
Type: c.T,
PrintSize: c.DisplaySize,
Type: c.Type,
PrintSize: c.Size,
Textconv: c.Textconv,
Direct: c.Direct,
PrintJSON: c.JSON,
Expand Down
2 changes: 1 addition & 1 deletion pkg/command/command_check_ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type CheckIgnore struct {
Stdin bool `name:"stdin" help:"Read file names from stdin"`
Z bool `name:":z" short:"z" help:"Terminate input and output records by a NUL character"`
Z bool `short:"z" shortonly:"" help:"Terminate input and output records by a NUL character"`
JSON bool `name:"json" short:"j" help:"Data will be returned in JSON format"`
Paths []string `arg:"" name:"pathname" optional:"" help:"Pathname given via the command-line"`
}
Expand Down
48 changes: 24 additions & 24 deletions pkg/command/command_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@ import (
"github.com/antgroup/hugescm/pkg/zeta"
)

const (
coSummaryFormat = `%szeta checkout (co) [--branch|--tag] [--commit] [--sparse] [--limit] <url> [<destination>]
%szeta checkout (co) <branch>
%szeta checkout (co) [<branch>] -- <file>...
%szeta checkout (co) --batch [<branch>]
%szeta checkout (co) <something> [<paths>]`
)

type Checkout struct {
UnresolvedArgs []string `arg:"" optional:"" hidden:""`
Branch string `name:"branch" short:"b" help:"Direct the new HEAD to the <name> branch after checkout"`
TagName string `name:"tag" short:"t" help:"Direct the new HEAD to the <name> tag's commit after checkout"`
Commit string `name:"commit" help:"Direct the new HEAD to the <commit> branch after checkout"`
Sparse []string `name:"sparse" short:"s" help:"A subset of repository files, all files are checked out by default" type:"string"`
Args []string `arg:"" optional:""`
Branch string `name:"branch" short:"b" help:"Direct the new HEAD to the <name> branch after checkout" placeholder:"<branch>"`
TagName string `name:"tag" short:"t" help:"Direct the new HEAD to the <name> tag's commit after checkout" placeholder:"<tag>"`
Commit string `name:"commit" help:"Direct the new HEAD to the <commit> branch after checkout" placeholder:"<commit>"`
Sparse []string `name:"sparse" short:"s" help:"A subset of repository files, all files are checked out by default" placeholder:"<dir>"`
Limit int64 `name:"limit" short:"L" help:"Omits blobs larger than n bytes or units. n may be zero. supported units: KB,MB,GB,K,M,G" default:"-1" type:"size"`
Batch bool `name:"batch" help:"Get and checkout files for each provided on stdin"`
Snapshot bool `name:"snapshot" help:"Checkout a non-editable snapshot"`
Expand All @@ -36,6 +28,14 @@ type Checkout struct {
passthroughArgs []string `kong:"-"`
}

const (
coSummaryFormat = `%szeta checkout (co) [--branch|--tag] [--commit] [--sparse] [--limit] <url> [<destination>]
%szeta checkout (co) <branch>
%szeta checkout (co) [<branch>] -- <file>...
%szeta checkout (co) --batch [<branch>]
%szeta checkout (co) <something> [<paths>]`
)

func (c *Checkout) Summary() string {
or := W(" or: ")
return fmt.Sprintf(coSummaryFormat, W("Usage: "), or, or, or, or)
Expand Down Expand Up @@ -82,15 +82,15 @@ func (c *Checkout) doRemote(g *Globals, remote, destination string) error {
}

func (c *Checkout) destination() string {
if len(c.UnresolvedArgs) >= 2 {
return c.UnresolvedArgs[1]
if len(c.Args) >= 2 {
return c.Args[1]
}
return ""
}

func (c *Checkout) revision() string {
if len(c.UnresolvedArgs) != 0 {
return c.UnresolvedArgs[0]
if len(c.Args) != 0 {
return c.Args[0]
}
return "HEAD"
}
Expand Down Expand Up @@ -127,9 +127,9 @@ func (c *Checkout) runCompatibleCheckout0(g *Globals, r *zeta.Repository, worktr
}

func (c *Checkout) runCompatibleCheckout(g *Globals, r *zeta.Repository) error {
pathSpec := make([]string, 0, len(c.UnresolvedArgs))
pathSpec := make([]string, 0, len(c.Args))
// zeta checkout <something> [<paths>]
if len(c.UnresolvedArgs) == 0 {
if len(c.Args) == 0 {
pathSpec = append(pathSpec, c.passthroughArgs...)
head, err := r.Current()
if err != nil {
Expand All @@ -138,9 +138,9 @@ func (c *Checkout) runCompatibleCheckout(g *Globals, r *zeta.Repository) error {
}
return c.runCompatibleCheckout0(g, r, true, head.Name(), head.Hash(), pathSpec)
}
rev, refname, err := r.RevisionEx(context.Background(), c.UnresolvedArgs[0])
rev, refname, err := r.RevisionEx(context.Background(), c.Args[0])
if zeta.IsErrUnknownRevision(err) {
pathSpec = append(pathSpec, c.UnresolvedArgs...)
pathSpec = append(pathSpec, c.Args...)
pathSpec = append(pathSpec, c.passthroughArgs...)
head, err := r.Current()
if err != nil {
Expand All @@ -155,7 +155,7 @@ func (c *Checkout) runCompatibleCheckout(g *Globals, r *zeta.Repository) error {
}
// zeta checkout <something> [<paths>]
g.DbgPrint("resolve revision: %s", rev)
pathSpec = append(pathSpec, c.UnresolvedArgs[1:]...)
pathSpec = append(pathSpec, c.Args[1:]...)
pathSpec = append(pathSpec, c.passthroughArgs...)
var worktreeOnly bool
if len(pathSpec) != 0 {
Expand All @@ -165,8 +165,8 @@ func (c *Checkout) runCompatibleCheckout(g *Globals, r *zeta.Repository) error {
}

func (c *Checkout) Run(g *Globals) error {
if len(c.UnresolvedArgs) > 0 && transport.IsRemoteEndpoint(c.UnresolvedArgs[0]) {
return c.doRemote(g, c.UnresolvedArgs[0], c.destination())
if len(c.Args) > 0 && transport.IsRemoteEndpoint(c.Args[0]) {
return c.doRemote(g, c.Args[0], c.destination())
}
r, err := zeta.Open(context.Background(), &zeta.OpenOptions{
Worktree: g.CWD,
Expand Down
4 changes: 2 additions & 2 deletions pkg/command/command_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
type Clean struct {
DryRun bool `name:"dry-run" short:"n" help:"dry run"`
Force bool `name:"force" short:"f" help:"force"`
Dir bool `name:":dir" short:"d" help:"Remove whole directories"`
ALL bool `name:":all" short:"x" help:"Remove ignored files, too"`
Dir bool `short:"d" shortonly:"" help:"Remove whole directories"`
ALL bool `short:"x" shortonly:"" help:"Remove ignored files, too"`
}

func (c *Clean) Run(g *Globals) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/command/command_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

type Commit struct {
Message []string `name:"message" short:"m" help:"Use the given as the commit message. Concatenate multiple -m options as separate paragraphs"`
File string `name:"file" short:"F" help:"Take the commit message from the given file. Use - to read the message from the standard input"`
Message []string `name:"message" short:"m" help:"Use the given as the commit message. Concatenate multiple -m options as separate paragraphs" placeholder:"<message>"`
File string `name:"file" short:"F" help:"Take the commit message from the given file. Use - to read the message from the standard input" placeholder:"<file>"`
All bool `name:"all" short:"a" help:"Automatically stage modified and deleted files, but newly untracked files remain unaffected"`
AllowEmpty bool `name:"allow-empty" help:"Allow creating a commit with the exact same tree structure as its parent commit"`
AllowEmptyMessage bool `name:"allow-empty-message" help:"Like --allow-empty this command is primarily for use by foreign SCM interface scripts"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/command/command_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type Config struct {
Get bool `name:"get" help:"Get the value for a given Key"`
GetALL bool `name:"get-all" help:"Get all values for a given Key"`
Add bool `name:"add" help:"Add a new variable: name value"`
Z bool `name:":z" short:"z" help:"Terminate values with NUL byte"`
Type string `name:"type" short:"T" help:"zeta config will ensure that any input or output is valid under the given type constraint(s), support: bool, int, float, date"`
Z bool `short:"z" shortonly:"" help:"Terminate values with NUL byte"`
Type string `name:"type" short:"T" help:"zeta config will ensure that any input or output is valid under the given type constraint(s), support: bool, int, float, date" placeholder:"<type>"`
}

func (c *Config) Run(g *Globals) error {
Expand Down
Loading

0 comments on commit b852beb

Please sign in to comment.