1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Globalization ;
4
+ using System . IO ;
3
5
using LibGit2Sharp . Core ;
4
6
using LibGit2Sharp . Core . Handles ;
5
7
@@ -31,11 +33,11 @@ private GitDiffOptions BuildOptions(DiffOptions diffOptions, IEnumerable<string>
31
33
return options ;
32
34
}
33
35
34
- options . PathSpec = GitStrArrayIn . BuildFrom ( ToFilePaths ( paths ) ) ;
36
+ options . PathSpec = GitStrArrayIn . BuildFrom ( ToFilePaths ( repo , paths ) ) ;
35
37
return options ;
36
38
}
37
39
38
- private static FilePath [ ] ToFilePaths ( IEnumerable < string > paths )
40
+ private static FilePath [ ] ToFilePaths ( Repository repo , IEnumerable < string > paths )
39
41
{
40
42
var filePaths = new List < FilePath > ( ) ;
41
43
@@ -46,7 +48,7 @@ private static FilePath[] ToFilePaths(IEnumerable<string> paths)
46
48
throw new ArgumentException ( "At least one provided path is either null or empty." , "paths" ) ;
47
49
}
48
50
49
- filePaths . Add ( path ) ;
51
+ filePaths . Add ( BuildRelativePathFrom ( repo , path ) ) ;
50
52
}
51
53
52
54
if ( filePaths . Count == 0 )
@@ -57,6 +59,26 @@ private static FilePath[] ToFilePaths(IEnumerable<string> paths)
57
59
return filePaths . ToArray ( ) ;
58
60
}
59
61
62
+ private static string BuildRelativePathFrom ( Repository repo , string path )
63
+ {
64
+ //TODO: To be removed when libgit2 natively implements this
65
+ if ( ! Path . IsPathRooted ( path ) )
66
+ {
67
+ return path ;
68
+ }
69
+
70
+ string normalizedPath = Path . GetFullPath ( path ) ;
71
+
72
+ if ( ! normalizedPath . StartsWith ( repo . Info . WorkingDirectory , StringComparison . Ordinal ) )
73
+ {
74
+ throw new ArgumentException ( string . Format ( CultureInfo . InvariantCulture ,
75
+ "Unable to process file '{0}'. This absolute filepath escapes out of the working directory of the repository ('{1}')." ,
76
+ normalizedPath , repo . Info . WorkingDirectory ) ) ;
77
+ }
78
+
79
+ return normalizedPath . Substring ( repo . Info . WorkingDirectory . Length ) ;
80
+ }
81
+
60
82
/// <summary>
61
83
/// Needed for mocking purposes.
62
84
/// </summary>
0 commit comments