@@ -18,6 +18,10 @@ import (
1818)
1919
2020func TestGitCLIBackend_RawDiff (t * testing.T ) {
21+ defaultOpts := git.RawDiffOpts {
22+ InterHunkContext : 3 ,
23+ ContextLines : 3 ,
24+ }
2125 var f1Diff = []byte (`diff --git f f
2226index a29bdeb434d874c9b1d8969c40c42161b03fafdc..c0d0fb45c382919737f8d0c20aaf57cf89b74af8 100644
2327--- f
@@ -48,15 +52,15 @@ index 0000000000000000000000000000000000000000..8a6a2d098ecaf90105f1cf2fa90fc460
4852 )
4953
5054 t .Run ("streams diff" , func (t * testing.T ) {
51- r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead )
55+ r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead , defaultOpts )
5256 require .NoError (t , err )
5357 diff , err := io .ReadAll (r )
5458 require .NoError (t , err )
5559 require .NoError (t , r .Close ())
5660 require .Equal (t , string (f1Diff ), string (diff ))
5761 })
5862 t .Run ("streams diff intersection" , func (t * testing.T ) {
59- r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeIntersection )
63+ r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeIntersection , defaultOpts )
6064 require .NoError (t , err )
6165 diff , err := io .ReadAll (r )
6266 require .NoError (t , err )
@@ -75,14 +79,49 @@ index 0000000000000000000000000000000000000000..8a6a2d098ecaf90105f1cf2fa90fc460
7579 "git commit -m foo --author='Foo Author <foo@sourcegraph.com>'" ,
7680 )
7781
78- r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead , "f2" )
82+ r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead , defaultOpts , "f2" )
7983 require .NoError (t , err )
8084 diff , err := io .ReadAll (r )
8185 require .NoError (t , err )
8286 require .NoError (t , r .Close ())
8387 // We expect only a diff for f2, not for f.
8488 require .Equal (t , string (f2Diff ), string (diff ))
8589 })
90+ t .Run ("custom context" , func (t * testing.T ) {
91+ // Prepare repo state:
92+ backend := BackendWithRepoCommands (t ,
93+ "echo 'line1\n line2\n line3\n lin4\n line5\n line6\n line7\n line8\n ' > f" ,
94+ "git add f" ,
95+ "git commit -m foo --author='Foo Author <foo@sourcegraph.com>'" ,
96+ "git tag testbase" ,
97+ "echo 'line1.1\n line2\n line3\n lin4\n line5.5\n line6\n line7\n line8\n ' > f" ,
98+ "git add f" ,
99+ "git commit -m foo --author='Foo Author <foo@sourcegraph.com>'" ,
100+ )
101+
102+ var expectedDiff = []byte (`diff --git f f
103+ index 0ef51c52043997fdd257a0b77d761e9ca58bcc1f..58692a00a73d1f78df00014edf4ef39ef4ba0019 100644
104+ --- f
105+ +++ f
106+ @@ -1 +1 @@
107+ -line1
108+ +line1.1
109+ @@ -5 +5 @@ lin4
110+ -line5
111+ +line5.5
112+ ` )
113+
114+ r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead , git.RawDiffOpts {
115+ InterHunkContext : 0 ,
116+ ContextLines : 0 ,
117+ })
118+ require .NoError (t , err )
119+ diff , err := io .ReadAll (r )
120+ require .NoError (t , err )
121+ require .NoError (t , r .Close ())
122+ t .Log (string (diff ))
123+ require .Equal (t , string (expectedDiff ), string (diff ))
124+ })
86125 t .Run ("not found revspec" , func (t * testing.T ) {
87126 // Prepare repo state:
88127 backend := BackendWithRepoCommands (t ,
@@ -92,19 +131,19 @@ index 0000000000000000000000000000000000000000..8a6a2d098ecaf90105f1cf2fa90fc460
92131 "git tag test" ,
93132 )
94133
95- _ , err := backend .RawDiff (ctx , "unknown" , "test" , git .GitDiffComparisonTypeOnlyInHead )
134+ _ , err := backend .RawDiff (ctx , "unknown" , "test" , git .GitDiffComparisonTypeOnlyInHead , defaultOpts )
96135 require .Error (t , err )
97136 require .True (t , errors.HasType [* gitdomain.RevisionNotFoundError ](err ))
98137
99- _ , err = backend .RawDiff (ctx , "test" , "unknown" , git .GitDiffComparisonTypeOnlyInHead )
138+ _ , err = backend .RawDiff (ctx , "test" , "unknown" , git .GitDiffComparisonTypeOnlyInHead , defaultOpts )
100139 require .Error (t , err )
101140 require .True (t , errors.HasType [* gitdomain.RevisionNotFoundError ](err ))
102141 })
103142 t .Run ("files outside repository" , func (t * testing.T ) {
104143 // We use git-diff-tree, but with git-diff you can diff any files on disk
105144 // which is dangerous. So we have this safeguard test here in place to
106145 // make sure we don't regress on that.
107- r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead , "/dev/null" , "/etc/hosts" )
146+ r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead , defaultOpts , "/dev/null" , "/etc/hosts" )
108147 require .NoError (t , err )
109148 _ , err = io .ReadAll (r )
110149 require .Error (t , err )
@@ -115,7 +154,7 @@ index 0000000000000000000000000000000000000000..8a6a2d098ecaf90105f1cf2fa90fc460
115154 ctx , cancel := context .WithCancel (ctx )
116155 t .Cleanup (cancel )
117156
118- r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead )
157+ r , err := backend .RawDiff (ctx , "testbase" , "HEAD" , git .GitDiffComparisonTypeOnlyInHead , defaultOpts )
119158 require .NoError (t , err )
120159
121160 cancel ()
0 commit comments