diff options
author | vince <vincetiu8@gmail.com> | 2020-07-09 14:59:47 +0800 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2021-02-14 12:17:48 +0100 |
commit | d96284da646cc1d3e3d7d3b2f7a1ab0e8e7a4d88 (patch) | |
tree | 032869cd4c3ff959a42b7489795cc2cd8afc6423 /bug/comment.go | |
parent | 2788c5fc87507974d3237d4edc233fda3f784b35 (diff) | |
download | git-bug-d96284da646cc1d3e3d7d3b2f7a1ab0e8e7a4d88.tar.gz |
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.
Diffstat (limited to 'bug/comment.go')
-rw-r--r-- | bug/comment.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/bug/comment.go b/bug/comment.go index 4c9d118e..1a9ca05a 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()) |