Commit() public method

Commit staged changes and update HEAD. The default git author is used.
public Commit ( string message ) : Commit
message string The commit message
return Commit
Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            //var arg0 = @"C:\temp\Dropbox\jnk\WeSellCars\"; // file path
            //var arg1 = @"C:\git\apdekock.github.io\"; //repo path
            //var arg2 = @"_posts\2015-08-04-weMineData.markdown"; //post path
            //var arg3 = @"C:\Program Files\Git\cmd\git.exe"; //git path

            try
            {
                StringBuilder listOfLines = new StringBuilder();
                var chromeOptions = new ChromeOptions();
                chromeOptions.AddArguments("-incognito");
                using (IWebDriver driver = new ChromeDriver(Path.Combine(Directory.GetCurrentDirectory(), "WebDriverServer"), chromeOptions))
                {
                    driver.Navigate().GoToUrl("http://www.wesellcars.co.za/vehicle/category/all");
                    var findElement = driver.FindElements(By.CssSelector("#main_content > div > div.vehicles.grid > div.item"));

                    foreach (var item in findElement)
                    {
                        var image = item.FindElement(By.CssSelector("a"));
                        var link = image.GetAttribute("href");
                        var lines =
                            new List<string>(item.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
                            {
                                link
                            };
                        var format = string.Join(",", lines);
                        listOfLines.AppendLine(format);
                        Console.WriteLine(format);
                    }

                    driver.Quit();
                }

                var cTempCarsTxt = args[0] + @"\WeSellCars_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv";
                var fileStream = File.Create(cTempCarsTxt);
                fileStream.Close();
                File.WriteAllText(cTempCarsTxt, listOfLines.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            StringBuilder postTemplate = new StringBuilder();
            postTemplate.AppendLine("---");
            postTemplate.AppendLine("layout: post ");
            postTemplate.AppendLine("title: \"Scraping and GitSharp, and Spark lines\" ");
            postTemplate.AppendLine("date: 2016-08-07");
            postTemplate.AppendLine(
                "quote: \"If you get pulled over for speeding. Tell them your spouse has diarrhoea. — Phil Dunphy [Phil’s - osophy]\"");
            postTemplate.AppendLine("categories: scraping, auto generating post, gitsharp");
            postTemplate.AppendLine("---");
            postTemplate.AppendLine(
                string.Format(
                    "This page is a daily re-generated post (last re-generated  **{0}**), that shows the movement of prices on the [www.weSellCars.co.za](http://www.wesellcars.co.za) website.",
                    DateTime.Now));
            postTemplate.AppendLine("");
            postTemplate.AppendLine("## Why?");
            postTemplate.AppendLine("");
            postTemplate.AppendLine(
                "This post is the culmination of some side projects I've been playing around with. Scraping, looking for a way to integrate with git through C# and a challenge to use this blog (which has no back-end or support for any server side scripting) to dynamically update a post. I realise that would best be accomplished through just making new posts but I opted for an altered post as this is a tech blog, and multiple posts about car prices would not be appropriate.");
            postTemplate.AppendLine("");
            postTemplate.AppendLine("# Lessons learned");
            postTemplate.AppendLine("");
            postTemplate.AppendLine(
                "* [GitSharp](http://www.eqqon.com/index.php/GitSharp) is limited and I needed to grab the project from [github](https://github.com/henon/GitSharp) in order to use it.");
            postTemplate.AppendLine(
                "    The NuGet package kept on complaining about a **repositoryformatversion** setting in config [Core] that it required even though it was present, it still complained. So, I downloaded the source to debug the issue but then I did not encounter it. Apart from that - gitsharp did not allow me to push - and it seems the project does not have a lot of contribution activity (not criticising, just stating. I should probably take this up and contribute, especially as I would like to employ git as a file store for an application. Levering off the already refined functions coudl be a win but more on that in another post).");
            postTemplate.AppendLine(
                "* Scraping with Selenium is probably not the best way - rather employ [HttpClient](https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx).");
            postTemplate.AppendLine(
                "* For quick, easy and painless sparklines [jQuery Sparklines](http://omnipotent.net/jquery.sparkline/#s-about)");
            postTemplate.AppendLine(
                "* No backend required, just a simple process running on a server, that commits to a repo (ghPages) gets the job done.");

            postTemplate.AppendLine("");
            var aggregateData = new AggregateData(new FileSystemLocation(args[0]));
            var dictionary = aggregateData.Aggregate();

            var html = aggregateData.GetHTML(dictionary);
            if (dictionary.Count > 0)
            {
                postTemplate.AppendLine("## The List");
            }
            postTemplate.AppendLine(html);

            // update post file
            FileInfo fi = new FileInfo(args[1] + args[2]);
            var streamWriter = fi.CreateText();
            streamWriter.WriteLine(postTemplate.ToString());

            streamWriter.Flush();
            streamWriter.Dispose();

            Repository repository = new Repository(args[1]);

            repository.Index.Add(args[1] + args[2]);
            Commit commited = repository.Commit(string.Format("Updated {0}", DateTime.Now),
                new Author("Philip de Kock", "*****@*****.**"));
            if (commited.IsValid)
            {
                string gitCommand = args[3];
                const string gitPushArgument = @"push origin";
                ProcessStartInfo psi = new ProcessStartInfo(gitCommand, gitPushArgument)
                {
                    WorkingDirectory = args[1],
                    UseShellExecute = true
                };
                Process.Start(psi);
            }
        }
Exemplo n.º 2
0
        public void Repository()
        {
            //Opening an existing git repository
            repo = new Repository("path/to/repo");

            // Now suppose you have created some new files and want to commit them
            repo.Index.Add("README", "License.txt");
            Commit commit = repo.Commit("My first commit with gitsharp", new Author("henon", "*****@*****.**"));

            // Easy, isn't it? Now let's have a look at the changes of this commit:
            foreach (Change change in commit.Changes)
                Console.WriteLine(change.Name + " " + change.ChangeType);

            //Get the staged changes from the index
            repo.Status.Added.Contains("README");

            //Access and manipulate the configuration
            repo.Config["core.autocrlf"] = "false";
        }
Exemplo n.º 3
0
        private void AddItemsToGit(bool initialCheckin)
        {
            var directories = Directory.GetDirectories(GitRepositoryFolder, "*.*", SearchOption.AllDirectories).ToList();

            directories.RemoveAll(r => r.Contains(".git") || r.Contains("\\Logs"));
            directories.Add(GitRepositoryFolder);

            var files = new List <string>();

            directories.ForEach(dir => files.AddRange(Directory.GetFiles(dir)));

            using (var frmStatus = new Status())
            {
                frmStatus.Show();

                frmStatus.SetStatusText("Gathering Items to Add to Source Control");
                if (initialCheckin)
                {
                    frmStatus.SetMaximum(files.Count);
                }
                else
                {
                    frmStatus.SetMaximum(files.Count + repo.Status.Added.Count + repo.Status.Removed.Count +
                                         repo.Status.Modified.Count);
                }

                frmStatus.SetStatusText("Adding Items");
                string commitMessage = initialCheckin
                    ? "Initial Load of Existing V3 Data Files"
                    : "Updated/Added File to V3 Folders";
                if (initialCheckin)
                {
                    files.ForEach(file =>
                    {
                        repo.Index.Add(file);
                        repo.Commit(commitMessage);
                        frmStatus.SetStatusText(commitMessage);
                        frmStatus.SetValue(frmStatus.Value + 1);
                    });
                }
                // repo.Index.AddAll();

                repo.Status.Added.ToList().ForEach(a =>
                {
                    repo.Index.Add(Path.Combine(GitRepositoryFolder, a));
                    commitMessage = string.Format("Added {0}", new FileInfo(Path.Combine(GitRepositoryFolder, a)).Name);
                    repo.Commit(commitMessage);
                    frmStatus.SetStatusText(commitMessage);
                    frmStatus.SetValue(frmStatus.Value + 1);
                });
                repo.Status.Removed.ToList().ForEach(a =>
                {
                    repo.Index.Add(Path.Combine(GitRepositoryFolder, a));
                    commitMessage = string.Format("Removed {0}", new FileInfo(Path.Combine(GitRepositoryFolder, a)).Name);
                    repo.Commit(commitMessage);
                    frmStatus.SetStatusText(commitMessage);
                    frmStatus.SetValue(frmStatus.Value + 1);
                });
                repo.Status.Modified.ToList().ForEach(a =>
                {
                    repo.Index.Add(Path.Combine(GitRepositoryFolder, a));
                    commitMessage = string.Format("Changed {0}", new FileInfo(Path.Combine(GitRepositoryFolder, a)).Name);
                    repo.Commit(commitMessage);
                    frmStatus.SetStatusText(commitMessage);
                    frmStatus.SetValue(frmStatus.Value + 1);
                });
                frmStatus.Close();
            }
        }