fork of go-gitdiff with jj support

Rename PatchHeader.Message to .Body (#7)

Git usually refers to the whole thing (title + body) as the "commit
message", so using different terms for the parts clarifies things. Also
add a new Message() function that returns the combined string.

authored by Billy Keyes and committed by GitHub b5ae0bd4 546a186b

Changed files
+33 -19
gitdiff
+24 -10
gitdiff/patch_header.go
··· 35 35 Committer *PatchIdentity 36 36 CommitterDate *PatchDate 37 37 38 - // The title and message summarizing the changes in the patch. Empty if a 39 - // title or message is not included in the header. 40 - Title string 41 - Message string 38 + // The title and body of the commit message describing the changes in the 39 + // patch. Empty if no message is included in the header. 40 + Title string 41 + Body string 42 + } 43 + 44 + // Message returns the commit message for the header. The message consists of 45 + // the title and the body separated by an empty line. 46 + func (h *PatchHeader) Message() string { 47 + var msg strings.Builder 48 + if h != nil { 49 + msg.WriteString(h.Title) 50 + if h.Body != "" { 51 + msg.WriteString("\n\n") 52 + msg.WriteString(h.Body) 53 + } 54 + } 55 + return msg.String() 42 56 } 43 57 44 58 // PatchIdentity identifies a person who authored or committed a patch. ··· 253 267 return nil, s.Err() 254 268 } 255 269 256 - title, indent := scanPatchTitle(s) 270 + title, indent := scanMessageTitle(s) 257 271 if s.Err() != nil { 258 272 return nil, s.Err() 259 273 } 260 274 h.Title = title 261 275 262 276 if title != "" { 263 - msg := scanPatchMessage(s, indent) 277 + body := scanMessageBody(s, indent) 264 278 if s.Err() != nil { 265 279 return nil, s.Err() 266 280 } 267 - h.Message = msg 281 + h.Body = body 268 282 } 269 283 270 284 return h, nil 271 285 } 272 286 273 - func scanPatchTitle(s *bufio.Scanner) (title string, indent string) { 287 + func scanMessageTitle(s *bufio.Scanner) (title string, indent string) { 274 288 var b strings.Builder 275 289 for i := 0; s.Scan(); i++ { 276 290 line := s.Text() ··· 292 306 return b.String(), indent 293 307 } 294 308 295 - func scanPatchMessage(s *bufio.Scanner, indent string) string { 309 + func scanMessageBody(s *bufio.Scanner, indent string) string { 296 310 var b strings.Builder 297 311 var empty int 298 312 for i := 0; s.Scan(); i++ { ··· 351 365 h.Title = msg.Header.Get("Subject") 352 366 353 367 s := bufio.NewScanner(msg.Body) 354 - h.Message = scanPatchMessage(s, "") 368 + h.Body = scanMessageBody(s, "") 355 369 if s.Err() != nil { 356 370 return nil, s.Err() 357 371 }
+9 -9
gitdiff/patch_header_test.go
··· 163 163 Raw: "Sat Apr 11 15:21:23 2020 -0700", 164 164 } 165 165 expectedTitle := "A sample commit to test header parsing" 166 - expectedMsg := "The medium format shows the body, which\nmay wrap on to multiple lines.\n\nAnother body line." 166 + expectedBody := "The medium format shows the body, which\nmay wrap on to multiple lines.\n\nAnother body line." 167 167 168 168 tests := map[string]struct { 169 169 Input string ··· 199 199 Author: expectedIdentity, 200 200 AuthorDate: expectedDate, 201 201 Title: expectedTitle, 202 - Message: expectedMsg, 202 + Body: expectedBody, 203 203 }, 204 204 }, 205 205 "prettyFull": { ··· 219 219 Author: expectedIdentity, 220 220 Committer: expectedIdentity, 221 221 Title: expectedTitle, 222 - Message: expectedMsg, 222 + Body: expectedBody, 223 223 }, 224 224 }, 225 225 "prettyFuller": { ··· 243 243 Committer: expectedIdentity, 244 244 CommitterDate: expectedDate, 245 245 Title: expectedTitle, 246 - Message: expectedMsg, 246 + Body: expectedBody, 247 247 }, 248 248 }, 249 249 "mailbox": { ··· 264 264 Parsed: expectedDate.Parsed, 265 265 Raw: "Sat, 11 Apr 2020 15:21:23 -0700", 266 266 }, 267 - Title: "[PATCH] " + expectedTitle, 268 - Message: expectedMsg, 267 + Title: "[PATCH] " + expectedTitle, 268 + Body: expectedBody, 269 269 }, 270 270 }, 271 271 "unwrapTitle": { ··· 304 304 Author: expectedIdentity, 305 305 AuthorDate: expectedDate, 306 306 Title: expectedTitle, 307 - Message: expectedMsg, 307 + Body: expectedBody, 308 308 }, 309 309 }, 310 310 "ignoreLeadingBlankLines": { ··· 354 354 if exp.Title != act.Title { 355 355 t.Errorf("incorrect parsed title:\n expected: %q\n actual: %q", exp.Title, act.Title) 356 356 } 357 - if exp.Message != act.Message { 358 - t.Errorf("incorrect parsed message:\n expected: %q\n actual: %q", exp.Message, act.Message) 357 + if exp.Body != act.Body { 358 + t.Errorf("incorrect parsed body:\n expected: %q\n actual: %q", exp.Body, act.Body) 359 359 } 360 360 }) 361 361 }