Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses Libgit2sharp to clone repos. Fixes #29 #31

Merged
merged 4 commits into from
Dec 1, 2014
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/SourceBrowser.Site/Controllers/UploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Web.Mvc;

using SourceBrowser.SolutionRetriever;
using System;

public class UploadController : Controller
{
Expand All @@ -24,14 +25,20 @@ public ActionResult Submit(string githubUrl)
var retriever = new GitHubRetriever(githubUrl);
if (!retriever.IsValidUrl())
{
// TODO: Return error
ViewBag.Error = "Invalid GitHub repository. Please try another";
ViewBag.Error = "Make sure that the provided path is valid.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the message should indicate this has to be a GitHub repository.

return View("Index");
}

string filePath = retriever.RetrieveProject();
var organizationPath = System.Web.Hosting.HostingEnvironment.MapPath("~/") + "SB_Files\\" + retriever.UserName;
var repoPath = Path.Combine(organizationPath, retriever.RepoName);
string filePath = string.Empty;
try
{
filePath = retriever.RetrieveProject();
}
catch (Exception ex)
{
ViewBag.Error = "There was an error downloading this repository.";
return View("Index");
}

// Generate the source browser files for this solution
var solutionPaths = GetSolutionPaths(filePath);
Expand All @@ -41,6 +48,9 @@ public ActionResult Submit(string githubUrl)
return View("Index");
}

var organizationPath = System.Web.Hosting.HostingEnvironment.MapPath("~/") + "SB_Files\\" + retriever.UserName;
var repoPath = Path.Combine(organizationPath, retriever.RepoName);

// TODO: Use parallel for.
foreach (var path in solutionPaths)
{
Expand Down
6 changes: 5 additions & 1 deletion src/SourceBrowser.Site/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
Expand Down
72 changes: 24 additions & 48 deletions src/SourceBrowser.SolutionRetriever/GitHubSolutionRetriever.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Octokit;
using System.IO;
using LibGit2Sharp;

namespace SourceBrowser.SolutionRetriever
{
public class GitHubRetriever
{
private string _url;
private Guid guid = Guid.NewGuid();
//private string _userName = String.Empty;
public string UserName { get; set; }
//private string _repoName = string.Empty;
public string RepoName { get; set; }

private string _downloadUrl
{
get
{
//TODO: retrieve branches instead of assuming
//Master exists
return _url + "/archive/master.zip";
}
}
private string _githubUrl;

public GitHubRetriever(string url)
{
Expand All @@ -48,54 +34,44 @@ public bool IsValidUrl()
if (String.IsNullOrWhiteSpace(RepoName))
return false;

var githubClient = new GitHubClient(new ProductHeaderValue("SourceBrowser"));
try
{
var repository = githubClient.Repository.Get(UserName, RepoName).Result;
}
catch (AggregateException e)
{
//Assuming the inner exception is a NotFoundException
return false;
}

return true;
}

public string RetrieveProject()
{
string baseRepositoryPath = System.Web.Hosting.HostingEnvironment.MapPath("~/") + "\\GithubStaging\\";
if (!Directory.Exists(baseRepositoryPath))
Directory.CreateDirectory(baseRepositoryPath);

string zipName = guid + ".zip";
using (var client = new WebClient())
{
client.DownloadFile(_downloadUrl, baseRepositoryPath + zipName);
}
string absoluteRepositoryPath = baseRepositoryPath + UserName + '\\' + RepoName;

// libgit2 requires the target directory to be empty
if (Directory.Exists(absoluteRepositoryPath))
{
try
{
Directory.Delete(absoluteRepositoryPath, recursive: true);
}
catch
{
// Swallow.
}
DeleteReadOnlyDirectory(absoluteRepositoryPath);
}
Directory.CreateDirectory(absoluteRepositoryPath);

//unpack the zip
System.IO.Compression.ZipFile.ExtractToDirectory(baseRepositoryPath + zipName, absoluteRepositoryPath);
deleteZipFile(baseRepositoryPath);
Repository.Clone(_url, absoluteRepositoryPath);
return absoluteRepositoryPath;
}

private void deleteZipFile(string baseRepoPath)
/// <summary>
/// Recursively deletes a directory as well as any subdirectories and files. If the files are read-only, they are flagged as normal and then deleted.
/// From http://stackoverflow.com/questions/25549589/programatically-delete-local-repository-with-libgit2sharp
/// </summary>
/// <param name="directory">The name of the directory to remove.</param>
public static void DeleteReadOnlyDirectory(string directory)
{
string zipName = guid + ".zip";
System.IO.File.Delete(baseRepoPath + zipName);
foreach (var subdirectory in Directory.EnumerateDirectories(directory))
{
DeleteReadOnlyDirectory(subdirectory);
}
foreach (var fileName in Directory.EnumerateFiles(directory))
{
var fileInfo = new FileInfo(fileName);
fileInfo.Attributes = FileAttributes.Normal;
fileInfo.Delete();
}
Directory.Delete(directory);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\LibGit2Sharp.0.20.0.0\build\net40\LibGit2Sharp.props" Condition="Exists('..\packages\LibGit2Sharp.0.20.0.0\build\net40\LibGit2Sharp.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -11,6 +12,7 @@
<AssemblyName>SourceBrowser.SolutionRetriever</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>7d443bcb</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -30,6 +32,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="LibGit2Sharp">
<HintPath>..\packages\LibGit2Sharp.0.20.0.0\lib\net40\LibGit2Sharp.dll</HintPath>
</Reference>
<Reference Include="Octokit">
<HintPath>..\packages\Octokit.0.5.2\lib\net45\Octokit.dll</HintPath>
</Reference>
Expand All @@ -53,6 +58,12 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\LibGit2Sharp.0.20.0.0\build\net40\LibGit2Sharp.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\LibGit2Sharp.0.20.0.0\build\net40\LibGit2Sharp.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
1 change: 1 addition & 0 deletions src/SourceBrowser.SolutionRetriever/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="LibGit2Sharp" version="0.20.0.0" targetFramework="net45" />
<package id="Octokit" version="0.5.2" targetFramework="net45" />
</packages>