-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make git push options accept short name (#32245)
Just like what most CLI parsers do: `--opt` means `opt=true` Then users could use `-o force-push` as `-o force-push=true`
- Loading branch information
1 parent
900ac62
commit afa8dd4
Showing
7 changed files
with
149 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package private | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/optional" | ||
) | ||
|
||
// GitPushOptions is a wrapper around a map[string]string | ||
type GitPushOptions map[string]string | ||
|
||
// GitPushOptions keys | ||
const ( | ||
GitPushOptionRepoPrivate = "repo.private" | ||
GitPushOptionRepoTemplate = "repo.template" | ||
GitPushOptionForcePush = "force-push" | ||
) | ||
|
||
// Bool checks for a key in the map and parses as a boolean | ||
// An option without value is considered true, eg: "-o force-push" or "-o repo.private" | ||
func (g GitPushOptions) Bool(key string) optional.Option[bool] { | ||
if val, ok := g[key]; ok { | ||
if val == "" { | ||
return optional.Some(true) | ||
} | ||
if b, err := strconv.ParseBool(val); err == nil { | ||
return optional.Some(b) | ||
} | ||
} | ||
return optional.None[bool]() | ||
} | ||
|
||
// AddFromKeyValue adds a key value pair to the map by "key=value" format or "key" for empty value | ||
func (g GitPushOptions) AddFromKeyValue(line string) { | ||
kv := strings.SplitN(line, "=", 2) | ||
if len(kv) == 2 { | ||
g[kv[0]] = kv[1] | ||
} else { | ||
g[kv[0]] = "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package private | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGitPushOptions(t *testing.T) { | ||
o := GitPushOptions{} | ||
|
||
v := o.Bool("no-such") | ||
assert.False(t, v.Has()) | ||
assert.False(t, v.Value()) | ||
|
||
o.AddFromKeyValue("opt1=a=b") | ||
o.AddFromKeyValue("opt2=false") | ||
o.AddFromKeyValue("opt3=true") | ||
o.AddFromKeyValue("opt4") | ||
|
||
assert.Equal(t, "a=b", o["opt1"]) | ||
assert.False(t, o.Bool("opt1").Value()) | ||
assert.True(t, o.Bool("opt2").Has()) | ||
assert.False(t, o.Bool("opt2").Value()) | ||
assert.True(t, o.Bool("opt3").Value()) | ||
assert.True(t, o.Bool("opt4").Value()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package integration | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"fmt" | ||
|
@@ -943,3 +944,59 @@ func TestDataAsync_Issue29101(t *testing.T) { | |
defer r2.Close() | ||
}) | ||
} | ||
|
||
func TestAgitPullPush(t *testing.T) { | ||
onGiteaRun(t, func(t *testing.T, u *url.URL) { | ||
baseAPITestContext := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) | ||
|
||
u.Path = baseAPITestContext.GitPath() | ||
u.User = url.UserPassword("user2", userPassword) | ||
|
||
dstPath := t.TempDir() | ||
doGitClone(dstPath, u)(t) | ||
|
||
gitRepo, err := git.OpenRepository(context.Background(), dstPath) | ||
assert.NoError(t, err) | ||
defer gitRepo.Close() | ||
|
||
doGitCreateBranch(dstPath, "test-agit-push") | ||
|
||
// commit 1 | ||
_, err = generateCommitWithNewData(littleSize, dstPath, "[email protected]", "User Two", "branch-data-file-") | ||
assert.NoError(t, err) | ||
|
||
// push to create an agit pull request | ||
err = git.NewCommand(git.DefaultContext, "push", "origin", | ||
"-o", "title=test-title", "-o", "description=test-description", | ||
"HEAD:refs/for/master/test-agit-push", | ||
).Run(&git.RunOpts{Dir: dstPath}) | ||
assert.NoError(t, err) | ||
|
||
// check pull request exist | ||
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{BaseRepoID: 1, Flow: issues_model.PullRequestFlowAGit, HeadBranch: "user2/test-agit-push"}) | ||
assert.NoError(t, pr.LoadIssue(db.DefaultContext)) | ||
assert.Equal(t, "test-title", pr.Issue.Title) | ||
assert.Equal(t, "test-description", pr.Issue.Content) | ||
|
||
// commit 2 | ||
_, err = generateCommitWithNewData(littleSize, dstPath, "[email protected]", "User Two", "branch-data-file-2-") | ||
assert.NoError(t, err) | ||
|
||
// push 2 | ||
err = git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master/test-agit-push").Run(&git.RunOpts{Dir: dstPath}) | ||
assert.NoError(t, err) | ||
|
||
// reset to first commit | ||
err = git.NewCommand(git.DefaultContext, "reset", "--hard", "HEAD~1").Run(&git.RunOpts{Dir: dstPath}) | ||
assert.NoError(t, err) | ||
|
||
// test force push without confirm | ||
_, stderr, err := git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master/test-agit-push").RunStdString(&git.RunOpts{Dir: dstPath}) | ||
assert.Error(t, err) | ||
assert.Contains(t, stderr, "[remote rejected] HEAD -> refs/for/master/test-agit-push (request `force-push` push option)") | ||
|
||
// test force push with confirm | ||
err = git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master/test-agit-push", "-o", "force-push").Run(&git.RunOpts{Dir: dstPath}) | ||
assert.NoError(t, err) | ||
}) | ||
} |