Skip to content

Instantly share code, notes, and snippets.

@renevo
Last active December 25, 2015 20:29
Show Gist options
  • Save renevo/7035625 to your computer and use it in GitHub Desktop.
Save renevo/7035625 to your computer and use it in GitHub Desktop.
Pulling ModPack from Git using NGit
using System;
using System.IO;
using NGit;
using NGit.Api;
// USES NuGet package ngit2 for Git.
namespace GitTest
{
class Program
{
static void Main(string[] args)
{
var path = Path.GetFullPath("./client/");
var repository = "https://github.com/RenEvo/BurpcraftModPack.git";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
var cloneCommand = Git.CloneRepository();
cloneCommand.SetDirectory(path);
cloneCommand.SetURI(repository);
cloneCommand.SetProgressMonitor(new ConsoleProgressMonitor());
cloneCommand.Call();
}
else
{
Git.Open(path).Reset().SetMode(ResetCommand.ResetType.HARD).Call();
var pullCommand = Git.Open(path).Pull();
pullCommand.SetProgressMonitor(new ConsoleProgressMonitor());
var result = pullCommand.Call();
var messages = result.GetFetchResult().GetMessages();
if (!string.IsNullOrWhiteSpace(messages))
Console.WriteLine(messages);
Console.WriteLine(result.GetMergeResult().GetMergeStatus());
}
Console.ReadLine();
}
public class ConsoleProgressMonitor : ProgressMonitor
{
private int _totalWork = 0;
private int _currentWork = 0;
public override void Start(int totalTasks)
{
Console.WriteLine("{0} Tasks to complete");
}
public override void BeginTask(string title, int totalWork)
{
_currentWork = 0;
_totalWork = totalWork;
Console.WriteLine(title);
}
public override void Update(int completed)
{
_currentWork++;
Console.WriteLine(_currentWork + " of " + _totalWork);
}
public override void EndTask()
{
Console.WriteLine("Completed Task");
}
public override bool IsCancelled()
{
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment