Skip to content

Commit 9fb4b2f

Browse files
authored
Merge pull request #6123 from unisonweb/cp/history-comment-message
2 parents 7c2f925 + 96a4c17 commit 9fb4b2f

File tree

6 files changed

+133
-31
lines changed

6 files changed

+133
-31
lines changed

unison-cli/src/Unison/Codebase/Editor/HandleInput.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ loop e = do
525525
else BranchEmpty branchEmpty
526526
HistoryI resultsCap diffCap from -> do
527527
handleHistory resultsCap diffCap from
528-
HistoryCommentI toAnnotate -> do
529-
handleHistoryComment toAnnotate
528+
HistoryCommentI toAnnotate message -> do
529+
handleHistoryComment toAnnotate message
530530
IOTestAllI -> Tests.handleAllIOTests
531531
IOTestI main -> Tests.handleIOTest main
532532
LibInstallI remind libdep -> handleInstallLib remind libdep

unison-cli/src/Unison/Codebase/Editor/HandleInput/HistoryComment.hs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import UnliftIO.Directory (findExecutable)
2222
import UnliftIO.Environment qualified as Env
2323
import UnliftIO.Process qualified as Proc
2424

25-
handleHistoryComment :: Maybe BranchId2 -> Cli ()
26-
handleHistoryComment mayThingToAnnotate = do
25+
handleHistoryComment :: Maybe BranchId2 -> Maybe Text -> Cli ()
26+
handleHistoryComment mayThingToAnnotate mayMessage = do
2727
authorName <-
2828
Cli.runTransaction Q.getAuthorName >>= \case
2929
Nothing -> Cli.returnEarly $ AuthorNameRequired
@@ -49,16 +49,24 @@ handleHistoryComment mayThingToAnnotate = do
4949
causalHashId <- Q.expectCausalHashIdByCausalHash causalHash
5050
mayExistingCommentInfo <- Q.getLatestCausalComment causalHashId
5151
pure (causalHashId, mayExistingCommentInfo)
52-
let populatedMsg = fromMaybe commentInstructions $ do
53-
HistoryComment {subject, content} <- mayHistoryComment
54-
pure $ Text.unlines [subject, "", content, commentInstructions]
55-
mayNewMessage <- liftIO (editMessage (Just populatedMsg))
56-
case mayNewMessage of
57-
Nothing -> Cli.respond $ CommentAborted
52+
maySubjectContent <- case mayMessage of
53+
Just msg -> do
54+
let (subject, content) = cleanComment msg
55+
pure $ Just (subject, content)
56+
Nothing -> do
57+
let populatedMsg = fromMaybe commentInstructions $ do
58+
HistoryComment {subject, content} <- mayHistoryComment
59+
pure $ Text.unlines [subject, "", content, commentInstructions]
60+
mayNewMessage <- liftIO (editMessage (Just populatedMsg))
61+
case mayNewMessage of
62+
Nothing -> pure Nothing
63+
Just (subject, content) -> pure $ Just (subject, content)
64+
case maySubjectContent of
5865
Just (subject, content) -> do
5966
let historyComment = HistoryComment {author = Config.unAuthorName authorName, subject, content, commentId = (), causal = causalHashId}
6067
Cli.runTransaction $ Q.commentOnCausal historyComment
6168
Cli.respond $ CommentedSuccessfully
69+
Nothing -> Cli.respond $ CommentAborted
6270
where
6371
commentInstructions =
6472
[r|
@@ -98,15 +106,18 @@ editMessage initialMessage = runMaybeT do
98106
Left _ -> empty
99107
Right () -> pure ()
100108
result <- liftIO (readUtf8 tempFilePath)
101-
let cleanedResult =
102-
result
103-
& Text.lines
104-
& filter (not . Text.isPrefixOf "--")
105-
& Text.unlines
106-
& Text.strip
107-
guard $ not (Text.null cleanedResult)
108-
let (subject, contents) =
109-
case Text.lines cleanedResult of
110-
[] -> ("", "")
111-
(s : rest) -> (Text.strip s, Text.strip $ Text.unlines rest)
109+
let (subject, contents) = cleanComment result
110+
guard $ not (Text.null subject)
112111
pure (subject, contents)
112+
113+
cleanComment :: Text -> (Text, Text)
114+
cleanComment txt =
115+
txt
116+
& Text.lines
117+
& filter (not . Text.isPrefixOf "--")
118+
& Text.unlines
119+
& Text.strip
120+
& Text.lines
121+
& \case
122+
[] -> ("", "")
123+
(s : rest) -> (Text.strip s, Text.strip $ Text.unlines rest)

unison-cli/src/Unison/Codebase/Editor/Input.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ data Input
167167
| ForkLocalBranchI (Either ShortCausalHash BranchRelativePath) BranchRelativePath
168168
| HistoryI (Maybe Int {- cap on number of results -}) (Maybe Int {- cap on diff elements shown -}) BranchId
169169
| -- An optional causal hash or branch to annotate.
170-
HistoryCommentI (Maybe BranchId2 {- causal to annotate -})
170+
HistoryCommentI (Maybe BranchId2 {- causal to annotate -}) (Maybe Text {- comment -})
171171
| IOTestAllI
172172
| IOTestI (HQ.HashQualified Name)
173173
| LibInstallI

unison-cli/src/Unison/CommandLine/InputPatterns.hs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,14 @@ handleNameSegmentArg arg = do
684684
-- output them as numbered output.
685685
I.StructuredArg _ -> Left "Expected a name segment"
686686

687+
-- | Just a single simple name segment. Useful for lib names, etc.
688+
handleTextArg :: I.Argument -> Either (P.Pretty CT.ColorText) Text
689+
handleTextArg arg = do
690+
case arg of
691+
I.RawArg txt -> pure $ Text.pack txt
692+
-- There are no valid structured args for a raw text arg
693+
I.StructuredArg _ -> Left "Expected a text argument"
694+
687695
handleNameArg :: I.Argument -> Either (P.Pretty CT.ColorText) Name
688696
handleNameArg = \case
689697
I.RawArg raw -> first P.text . Name.parseTextEither . Text.pack $ raw
@@ -1758,22 +1766,32 @@ historyComment =
17581766
"history.comment"
17591767
["comment", "comment.history"]
17601768
I.Visible
1761-
(Parameters [] $ Optional [("hash or branch to create a comment after", namespaceOrProjectBranchArg config)] Nothing)
1769+
(Parameters [] $ Optional [("hash or branch to create a comment after", namespaceOrProjectBranchArg config), ("comment message", noCompletionsArg)] Nothing)
17621770
( P.wrapColumn2
17631771
[ ( makeExample historyComment [],
17641772
"Creates a comment after the head of the current branch."
17651773
),
1766-
( makeExample historyComment ["/main"],
1774+
( makeExample historyComment ["/main:"],
17671775
"Creates a comment after the head of the `main` branch."
1776+
),
1777+
( makeExample historyComment ["#abcdefg"],
1778+
"Creates a comment in the history after #abcdefg"
1779+
),
1780+
( makeExample historyComment ["/main:", "\"Comment message\""],
1781+
"Creates a comment with the content 'Comment message' after the head of the `main` branch."
17681782
)
17691783
]
17701784
)
17711785
\case
1772-
[] -> pure $ Input.HistoryCommentI Nothing
1786+
[] -> pure $ Input.HistoryCommentI Nothing Nothing
17731787
[src] -> do
17741788
target <- handleBranchId2Arg src
1775-
pure $ Input.HistoryCommentI (Just target)
1776-
_ -> wrongArgsLength "at most one argument" []
1789+
pure $ Input.HistoryCommentI (Just target) Nothing
1790+
[src, msg] -> do
1791+
target <- handleBranchId2Arg src
1792+
message <- handleTextArg msg
1793+
pure $ Input.HistoryCommentI (Just target) (Just message)
1794+
_ -> wrongArgsLength "at most two arguments" []
17771795
where
17781796
config =
17791797
ProjectBranchSuggestionsConfig

unison-src/transcripts/idempotent/help.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,21 @@
522522
The full hash must be provided.
523523
524524
history.comment (or comment, comment.history)
525-
`history.comment` Creates a comment after the head of
526-
the current branch.
527-
`history.comment /main` Creates a comment after the head of
528-
the `main` branch.
525+
`history.comment` Creates a comment
526+
after the head of
527+
the current
528+
branch.
529+
`history.comment /main:` Creates a comment
530+
after the head of
531+
the `main` branch.
532+
`history.comment #abcdefg` Creates a comment
533+
in the history
534+
after #abcdefg
535+
`history.comment /main: "Comment message"` Creates a comment
536+
with the content
537+
'Comment message'
538+
after the head of
539+
the `main` branch.
529540
530541
io.test (or test.io)
531542
`io.test mytest` Runs `!mytest`, where `mytest` is a delayed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# History Comments transcript
2+
3+
``` ucm :hide
4+
scratch/main> builtins.merge lib.builtins
5+
```
6+
7+
``` unison :hide
8+
x = 1
9+
```
10+
11+
``` ucm
12+
scratch/main> add
13+
14+
Okay, I'm searching the branch for code that needs to be
15+
updated...
16+
17+
Done.
18+
19+
scratch/main> config.set author.name Unison
20+
21+
scratch/main> history.comment /main: "Initial commit with variable x set to 1"
22+
23+
Done.
24+
25+
scratch/main> alias.term x y
26+
27+
Done.
28+
29+
scratch/main> history.comment /main: "Renamed x to y"
30+
31+
Done.
32+
33+
scratch/main> history
34+
35+
Note: The most recent namespace hash is immediately below this
36+
message.
37+
38+
⊙ Unison
39+
┃ Renamed x to y
40+
41+
⊙ 1. #05mo7svj4c
42+
43+
+ Adds / updates:
44+
45+
y
46+
47+
= Copies:
48+
49+
Original name New name(s)
50+
x y
51+
52+
⊙ Unison
53+
┃ Initial commit with variable x set to 1
54+
55+
⊙ 2. #icsccouust
56+
57+
+ Adds / updates:
58+
59+
x
60+
61+
□ 3. #d7vlf0jooh (start of history)
62+
```

0 commit comments

Comments
 (0)