@@ -1389,104 +1389,22 @@ outer:
13891389 return review , nil
13901390}
13911391
1392+ // CommentAsDiff returns c.Patch as *Diff
13921393// CommentAsDiff returns c.Patch as *Diff
13931394func CommentAsDiff (ctx context.Context , c * issues_model.Comment ) (* Diff , error ) {
1394- // Try to parse existing patch first
1395- if c .Patch != "" {
1396- diff , err := ParsePatch (ctx , setting .Git .MaxGitDiffLines ,
1397- setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (c .Patch ), "" )
1398- if err == nil && len (diff .Files ) > 0 && len (diff .Files [0 ].Sections ) > 0 {
1399- return diff , nil
1400- }
1401- }
1402-
1403- // If patch is empty or invalid, generate code context for unchanged line comments
1404- if c .TreePath != "" && c .CommitSHA != "" && c .Line != 0 {
1405- return generateCodeContextForComment (ctx , c )
1406- }
1407-
1408- return nil , fmt .Errorf ("no valid patch or context available for comment ID: %d" , c .ID )
1409- }
1410-
1411- func generateCodeContextForComment (ctx context.Context , c * issues_model.Comment ) (* Diff , error ) {
1412- if err := c .LoadIssue (ctx ); err != nil {
1413- return nil , fmt .Errorf ("LoadIssue: %w" , err )
1414- }
1415- if err := c .Issue .LoadRepo (ctx ); err != nil {
1416- return nil , fmt .Errorf ("LoadRepo: %w" , err )
1417- }
1418- if err := c .Issue .LoadPullRequest (ctx ); err != nil {
1419- return nil , fmt .Errorf ("LoadPullRequest: %w" , err )
1420- }
1421-
1422- pr := c .Issue .PullRequest
1423- if err := pr .LoadBaseRepo (ctx ); err != nil {
1424- return nil , fmt .Errorf ("LoadBaseRepo: %w" , err )
1425- }
1426-
1427- gitRepo , closer , err := gitrepo .RepositoryFromContextOrOpen (ctx , pr .BaseRepo )
1428- if err != nil {
1429- return nil , fmt .Errorf ("RepositoryFromContextOrOpen: %w" , err )
1430- }
1431- defer closer .Close ()
1432-
1433- // Get the file content at the commit
1434- commit , err := gitRepo .GetCommit (c .CommitSHA )
1435- if err != nil {
1436- return nil , fmt .Errorf ("GetCommit: %w" , err )
1437- }
1438-
1439- entry , err := commit .GetTreeEntryByPath (c .TreePath )
1440- if err != nil {
1441- return nil , fmt .Errorf ("GetTreeEntryByPath: %w" , err )
1442- }
1443-
1444- blob := entry .Blob ()
1445- dataRc , err := blob .DataAsync ()
1395+ diff , err := ParsePatch (ctx , setting .Git .MaxGitDiffLines ,
1396+ setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (c .Patch ), "" )
14461397 if err != nil {
1447- return nil , fmt .Errorf ("DataAsync: %w" , err )
1448- }
1449- defer dataRc .Close ()
1450-
1451- // Read file lines
1452- scanner := bufio .NewScanner (dataRc )
1453- var lines []string
1454- for scanner .Scan () {
1455- lines = append (lines , scanner .Text ())
1456- }
1457- if err := scanner .Err (); err != nil {
1458- return nil , fmt .Errorf ("scanner error: %w" , err )
1459- }
1460-
1461- // Validate comment line is within file bounds
1462- commentLine := int (c .UnsignedLine ())
1463- if commentLine < 1 || commentLine > len (lines ) {
1464- return nil , fmt .Errorf ("comment line %d is out of file bounds (1-%d)" , commentLine , len (lines ))
1398+ log .Error ("Unable to parse patch: %v" , err )
1399+ return nil , err
14651400 }
1466-
1467- // Calculate line range to show (commented line + lines above it, matching CutDiffAroundLine behavior)
1468- contextLines := setting .UI .CodeCommentLines
1469- startLine := max (commentLine - contextLines , 1 )
1470- endLine := commentLine
1471-
1472- var patchBuilder strings.Builder
1473- patchBuilder .WriteString (fmt .Sprintf ("diff --git a/%s b/%s\n " , c .TreePath , c .TreePath ))
1474- patchBuilder .WriteString (fmt .Sprintf ("--- a/%s\n " , c .TreePath ))
1475- patchBuilder .WriteString (fmt .Sprintf ("+++ b/%s\n " , c .TreePath ))
1476- patchBuilder .WriteString (fmt .Sprintf ("@@ -%d,%d +%d,%d @@\n " , startLine , endLine - startLine + 1 , startLine , endLine - startLine + 1 ))
1477-
1478- for i := startLine - 1 ; i < endLine ; i ++ {
1479- patchBuilder .WriteString (" " )
1480- patchBuilder .WriteString (lines [i ])
1481- patchBuilder .WriteString ("\n " )
1401+ if len (diff .Files ) == 0 {
1402+ return nil , fmt .Errorf ("no file found for comment ID: %d" , c .ID )
14821403 }
1483-
1484- diff , err := ParsePatch (ctx , setting .Git .MaxGitDiffLines ,
1485- setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (patchBuilder .String ()), "" )
1486- if err != nil {
1487- return nil , fmt .Errorf ("ParsePatch: %w" , err )
1404+ secs := diff .Files [0 ].Sections
1405+ if len (secs ) == 0 {
1406+ return nil , fmt .Errorf ("no sections found for comment ID: %d" , c .ID )
14881407 }
1489-
14901408 return diff , nil
14911409}
14921410
0 commit comments