示例#1
0
        public bool HasChanges(string[] diffLines, int startLine, int endLine)
        {
            var difftool = new DiffTool();
            var hunks    = difftool.GetHunks(diffLines, startLine, endLine);

            return(hunks.Count() > 0);
        }
示例#2
0
        public void Apply(string[] diffLines, int startLine, int endLine, bool cached, bool reverse)
        {
            var difftool = new DiffTool();
            var hunks    = difftool.GetHunks(diffLines, startLine, endLine, reverse);

            if (hunks.Count() <= 0)
            {
                throw new Exception("No change selected");
            }

            var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff");

            using (var file = new StreamWriter(tmpFileName, false, Encoding.UTF8))
            {
                for (int i = 0; i < 4; i++)
                {
                    // Skip the line: index xxxx..xxxx
                    if (i != 1)
                    {
                        var line = diffLines[i];
                        file.Write(FixEOL(line));
                    }
                }
                foreach (var hunk in hunks)
                {
                    var heading = $"@@ -{hunk.OldBlock[0]},{hunk.OldBlock[1]} +{hunk.NewBlock[0]},{hunk.NewBlock[1]} @@{hunk.Heading}";
                    file.Write(FixEOL(heading));
                    foreach (var line in hunk.Lines)
                    {
                        file.Write(FixEOL(line));
                    }
                }
            }
            var cmd = "apply --ignore-whitespace";

            if (cached)
            {
                cmd += " --cached";
            }
            var result = GitBash.Run($"{cmd} \"{tmpFileName}\"", WorkingDirectory);

            File.Delete(tmpFileName);
            if (result.HasError)
            {
                throw new GitException(result.Error);
            }
        }