From ab57d74a312f325b9d889752aa92c00c395de20f Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 8 Nov 2020 19:18:44 +0100 Subject: deal with the previous changes --- doc/md/git-bug_user.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'doc/md') diff --git a/doc/md/git-bug_user.md b/doc/md/git-bug_user.md index d9388def2..302a1eda8 100644 --- a/doc/md/git-bug_user.md +++ b/doc/md/git-bug_user.md @@ -9,7 +9,7 @@ git-bug user [USER-ID] [flags] ### Options ``` - -f, --field string Select field to display. Valid values are [email,humanId,id,lastModification,lastModificationLamport,login,metadata,name] + -f, --field string Select field to display. Valid values are [email,humanId,id,lastModification,lastModificationLamports,login,metadata,name] -h, --help help for user ``` -- cgit v1.2.3 From d96284da646cc1d3e3d7d3b2f7a1ab0e8e7a4d88 Mon Sep 17 00:00:00 2001 From: vince Date: Thu, 9 Jul 2020 14:59:47 +0800 Subject: Change the comment ID to use both bug and comment ID references. Add comment edit command This commit adds the comment edit command, which provides a CLI tool that allows a user to edit a comment. --- bug/comment.go | 46 ++++++++++++++++++++++++ bug/comment_test.go | 27 +++++++++++++++ bug/op_add_comment.go | 5 +-- bug/op_create.go | 5 +-- bug/snapshot.go | 5 +++ cache/repo_cache_bug.go | 47 +++++++++++++++++++++++++ commands/comment.go | 1 + commands/comment_edit.go | 71 ++++++++++++++++++++++++++++++++++++++ commands/show.go | 3 +- doc/man/git-bug-comment-edit.1 | 35 +++++++++++++++++++ doc/man/git-bug-comment.1 | 2 +- doc/md/git-bug_comment.md | 1 + doc/md/git-bug_comment_edit.md | 20 +++++++++++ misc/bash_completion/git-bug | 33 ++++++++++++++++++ misc/powershell_completion/git-bug | 8 +++++ 15 files changed, 303 insertions(+), 6 deletions(-) create mode 100644 bug/comment_test.go create mode 100644 commands/comment_edit.go create mode 100644 doc/man/git-bug-comment-edit.1 create mode 100644 doc/md/git-bug_comment_edit.md (limited to 'doc/md') diff --git a/bug/comment.go b/bug/comment.go index 4c9d118eb..1a9ca05af 100644 --- a/bug/comment.go +++ b/bug/comment.go @@ -1,6 +1,8 @@ package bug import ( + "strings" + "github.com/dustin/go-humanize" "github.com/MichaelMure/git-bug/entity" @@ -31,6 +33,50 @@ func (c Comment) Id() entity.Id { return c.id } +const compiledCommentIdFormat = "BCBCBCBBBCBBBBCBBBBCBBBBCBBBBCBBBBCBBBBC" + +// DeriveCommentId compute a merged Id for a comment holding information from +// both the Bug's Id and the Comment's Id. This allow to later find efficiently +// a Comment because we can access the bug directly instead of searching for a +// Bug that has a Comment matching the Id. +// +// To allow the use of an arbitrary length prefix of this merged Id, Ids from Bug +// and Comment are interleaved with this irregular pattern to give the best chance +// to find the Comment even with a 7 character prefix. +// +// A complete merged Id hold 30 characters for the Bug and 10 for the Comment, +// which give a key space of 36^30 for the Bug (~5 * 10^46) and 36^10 for the +// Comment (~3 * 10^15). This asymmetry assume a reasonable number of Comment +// within a Bug, while still allowing for a vast key space for Bug (that is, a +// globally merged bug database) with a low risk of collision. +func DeriveCommentId(bugId entity.Id, commentId entity.Id) entity.Id { + var id strings.Builder + for _, char := range compiledCommentIdFormat { + if char == 'B' { + id.WriteByte(bugId[0]) + bugId = bugId[1:] + } else { + id.WriteByte(commentId[0]) + commentId = commentId[1:] + } + } + return entity.Id(id.String()) +} + +func SplitCommentId(prefix string) (bugPrefix string, commentPrefix string) { + var bugIdPrefix strings.Builder + var commentIdPrefix strings.Builder + + for i, char := range prefix { + if compiledCommentIdFormat[i] == 'B' { + bugIdPrefix.WriteRune(char) + } else { + commentIdPrefix.WriteRune(char) + } + } + return bugIdPrefix.String(), commentIdPrefix.String() +} + // FormatTimeRel format the UnixTime of the comment for human consumption func (c Comment) FormatTimeRel() string { return humanize.Time(c.UnixTime.Time()) diff --git a/bug/comment_test.go b/bug/comment_test.go new file mode 100644 index 000000000..423d10d8f --- /dev/null +++ b/bug/comment_test.go @@ -0,0 +1,27 @@ +package bug + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/MichaelMure/git-bug/entity" +) + +func TestCommentId(t *testing.T) { + bugId := entity.Id("abcdefghijklmnopqrstuvwxyz1234__________") + opId := entity.Id("ABCDEFGHIJ______________________________") + expectedId := entity.Id("aAbBcCdefDghijEklmnFopqrGstuvHwxyzI1234J") + + mergedId := DeriveCommentId(bugId, opId) + require.Equal(t, expectedId, mergedId) + + // full length + splitBugId, splitCommentId := SplitCommentId(mergedId.String()) + require.Equal(t, string(bugId[:30]), splitBugId) + require.Equal(t, string(opId[:10]), splitCommentId) + + splitBugId, splitCommentId = SplitCommentId(string(expectedId[:6])) + require.Equal(t, string(bugId[:3]), splitBugId) + require.Equal(t, string(opId[:3]), splitCommentId) +} diff --git a/bug/op_add_comment.go b/bug/op_add_comment.go index 3f19e42e7..df426ee0c 100644 --- a/bug/op_add_comment.go +++ b/bug/op_add_comment.go @@ -36,8 +36,9 @@ func (op *AddCommentOperation) Apply(snapshot *Snapshot) { snapshot.addActor(op.Author) snapshot.addParticipant(op.Author) + commentId := DeriveCommentId(snapshot.Id(), op.Id()) comment := Comment{ - id: op.Id(), + id: commentId, Message: op.Message, Author: op.Author, Files: op.Files, @@ -47,7 +48,7 @@ func (op *AddCommentOperation) Apply(snapshot *Snapshot) { snapshot.Comments = append(snapshot.Comments, comment) item := &AddCommentTimelineItem{ - CommentTimelineItem: NewCommentTimelineItem(op.Id(), comment), + CommentTimelineItem: NewCommentTimelineItem(commentId, comment), } snapshot.Timeline = append(snapshot.Timeline, item) diff --git a/bug/op_create.go b/bug/op_create.go index 41e0fca19..15fb69b55 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -59,8 +59,9 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) { snapshot.Title = op.Title + commentId := DeriveCommentId(snapshot.Id(), op.Id()) comment := Comment{ - id: op.Id(), + id: commentId, Message: op.Message, Author: op.Author, UnixTime: timestamp.Timestamp(op.UnixTime), @@ -72,7 +73,7 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) { snapshot.Timeline = []TimelineItem{ &CreateTimelineItem{ - CommentTimelineItem: NewCommentTimelineItem(op.Id(), comment), + CommentTimelineItem: NewCommentTimelineItem(commentId, comment), }, } } diff --git a/bug/snapshot.go b/bug/snapshot.go index 11df04b21..0005b9301 100644 --- a/bug/snapshot.go +++ b/bug/snapshot.go @@ -28,6 +28,11 @@ type Snapshot struct { // Return the Bug identifier func (snap *Snapshot) Id() entity.Id { + if snap.id == "" { + // simply panic as it would be a coding error + // (using an id of a bug not stored yet) + panic("no id yet") + } return snap.id } diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 1701f66d0..cfcbb72d6 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -261,6 +261,53 @@ func (c *RepoCache) resolveBugMatcher(f func(*BugExcerpt) bool) (entity.Id, erro return matching[0], nil } +// ResolveComment search for a Bug/Comment combination matching the merged +// bug/comment Id prefix. Returns the Bug containing the Comment and the Comment's +// Id. +func (c *RepoCache) ResolveComment(prefix string) (*BugCache, entity.Id, error) { + bugPrefix, _ := bug.SplitCommentId(prefix) + bugCandidate := make([]entity.Id, 0, 5) + + // build a list of possible matching bugs + c.muBug.RLock() + for _, excerpt := range c.bugExcerpts { + if excerpt.Id.HasPrefix(bugPrefix) { + bugCandidate = append(bugCandidate, excerpt.Id) + } + } + c.muBug.RUnlock() + + matchingBugIds := make([]entity.Id, 0, 5) + matchingCommentId := entity.UnsetId + var matchingBug *BugCache + + // search for matching comments + // searching every bug candidate allow for some collision with the bug prefix only, + // before being refined with the full comment prefix + for _, bugId := range bugCandidate { + b, err := c.ResolveBug(bugId) + if err != nil { + return nil, entity.UnsetId, err + } + + for _, comment := range b.Snapshot().Comments { + if comment.Id().HasPrefix(prefix) { + matchingBugIds = append(matchingBugIds, bugId) + matchingBug = b + matchingCommentId = comment.Id() + } + } + } + + if len(matchingBugIds) > 1 { + return nil, entity.UnsetId, entity.NewErrMultipleMatch("bug/comment", matchingBugIds) + } else if len(matchingBugIds) == 0 { + return nil, entity.UnsetId, errors.New("comment doesn't exist") + } + + return matchingBug, matchingCommentId, nil +} + // QueryBugs return the id of all Bug matching the given Query func (c *RepoCache) QueryBugs(q *query.Query) ([]entity.Id, error) { c.muBug.RLock() diff --git a/commands/comment.go b/commands/comment.go index d8995c3ed..eb90624aa 100644 --- a/commands/comment.go +++ b/commands/comment.go @@ -22,6 +22,7 @@ func newCommentCommand() *cobra.Command { } cmd.AddCommand(newCommentAddCommand()) + cmd.AddCommand(newCommentEditCommand()) return cmd } diff --git a/commands/comment_edit.go b/commands/comment_edit.go new file mode 100644 index 000000000..61132967b --- /dev/null +++ b/commands/comment_edit.go @@ -0,0 +1,71 @@ +package commands + +import ( + "github.com/spf13/cobra" + + "github.com/MichaelMure/git-bug/input" +) + +type commentEditOptions struct { + messageFile string + message string +} + +func newCommentEditCommand() *cobra.Command { + env := newEnv() + options := commentEditOptions{} + + cmd := &cobra.Command{ + Use: "edit ", + Short: "Edit an existing comment on a bug.", + Args: cobra.ExactArgs(1), + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), + RunE: func(cmd *cobra.Command, args []string) error { + return runCommentEdit(env, options, args) + }, + } + + flags := cmd.Flags() + flags.SortFlags = false + + flags.StringVarP(&options.messageFile, "file", "F", "", + "Take the message from the given file. Use - to read the message from the standard input") + + flags.StringVarP(&options.message, "message", "m", "", + "Provide the new message from the command line") + + return cmd +} + +func runCommentEdit(env *Env, opts commentEditOptions, args []string) error { + b, commentId, err := env.backend.ResolveComment(args[0]) + if err != nil { + return err + } + + if opts.messageFile != "" && opts.message == "" { + opts.message, err = input.BugCommentFileInput(opts.messageFile) + if err != nil { + return err + } + } + + if opts.messageFile == "" && opts.message == "" { + opts.message, err = input.BugCommentEditorInput(env.backend, "") + if err == input.ErrEmptyMessage { + env.err.Println("Empty message, aborting.") + return nil + } + if err != nil { + return err + } + } + + _, err = b.EditComment(commentId, opts.message) + if err != nil { + return err + } + + return b.Commit() +} diff --git a/commands/show.go b/commands/show.go index 9ebd1926d..10087f927 100644 --- a/commands/show.go +++ b/commands/show.go @@ -158,8 +158,9 @@ func showDefaultFormatter(env *Env, snapshot *bug.Snapshot) error { for i, comment := range snapshot.Comments { var message string - env.out.Printf("%s#%d %s <%s>\n\n", + env.out.Printf("%s%s #%d %s <%s>\n\n", indent, + comment.Id().Human(), i, comment.Author.DisplayName(), comment.Author.Email(), diff --git a/doc/man/git-bug-comment-edit.1 b/doc/man/git-bug-comment-edit.1 new file mode 100644 index 000000000..e3cb2daf0 --- /dev/null +++ b/doc/man/git-bug-comment-edit.1 @@ -0,0 +1,35 @@ +.nh +.TH "GIT\-BUG" "1" "Apr 2019" "Generated from git\-bug's source code" "" + +.SH NAME +.PP +git\-bug\-comment\-edit \- Edit an existing comment on a bug. + + +.SH SYNOPSIS +.PP +\fBgit\-bug comment edit [flags]\fP + + +.SH DESCRIPTION +.PP +Edit an existing comment on a bug. + + +.SH OPTIONS +.PP +\fB\-F\fP, \fB\-\-file\fP="" + Take the message from the given file. Use \- to read the message from the standard input + +.PP +\fB\-m\fP, \fB\-\-message\fP="" + Provide the new message from the command line + +.PP +\fB\-h\fP, \fB\-\-help\fP[=false] + help for edit + + +.SH SEE ALSO +.PP +\fBgit\-bug\-comment(1)\fP diff --git a/doc/man/git-bug-comment.1 b/doc/man/git-bug-comment.1 index 7cad5a0d4..cb0740bb8 100644 --- a/doc/man/git-bug-comment.1 +++ b/doc/man/git-bug-comment.1 @@ -24,4 +24,4 @@ Display or add comments to a bug. .SH SEE ALSO .PP -\fBgit\-bug(1)\fP, \fBgit\-bug\-comment\-add(1)\fP +\fBgit\-bug(1)\fP, \fBgit\-bug\-comment\-add(1)\fP, \fBgit\-bug\-comment\-edit(1)\fP diff --git a/doc/md/git-bug_comment.md b/doc/md/git-bug_comment.md index 6ac7c45ba..48050a979 100644 --- a/doc/md/git-bug_comment.md +++ b/doc/md/git-bug_comment.md @@ -16,4 +16,5 @@ git-bug comment [ID] [flags] * [git-bug](git-bug.md) - A bug tracker embedded in Git. * [git-bug comment add](git-bug_comment_add.md) - Add a new comment to a bug. +* [git-bug comment edit](git-bug_comment_edit.md) - Edit an existing comment on a bug. diff --git a/doc/md/git-bug_comment_edit.md b/doc/md/git-bug_comment_edit.md new file mode 100644 index 000000000..2546dff16 --- /dev/null +++ b/doc/md/git-bug_comment_edit.md @@ -0,0 +1,20 @@ +## git-bug comment edit + +Edit an existing comment on a bug. + +``` +git-bug comment edit [flags] +``` + +### Options + +``` + -F, --file string Take the message from the given file. Use - to read the message from the standard input + -m, --message string Provide the new message from the command line + -h, --help help for edit +``` + +### SEE ALSO + +* [git-bug comment](git-bug_comment.md) - Display or add comments to a bug. + diff --git a/misc/bash_completion/git-bug b/misc/bash_completion/git-bug index 912e87b48..b3103e885 100644 --- a/misc/bash_completion/git-bug +++ b/misc/bash_completion/git-bug @@ -722,6 +722,38 @@ _git-bug_comment_add() noun_aliases=() } +_git-bug_comment_edit() +{ + last_command="git-bug_comment_edit" + + command_aliases=() + + commands=() + + flags=() + two_word_flags=() + local_nonpersistent_flags=() + flags_with_completion=() + flags_completion=() + + flags+=("--file=") + two_word_flags+=("--file") + two_word_flags+=("-F") + local_nonpersistent_flags+=("--file") + local_nonpersistent_flags+=("--file=") + local_nonpersistent_flags+=("-F") + flags+=("--message=") + two_word_flags+=("--message") + two_word_flags+=("-m") + local_nonpersistent_flags+=("--message") + local_nonpersistent_flags+=("--message=") + local_nonpersistent_flags+=("-m") + + must_have_one_flag=() + must_have_one_noun=() + noun_aliases=() +} + _git-bug_comment() { last_command="git-bug_comment" @@ -730,6 +762,7 @@ _git-bug_comment() commands=() commands+=("add") + commands+=("edit") flags=() two_word_flags=() diff --git a/misc/powershell_completion/git-bug b/misc/powershell_completion/git-bug index 29c282378..5ff155156 100644 --- a/misc/powershell_completion/git-bug +++ b/misc/powershell_completion/git-bug @@ -118,6 +118,7 @@ Register-ArgumentCompleter -Native -CommandName 'git-bug' -ScriptBlock { } 'git-bug;comment' { [CompletionResult]::new('add', 'add', [CompletionResultType]::ParameterValue, 'Add a new comment to a bug.') + [CompletionResult]::new('edit', 'edit', [CompletionResultType]::ParameterValue, 'Edit an existing comment on a bug.') break } 'git-bug;comment;add' { @@ -127,6 +128,13 @@ Register-ArgumentCompleter -Native -CommandName 'git-bug' -ScriptBlock { [CompletionResult]::new('--message', 'message', [CompletionResultType]::ParameterName, 'Provide the new message from the command line') break } + 'git-bug;comment;edit' { + [CompletionResult]::new('-F', 'F', [CompletionResultType]::ParameterName, 'Take the message from the given file. Use - to read the message from the standard input') + [CompletionResult]::new('--file', 'file', [CompletionResultType]::ParameterName, 'Take the message from the given file. Use - to read the message from the standard input') + [CompletionResult]::new('-m', 'm', [CompletionResultType]::ParameterName, 'Provide the new message from the command line') + [CompletionResult]::new('--message', 'message', [CompletionResultType]::ParameterName, 'Provide the new message from the command line') + break + } 'git-bug;deselect' { break } -- cgit v1.2.3 From d0d7be8db010e2c68c98d0a34387e4fac0c4d6ee Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 14 Feb 2021 12:14:03 +0100 Subject: minor cleanups --- .gitignore | 1 + bug/op_edit_comment.go | 2 +- bug/op_label_change.go | 2 +- bug/op_noop.go | 2 +- bug/op_set_metadata.go | 2 +- bug/op_set_status.go | 2 +- bug/op_set_title.go | 2 +- doc/man/git-bug-comment-edit.1 | 2 +- doc/md/git-bug_comment_edit.md | 2 +- entity/err.go | 2 +- 10 files changed, 10 insertions(+), 9 deletions(-) (limited to 'doc/md') diff --git a/.gitignore b/.gitignore index 012dadcd0..be1fc3f1c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ git-bug dist coverage.txt .idea/ +.git_bak* diff --git a/bug/op_edit_comment.go b/bug/op_edit_comment.go index f9e30e62b..e08aeaad9 100644 --- a/bug/op_edit_comment.go +++ b/bug/op_edit_comment.go @@ -96,7 +96,7 @@ func (op *EditCommentOperation) Validate() error { return nil } -// UnmarshalJSON is a two step JSON unmarshaling +// UnmarshalJSON is a two step JSON unmarshalling // This workaround is necessary to avoid the inner OpBase.MarshalJSON // overriding the outer op's MarshalJSON func (op *EditCommentOperation) UnmarshalJSON(data []byte) error { diff --git a/bug/op_label_change.go b/bug/op_label_change.go index 5e51534d6..d682fe54d 100644 --- a/bug/op_label_change.go +++ b/bug/op_label_change.go @@ -95,7 +95,7 @@ func (op *LabelChangeOperation) Validate() error { return nil } -// UnmarshalJSON is a two step JSON unmarshaling +// UnmarshalJSON is a two step JSON unmarshalling // This workaround is necessary to avoid the inner OpBase.MarshalJSON // overriding the outer op's MarshalJSON func (op *LabelChangeOperation) UnmarshalJSON(data []byte) error { diff --git a/bug/op_noop.go b/bug/op_noop.go index d59666ae3..81efdb257 100644 --- a/bug/op_noop.go +++ b/bug/op_noop.go @@ -31,7 +31,7 @@ func (op *NoOpOperation) Validate() error { return op.OpBase.Validate(op, NoOpOp) } -// UnmarshalJSON is a two step JSON unmarshaling +// UnmarshalJSON is a two step JSON unmarshalling // This workaround is necessary to avoid the inner OpBase.MarshalJSON // overriding the outer op's MarshalJSON func (op *NoOpOperation) UnmarshalJSON(data []byte) error { diff --git a/bug/op_set_metadata.go b/bug/op_set_metadata.go index c4988b84f..4e596728c 100644 --- a/bug/op_set_metadata.go +++ b/bug/op_set_metadata.go @@ -49,7 +49,7 @@ func (op *SetMetadataOperation) Validate() error { return nil } -// UnmarshalJSON is a two step JSON unmarshaling +// UnmarshalJSON is a two step JSON unmarshalling // This workaround is necessary to avoid the inner OpBase.MarshalJSON // overriding the outer op's MarshalJSON func (op *SetMetadataOperation) UnmarshalJSON(data []byte) error { diff --git a/bug/op_set_status.go b/bug/op_set_status.go index fcf33f8c5..ca8c434a9 100644 --- a/bug/op_set_status.go +++ b/bug/op_set_status.go @@ -51,7 +51,7 @@ func (op *SetStatusOperation) Validate() error { return nil } -// UnmarshalJSON is a two step JSON unmarshaling +// UnmarshalJSON is a two step JSON unmarshalling // This workaround is necessary to avoid the inner OpBase.MarshalJSON // overriding the outer op's MarshalJSON func (op *SetStatusOperation) UnmarshalJSON(data []byte) error { diff --git a/bug/op_set_title.go b/bug/op_set_title.go index 7fd7c20d1..899b4fa37 100644 --- a/bug/op_set_title.go +++ b/bug/op_set_title.go @@ -71,7 +71,7 @@ func (op *SetTitleOperation) Validate() error { return nil } -// UnmarshalJSON is a two step JSON unmarshaling +// UnmarshalJSON is a two step JSON unmarshalling // This workaround is necessary to avoid the inner OpBase.MarshalJSON // overriding the outer op's MarshalJSON func (op *SetTitleOperation) UnmarshalJSON(data []byte) error { diff --git a/doc/man/git-bug-comment-edit.1 b/doc/man/git-bug-comment-edit.1 index e3cb2daf0..741743a67 100644 --- a/doc/man/git-bug-comment-edit.1 +++ b/doc/man/git-bug-comment-edit.1 @@ -8,7 +8,7 @@ git\-bug\-comment\-edit \- Edit an existing comment on a bug. .SH SYNOPSIS .PP -\fBgit\-bug comment edit [flags]\fP +\fBgit\-bug comment edit [COMMENT\_ID] [flags]\fP .SH DESCRIPTION diff --git a/doc/md/git-bug_comment_edit.md b/doc/md/git-bug_comment_edit.md index 2546dff16..265719277 100644 --- a/doc/md/git-bug_comment_edit.md +++ b/doc/md/git-bug_comment_edit.md @@ -3,7 +3,7 @@ Edit an existing comment on a bug. ``` -git-bug comment edit [flags] +git-bug comment edit [COMMENT_ID] [flags] ``` ### Options diff --git a/entity/err.go b/entity/err.go index 9222e4dac..9f7f5a1a1 100644 --- a/entity/err.go +++ b/entity/err.go @@ -52,7 +52,7 @@ func NewErrUnknowFormat(expected uint) *ErrInvalidFormat { func (e ErrInvalidFormat) Error() string { if e.version == 0 { - return fmt.Sprintf("unreadable data, expected format version %v", e.expected) + return fmt.Sprintf("unreadable data, you likely have an outdated repository format, please use https://github.com/MichaelMure/git-bug-migration to upgrade to format version %v", e.expected) } if e.version < e.expected { return fmt.Sprintf("outdated repository format %v, please use https://github.com/MichaelMure/git-bug-migration to upgrade to format version %v", e.version, e.expected) -- cgit v1.2.3 From ea329aed6909cac85680dbae37f6f4dcca134f8b Mon Sep 17 00:00:00 2001 From: Sascha Date: Sun, 28 Feb 2021 16:27:14 +0100 Subject: Add option to specify host address '--host'-cmdline-option is added to the webui command. Previously, the WebUI couldn't be hosted inside of a container. As the WebUI-server only listend per default to localhost and there was no option to change the address, the server should listend to. This means, that the WebUI was only reachable from localhost. So only from inside of the container but never from outside. The '--host'-option allows to set the IP address or a hostname which the WebUI-server should listen to. E.g. by setting 0.0.0.0 or :: as address. Update documentation for new option. Update shell completion for new option. Compilation seems to add another go-gitlab version. --- commands/webui.go | 8 ++++++-- doc/man/git-bug-webui.1 | 6 +++++- doc/md/git-bug_webui.md | 11 ++++++----- go.sum | 1 + misc/bash_completion/git-bug | 4 ++++ 5 files changed, 22 insertions(+), 8 deletions(-) (limited to 'doc/md') diff --git a/commands/webui.go b/commands/webui.go index 7e5fc7520..d910a7034 100644 --- a/commands/webui.go +++ b/commands/webui.go @@ -4,9 +4,11 @@ import ( "context" "fmt" "log" + "net" "net/http" "os" "os/signal" + "strconv" "time" "github.com/99designs/gqlgen/graphql/playground" @@ -27,6 +29,7 @@ import ( const webUIOpenConfigKey = "git-bug.webui.open" type webUIOptions struct { + host string port int open bool noOpen bool @@ -54,9 +57,10 @@ Available git config: flags := cmd.Flags() flags.SortFlags = false + flags.StringVar(&options.host, "host", "127.0.0.1", "Network address or hostname to listen to (default to 127.0.0.1)") flags.BoolVar(&options.open, "open", false, "Automatically open the web UI in the default browser") flags.BoolVar(&options.noOpen, "no-open", false, "Prevent the automatic opening of the web UI in the default browser") - flags.IntVarP(&options.port, "port", "p", 0, "Port to listen to (default is random)") + flags.IntVarP(&options.port, "port", "p", 0, "Port to listen to (default to random available port)") flags.BoolVar(&options.readOnly, "read-only", false, "Whether to run the web UI in read-only mode") return cmd @@ -71,7 +75,7 @@ func runWebUI(env *Env, opts webUIOptions, args []string) error { } } - addr := fmt.Sprintf("127.0.0.1:%d", opts.port) + addr := net.JoinHostPort(opts.host, strconv.Itoa(opts.port)) webUiAddr := fmt.Sprintf("http://%s", addr) router := mux.NewRouter() diff --git a/doc/man/git-bug-webui.1 b/doc/man/git-bug-webui.1 index 954250a28..6e4622be7 100644 --- a/doc/man/git-bug-webui.1 +++ b/doc/man/git-bug-webui.1 @@ -21,6 +21,10 @@ Available git config: .SH OPTIONS +.PP +\fB\-\-host\fP="127.0.0.1" + Network address or hostname to listen to (default to 127.0.0.1) + .PP \fB\-\-open\fP[=false] Automatically open the web UI in the default browser @@ -31,7 +35,7 @@ Available git config: .PP \fB\-p\fP, \fB\-\-port\fP=0 - Port to listen to (default is random) + Port to listen to (default to random available port) .PP \fB\-\-read\-only\fP[=false] diff --git a/doc/md/git-bug_webui.md b/doc/md/git-bug_webui.md index 98a61eb2b..ccfaff9a6 100644 --- a/doc/md/git-bug_webui.md +++ b/doc/md/git-bug_webui.md @@ -17,11 +17,12 @@ git-bug webui [flags] ### Options ``` - --open Automatically open the web UI in the default browser - --no-open Prevent the automatic opening of the web UI in the default browser - -p, --port int Port to listen to (default is random) - --read-only Whether to run the web UI in read-only mode - -h, --help help for webui + --host string Network address or hostname to listen to (default to 127.0.0.1) (default "127.0.0.1") + --open Automatically open the web UI in the default browser + --no-open Prevent the automatic opening of the web UI in the default browser + -p, --port int Port to listen to (default to random available port) + --read-only Whether to run the web UI in read-only mode + -h, --help help for webui ``` ### SEE ALSO diff --git a/go.sum b/go.sum index 2cfdff75e..b13e190e9 100644 --- a/go.sum +++ b/go.sum @@ -476,6 +476,7 @@ github.com/willf/bitset v1.1.10 h1:NotGKqX0KwQ72NUzqrjZq5ipPNDQex9lo3WpaS8L2sc= github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= github.com/xanzy/go-gitlab v0.40.1 h1:jHueLh5Inzv20TL5Yki+CaLmyvtw3Yq7blbWx7GmglQ= github.com/xanzy/go-gitlab v0.40.1/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug= +github.com/xanzy/go-gitlab v0.44.0 h1:cEiGhqu7EpFGuei2a2etAwB+x6403E5CvpLn35y+GPs= github.com/xanzy/go-gitlab v0.44.0/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= diff --git a/misc/bash_completion/git-bug b/misc/bash_completion/git-bug index 3cedd86a1..8f2a0f8f3 100644 --- a/misc/bash_completion/git-bug +++ b/misc/bash_completion/git-bug @@ -1331,6 +1331,10 @@ _git-bug_webui() flags_with_completion=() flags_completion=() + flags+=("--host=") + two_word_flags+=("--host") + local_nonpersistent_flags+=("--host") + local_nonpersistent_flags+=("--host=") flags+=("--open") local_nonpersistent_flags+=("--open") flags+=("--no-open") -- cgit v1.2.3 From 626ec9835b4450fb2994ec931357279700437b6b Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Sun, 7 Mar 2021 14:09:15 +0100 Subject: webui: allow specifying the initial query Example use-case: given a github URL in a source code comment or commit message, one can now run: git bug webui --query 'metadata:github-url:"https://github.com/author/myproject/issues/42"' on the commandline to look up the details of that issue on the web ui quickly, offline. Fixes . --- commands/webui.go | 8 ++++++++ doc/man/git-bug-webui.1 | 4 ++++ doc/md/git-bug_webui.md | 13 +++++++------ misc/bash_completion/git-bug | 4 ++++ 4 files changed, 23 insertions(+), 6 deletions(-) (limited to 'doc/md') diff --git a/commands/webui.go b/commands/webui.go index d910a7034..3857e968c 100644 --- a/commands/webui.go +++ b/commands/webui.go @@ -6,6 +6,7 @@ import ( "log" "net" "net/http" + "net/url" "os" "os/signal" "strconv" @@ -34,6 +35,7 @@ type webUIOptions struct { open bool noOpen bool readOnly bool + query string } func newWebUICommand() *cobra.Command { @@ -62,6 +64,7 @@ Available git config: flags.BoolVar(&options.noOpen, "no-open", false, "Prevent the automatic opening of the web UI in the default browser") flags.IntVarP(&options.port, "port", "p", 0, "Port to listen to (default to random available port)") flags.BoolVar(&options.readOnly, "read-only", false, "Whether to run the web UI in read-only mode") + flags.StringVar(&options.query, "query", "", "Set a custom query") return cmd } @@ -78,6 +81,11 @@ func runWebUI(env *Env, opts webUIOptions, args []string) error { addr := net.JoinHostPort(opts.host, strconv.Itoa(opts.port)) webUiAddr := fmt.Sprintf("http://%s", addr) + if len(opts.query) > 0 { + // Explicitly set the query parameter instead of going with a default one. + webUiAddr = fmt.Sprintf("%s/?q=%s", webUiAddr, url.QueryEscape(opts.query)) + } + router := mux.NewRouter() // If the webUI is not read-only, use an authentication middleware with a diff --git a/doc/man/git-bug-webui.1 b/doc/man/git-bug-webui.1 index 6e4622be7..6bed91439 100644 --- a/doc/man/git-bug-webui.1 +++ b/doc/man/git-bug-webui.1 @@ -41,6 +41,10 @@ Available git config: \fB\-\-read\-only\fP[=false] Whether to run the web UI in read\-only mode +.PP +\fB\-\-query\fP="" + Set a custom query + .PP \fB\-h\fP, \fB\-\-help\fP[=false] help for webui diff --git a/doc/md/git-bug_webui.md b/doc/md/git-bug_webui.md index ccfaff9a6..0cdfd5175 100644 --- a/doc/md/git-bug_webui.md +++ b/doc/md/git-bug_webui.md @@ -17,12 +17,13 @@ git-bug webui [flags] ### Options ``` - --host string Network address or hostname to listen to (default to 127.0.0.1) (default "127.0.0.1") - --open Automatically open the web UI in the default browser - --no-open Prevent the automatic opening of the web UI in the default browser - -p, --port int Port to listen to (default to random available port) - --read-only Whether to run the web UI in read-only mode - -h, --help help for webui + --host string Network address or hostname to listen to (default to 127.0.0.1) (default "127.0.0.1") + --open Automatically open the web UI in the default browser + --no-open Prevent the automatic opening of the web UI in the default browser + -p, --port int Port to listen to (default to random available port) + --read-only Whether to run the web UI in read-only mode + --query string Set a custom query + -h, --help help for webui ``` ### SEE ALSO diff --git a/misc/bash_completion/git-bug b/misc/bash_completion/git-bug index 8f2a0f8f3..7c7cd975f 100644 --- a/misc/bash_completion/git-bug +++ b/misc/bash_completion/git-bug @@ -1347,6 +1347,10 @@ _git-bug_webui() local_nonpersistent_flags+=("-p") flags+=("--read-only") local_nonpersistent_flags+=("--read-only") + flags+=("--query=") + two_word_flags+=("--query") + local_nonpersistent_flags+=("--query") + local_nonpersistent_flags+=("--query=") must_have_one_flag=() must_have_one_noun=() -- cgit v1.2.3 From 3a819525d7811dcfb01d928af0e243de4388c456 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 7 Mar 2021 21:44:48 +0100 Subject: commands: minor fixes for the webui open with query - go fmt - add a shorthand - fix displayed webUI URL in the terminal --- commands/webui.go | 9 +++++---- doc/man/git-bug-webui.1 | 4 ++-- doc/md/git-bug_webui.md | 2 +- go.sum | 5 ----- misc/bash_completion/git-bug | 2 ++ repository/repo.go | 2 +- 6 files changed, 11 insertions(+), 13 deletions(-) (limited to 'doc/md') diff --git a/commands/webui.go b/commands/webui.go index 3857e968c..2f80bcd03 100644 --- a/commands/webui.go +++ b/commands/webui.go @@ -35,7 +35,7 @@ type webUIOptions struct { open bool noOpen bool readOnly bool - query string + query string } func newWebUICommand() *cobra.Command { @@ -64,7 +64,7 @@ Available git config: flags.BoolVar(&options.noOpen, "no-open", false, "Prevent the automatic opening of the web UI in the default browser") flags.IntVarP(&options.port, "port", "p", 0, "Port to listen to (default to random available port)") flags.BoolVar(&options.readOnly, "read-only", false, "Whether to run the web UI in read-only mode") - flags.StringVar(&options.query, "query", "", "Set a custom query") + flags.StringVarP(&options.query, "query", "q", "", "The query to open in the web UI bug list") return cmd } @@ -80,10 +80,11 @@ func runWebUI(env *Env, opts webUIOptions, args []string) error { addr := net.JoinHostPort(opts.host, strconv.Itoa(opts.port)) webUiAddr := fmt.Sprintf("http://%s", addr) + toOpen := webUiAddr if len(opts.query) > 0 { // Explicitly set the query parameter instead of going with a default one. - webUiAddr = fmt.Sprintf("%s/?q=%s", webUiAddr, url.QueryEscape(opts.query)) + toOpen = fmt.Sprintf("%s/?q=%s", webUiAddr, url.QueryEscape(opts.query)) } router := mux.NewRouter() @@ -162,7 +163,7 @@ func runWebUI(env *Env, opts webUIOptions, args []string) error { shouldOpen := (configOpen && !opts.noOpen) || opts.open if shouldOpen { - err = open.Run(webUiAddr) + err = open.Run(toOpen) if err != nil { env.out.Println(err) } diff --git a/doc/man/git-bug-webui.1 b/doc/man/git-bug-webui.1 index 6bed91439..782cba561 100644 --- a/doc/man/git-bug-webui.1 +++ b/doc/man/git-bug-webui.1 @@ -42,8 +42,8 @@ Available git config: Whether to run the web UI in read\-only mode .PP -\fB\-\-query\fP="" - Set a custom query +\fB\-q\fP, \fB\-\-query\fP="" + The query to open in the web UI bug list .PP \fB\-h\fP, \fB\-\-help\fP[=false] diff --git a/doc/md/git-bug_webui.md b/doc/md/git-bug_webui.md index 0cdfd5175..2d6be9201 100644 --- a/doc/md/git-bug_webui.md +++ b/doc/md/git-bug_webui.md @@ -22,7 +22,7 @@ git-bug webui [flags] --no-open Prevent the automatic opening of the web UI in the default browser -p, --port int Port to listen to (default to random available port) --read-only Whether to run the web UI in read-only mode - --query string Set a custom query + -q, --query string The query to open in the web UI bug list -h, --help help for webui ``` diff --git a/go.sum b/go.sum index b13e190e9..960409299 100644 --- a/go.sum +++ b/go.sum @@ -436,8 +436,6 @@ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasO github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= -github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4= -github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI= github.com/spf13/cobra v1.1.3 h1:xghbfqPkxzxP3C/f3n5DdpAbdKLj4ZE4BWQI362l53M= github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= @@ -474,8 +472,6 @@ github.com/vektah/gqlparser v1.3.1 h1:8b0IcD3qZKWJQHSzynbDlrtP3IxVydZ2DZepCGofqf github.com/vektah/gqlparser v1.3.1/go.mod h1:bkVf0FX+Stjg/MHnm8mEyubuaArhNEqfQhF+OTiAL74= github.com/willf/bitset v1.1.10 h1:NotGKqX0KwQ72NUzqrjZq5ipPNDQex9lo3WpaS8L2sc= github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= -github.com/xanzy/go-gitlab v0.40.1 h1:jHueLh5Inzv20TL5Yki+CaLmyvtw3Yq7blbWx7GmglQ= -github.com/xanzy/go-gitlab v0.40.1/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug= github.com/xanzy/go-gitlab v0.44.0 h1:cEiGhqu7EpFGuei2a2etAwB+x6403E5CvpLn35y+GPs= github.com/xanzy/go-gitlab v0.44.0/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= @@ -791,7 +787,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/misc/bash_completion/git-bug b/misc/bash_completion/git-bug index 7c7cd975f..0bf62d9a1 100644 --- a/misc/bash_completion/git-bug +++ b/misc/bash_completion/git-bug @@ -1349,8 +1349,10 @@ _git-bug_webui() local_nonpersistent_flags+=("--read-only") flags+=("--query=") two_word_flags+=("--query") + two_word_flags+=("-q") local_nonpersistent_flags+=("--query") local_nonpersistent_flags+=("--query=") + local_nonpersistent_flags+=("-q") must_have_one_flag=() must_have_one_noun=() diff --git a/repository/repo.go b/repository/repo.go index eb9296d48..a1dc129ed 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -11,7 +11,7 @@ import ( ) var ( - // ErrNotARepo is the error returned when the git repo root wan't be found + // ErrNotARepo is the error returned when the git repo root can't be found ErrNotARepo = errors.New("not a git repository") // ErrClockNotExist is the error returned when a clock can't be found ErrClockNotExist = errors.New("clock doesn't exist") -- cgit v1.2.3