Tuesday 8 May 2018

Download GIT Files using C#.NET



 Introduction

In  this post I will show you how to download repository and single file from GIT using C#. We are using LibGit2Sharp


Using the code

Step 1 Adding LibGit2Sharp Nuget Package to C# project

To add Swagger to Web API, we just need to install an open source project called Swashbuckle via NuGet.

Download GIT Files using C#.NET




Step 2 .  Install NuGet Packages


Install "LibGit2Sharp" NuGet package in the solution. 



Download GIT Files using C#.NET Sujeet Bhujbal





Step 3 

Copy below method and pass parameters like filename,git repository name , username and password.

public static void GitFetch(string fileName, string gitRepository, string userName, string passWord)
        {
            try
            {
                string workingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\GitFiles\\";
                DeleteFolder(workingDirectory);
                string gitPath = string.Empty;

                var co = new CloneOptions()
                {
                    CredentialsProvider = (_url, _user, _cred) =>
                        new UsernamePasswordCredentials
                        {
                            Username = userName,
                            Password = passWord
                        },
                    Checkout = false,
                    RecurseSubmodules = false,
                    IsBare = true
                };

                gitPath = Repository.Clone(gitRepository, workingDirectory, co);
                string outPutPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + fileName;
                using (var repo = new Repository(gitPath))
                {
                    var masterBranch = repo.Branches["master"];
                    var latestCommit = masterBranch.Tip;
                    var blob = latestCommit[fileName].Target as Blob;
                    WriteFile(outPutPath, blob.GetContentText().ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadKey();
            }
        }
           

   public static void WriteFile(string filePath, string data)
        {

            CreateFolderIfNeeded(filePath);
            DeleteExistingFile(filePath);
            using (StreamWriter sw = new StreamWriter(File.Open(filePath, System.IO.FileMode.Append)))
            {
                sw.WriteLine(data);
                sw.Flush();
            }
        }


        public static void CreateFolderIfNeeded(string filename)
        {
            string folder = System.IO.Path.GetDirectoryName(filename);
            if (!System.IO.Directory.Exists(folder))
            {
                System.IO.Directory.CreateDirectory(folder);
            }
        }

        public static void DeleteExistingFile(string filename)
        {

            if (System.IO.File.Exists(filename))
            {
                System.IO.File.Delete(filename);
            }
        }
        public static void DeleteFolder(string path)
        {
            if (System.IO.Directory.Exists(path))
            {
                System.IO.Directory.Delete(path, true);
            }
        }


Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
------------------------------------------------------------------------------