-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Make git push options accept short name #32245
Merged
+149
−44
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not using len of push options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why using len of push options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better set a length when we know the max length of a slice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But there could be far more push options than it really needs here.
At most, here it only needs 2, but there could be 4 or 5 or even more push options. They are not related.