From ff74f32158930ffa8ac51d9cfb2de705930172de Mon Sep 17 00:00:00 2001 From: Doria Keung Date: Wed, 4 Oct 2023 11:17:38 -0400 Subject: [PATCH] Add `--branch` to `buf push` (#2457) This aliases the `--draft` flag to `--branch` in `buf push`. We are planning to start branch support with parity to `--draft`. The description is also updated for the flag to reflect branch. --- private/buf/cmd/buf/command/push/push.go | 31 ++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/private/buf/cmd/buf/command/push/push.go b/private/buf/cmd/buf/command/push/push.go index 0f1ec9be0e..ac1e065ffb 100644 --- a/private/buf/cmd/buf/command/push/push.go +++ b/private/buf/cmd/buf/command/push/push.go @@ -41,6 +41,7 @@ const ( tagFlagName = "tag" tagFlagShortName = "t" draftFlagName = "draft" + branchFlagName = "branch" errorFormatFlagName = "error-format" disableSymlinksFlagName = "disable-symlinks" createFlagName = "create" @@ -72,6 +73,7 @@ func NewCommand( type flags struct { Tags []string + Branch string Draft string ErrorFormat string DisableSymlinks bool @@ -106,9 +108,21 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) { draftFlagName, "", fmt.Sprintf( - "Make the pushed commit a draft with the specified name. Cannot be used together with --%s (-%s)", + "Make the pushed commit a draft with the specified name. Cannot be used together with --%s (-%s) or --%s", tagFlagName, tagFlagShortName, + branchFlagName, + ), + ) + flagSet.StringVar( + &f.Branch, + branchFlagName, + "", + fmt.Sprintf( + "Push a commit to a branch with the specified name. Cannot be used together with --%s (-%s) or --%s", + tagFlagName, + tagFlagShortName, + draftFlagName, ), ) flagSet.StringVar( @@ -146,9 +160,17 @@ func run( if err := bufcli.ValidateErrorFormatFlag(flags.ErrorFormat, errorFormatFlagName); err != nil { return err } + if flags.Draft != "" && flags.Branch != "" { + return appcmd.NewInvalidArgumentErrorf("--%s and --%s cannot be used together.", draftFlagName, branchFlagName) + } if len(flags.Tags) > 0 && flags.Draft != "" { return appcmd.NewInvalidArgumentErrorf("--%s (-%s) and --%s cannot be used together.", tagFlagName, tagFlagShortName, draftFlagName) } + // For now, we are restricting branch behavior to be exactly the same as drafts, and thus + // cannot be used in conjunction with tags. + if len(flags.Tags) > 0 && flags.Branch != "" { + return appcmd.NewInvalidArgumentErrorf("--%s (-%s) and --%s cannot be used together.", tagFlagName, tagFlagShortName, branchFlagName) + } if flags.CreateVisibility != "" { if !flags.Create { return appcmd.NewInvalidArgumentErrorf("Cannot set --%s without --%s.", createVisibilityFlagName, createFlagName) @@ -257,6 +279,11 @@ func push( if err != nil { return nil, err } + draftOrBranchName := flags.Draft + if draftOrBranchName == "" { + // If draft is not set, then we we set the draft name to branch. + draftOrBranchName = flags.Branch + } resp, err := service.PushManifestAndBlobs( ctx, connect.NewRequest(®istryv1alpha1.PushManifestAndBlobsRequest{ @@ -265,7 +292,7 @@ func push( Manifest: bucketManifest, Blobs: blobs, Tags: flags.Tags, - DraftName: flags.Draft, + DraftName: draftOrBranchName, }), ) if err != nil {