Example #1
0
        /// <summary>
        /// Resolve symlinks and canonicalize the path (including "/" -> "\" on Windows)
        /// </summary>
        private static string RealPath(string path)
        {
            if (PlatformUtils.IsPosix())
            {
                bool trailingSlash = path.EndsWith("/");

                string resolvedPath;
                byte[] pathBytes = Encoding.UTF8.GetBytes(path);
                unsafe
                {
                    byte *resolvedPtr;
                    fixed(byte *pathPtr = pathBytes)
                    {
                        if ((resolvedPtr = Stdlib.realpath(pathPtr, (byte *)IntPtr.Zero)) == (byte *)IntPtr.Zero)
                        {
                            return(null);
                        }
                    }

                    resolvedPath = U8StringConverter.ToManaged(resolvedPtr);
                }

                // Preserve the trailing slash if there was one present initially
                return(trailingSlash ? $"{resolvedPath}/" : resolvedPath);
            }

            if (PlatformUtils.IsWindows())
            {
                // GetFullPath on Windows already preserves trailing slashes
                return(Path.GetFullPath(path));
            }

            throw new PlatformNotSupportedException();
        }
        public void U8StringConverter_ToManaged_Null_ReturnsNull()
        {
            unsafe
            {
                string actual = U8StringConverter.ToManaged(null);

                Assert.Null(actual);
            }
        }
        public void U8StringConverter_ToManaged_ZeroPtr_ReturnsNull()
        {
            unsafe
            {
                string actual = U8StringConverter.ToManaged((byte *)IntPtr.Zero);

                Assert.Null(actual);
            }
        }
        public void U8StringConverter_ToManaged_ComplexString_ReturnsExpectedString()
        {
            unsafe
            {
                fixed(byte *ptr = ComplexUtf8)
                {
                    string actual = U8StringConverter.ToManaged(ptr);

                    Assert.Equal(ComplexString, actual);
                }
            }
        }
        public void U8StringConverter_ToManaged_NullByte_ReturnsEmptyString()
        {
            unsafe
            {
                fixed(byte *ptr = NullString)
                {
                    string actual = U8StringConverter.ToManaged(ptr);

                    Assert.Equal(string.Empty, actual);
                }
            }
        }
Example #6
0
        public static void ThrowIfError(int result, string functionName = null)
        {
            if (result != 0)
            {
                unsafe
                {
                    git_error error = git_error_last();

                    string errorMessage = U8StringConverter.ToManaged(error.message);

                    string mainMessage = functionName is null
                        ? $"libgit2 '{functionName}' returned non-zero value"
                        : "libgit2 returned non-zero value";

                    throw new InteropException(mainMessage, result, new Exception(errorMessage));
                }
            }
        }
 public unsafe string GetValue()
 {
     return(U8StringConverter.ToManaged(value));
 }
 public unsafe string GetName()
 {
     return(U8StringConverter.ToManaged(name));
 }
 public override unsafe string ToString()
 {
     return(U8StringConverter.ToManaged(ptr));
 }