public string CompareCommitedfiles(IEnumerable <string> files) { try { temppath = Path.GetTempPath(); temppath = temppath + "master"; List <string> NewFile = new List <string>(); List <string> Olfile = new List <string>(); List <FilesDetails> Csfilenamestruct = new List <FilesDetails>(); FilesDetails f1; foreach (string csfilepath in files) { CodeElementsParams.Filename = csfilepath.Split('/').Last(); CodeElementsParams.Filepath = csfilepath.Remove(csfilepath.LastIndexOf('/')); f1.Filename = CodeElementsParams.Filename; f1.filePath = CodeElementsParams.Filepath; string fileLoc = csfilepath.Replace('/', '\\'); string path = Path.Combine(temppath + "\\" + fileLoc); string csfilecomand = "git checkout" + " " + CodeElementsParams.CommitId + " " + csfilepath; ExecuteGitCommand(temppath, csfilecomand); string latestFile = File.ReadAllText(path); string oldcsfilecomand = "git checkout" + " " + CodeElementsParams.CommitId + "~1" + " " + csfilepath; ExecuteGitCommand(temppath, oldcsfilecomand); string oldFile = File.ReadAllText(path); if (latestFile != oldFile) { NewFile.Add(latestFile); Olfile.Add(oldFile); Csfilenamestruct.Add(f1); } //Get latest version of csfile string updatecommand = "git checkout" + " " + _mainbranch + " " + "--" + " " + csfilepath; ExecuteGitCommand(temppath, updatecommand); } var len = NewFile.Count(); List <string> codeElementValues = new List <string>(); for (int i = 0; i < len; i++) { FilesDetails Fdetails = Csfilenamestruct[i]; var val = Codeparse.Createsyntaxtree(NewFile[i], Olfile[i], Fdetails); codeElementValues.Add(val); } codeElementValues = codeElementValues.Distinct().ToList(); string codeelementcode = string.Join("", codeElementValues.ToArray()); return(codeelementcode); } catch (Exception ex) { throw ex; } }
public string Createsyntaxtree(string newcsfile, string oldCsfile, FilesDetails filedetails) { try { List <string> spans = new List <string>(); FilesDetails.FileSpan.TryGetValue(filedetails.Filename, out spans); List <int> sp = spans.ConvertAll(int.Parse); SourceText oldtext = SourceText.From(oldCsfile); var newtree = CSharpSyntaxTree.ParseText(newcsfile); var oldTree = CSharpSyntaxTree.ParseText(oldCsfile); var newTreeroot = newtree.GetRoot(); // var span1 = newtree.GetText().GetTextChanges(oldtext); foreach (var spindex in sp) { var span = newtree.GetText().Lines[2689].Span; var nodes = newTreeroot.DescendantNodes().Where(x => x.Span.IntersectsWith(span)); //get based on nodes span: try { var newElement = ""; newElement = "File Path: " + filedetails.filePath + "\n" + "File Name: " + filedetails.Filename + "\n"; var m = newTreeroot.FindNode(span).FirstAncestorOrSelf <MethodDeclarationSyntax>(); if (m != null) { string methoddetails = m.Modifiers + " " + m.ReturnType + " " + m.Identifier + m.ParameterList; string mname1 = "Method Name: " + methoddetails + "\n"; newElement = newElement + mname1; var lnodes = nodes.ToList(); var blockindex = lnodes.IndexOf(x => x.Kind().ToString() == "Block"); for (int i = blockindex; i < lnodes.Count; i++) { Console.WriteLine(lnodes[i].Kind()); if (lnodes[i].Kind().ToString() == "IdentifierName") { break; } string kindtext = lnodes[i].Kind().ToString(); newElement = newElement + kindtext + "\n"; } } var p = newTreeroot.FindNode(span).FirstAncestorOrSelf <PropertyDeclarationSyntax>(); if (p != null) { string propertydeatils = p.Modifiers + " " + p.Identifier.ToString() + " " + p.ExpressionBody; string PropertyName = "Property: " + propertydeatils + "\n"; newElement = newElement + PropertyName; //Propperty to identifier } var c = newTreeroot.FindNode(span).FirstAncestorOrSelf <ConstructorDeclarationSyntax>(); if (c != null) { string constrctordetails = c.Modifiers + " " + c.Identifier + " " + c.ParameterList; string ConstructorName = "Constructor: " + constrctordetails + "\n"; newElement = newElement + ConstructorName; //block to identifier } } catch (Exception ex) { throw; } } var difflst = newtree.GetChanges(oldTree); if (difflst.Count > 0) { difflst.RemoveAll(x => x.NewText == ""); difflst.RemoveAll(x => x.NewText == "//"); difflst.RemoveAll(x => x.NewText == " "); difflst.RemoveAll(x => x.NewText == " "); difflst.RemoveAll(x => x.NewText == " "); } foreach (var item in difflst) { var newElement = ""; newElement = "File Path: " + filedetails.filePath + "\n" + "File Name: " + filedetails.Filename + "\n"; string parentnode = string.Empty; MethodDeclarationSyntax methodfirst = null; try { methodfirst = newTreeroot.FindNode(item.Span).FirstAncestorOrSelf <MethodDeclarationSyntax>(); } catch (Exception ex) //Only case of Exception { LogEntry.LogMsg.Error(newElement, ex); Console.WriteLine(item); Console.WriteLine(newElement); continue; } if (methodfirst != null) { parentnode = methodfirst.Kind().ToString(); string methoddetails = methodfirst.Modifiers + " " + methodfirst.ReturnType + " " + methodfirst.Identifier + methodfirst.ParameterList; string mname1 = "Method Name: " + methoddetails + "\n"; newElement = newElement + mname1; newElement = newElement + "SyntaxNode Tree:" + "\n"; } PropertyDeclarationSyntax prop = null; try { prop = newTreeroot.FindNode(item.Span).FirstAncestorOrSelf <PropertyDeclarationSyntax>(); } catch (Exception ex) { LogEntry.LogMsg.Error(newElement, ex); Console.WriteLine(item); Console.WriteLine(newElement); continue; } if (prop != null) { parentnode = prop.Kind().ToString(); string propertydeatils = prop.Modifiers + " " + prop.Identifier.ToString() + " " + prop.ExpressionBody; string PropertyName = "Property: " + propertydeatils + "\n"; newElement = newElement + PropertyName + "SyntaxNode Tree:" + "\n";; //newElement = newElement + "SyntaxNode Tree:" + "\n"; } //contructor ConstructorDeclarationSyntax cons = null; try { cons = newTreeroot.FindNode(item.Span).FirstAncestorOrSelf <ConstructorDeclarationSyntax>(); } catch (Exception ex) { LogEntry.LogMsg.Error(newElement, ex); Console.WriteLine(item); Console.WriteLine(newElement); continue; } if (cons != null) { parentnode = cons.Kind().ToString(); string constrctordetails = cons.Modifiers + " " + cons.Identifier + " " + cons.ParameterList; string ConstructorName = "Constructor: " + constrctordetails + "\n"; newElement = newElement + ConstructorName; newElement = newElement + "SyntaxNode Tree:" + "\n"; } if (parentnode != string.Empty) { //Code Elements var codeelementlist = newTreeroot.FindNode(item.Span).DescendantNodesAndSelf(); foreach (var CElement in codeelementlist) { if (!_ignorelist.Contains(CElement.ToString())) { newElement = newElement + CElement.Kind().ToString() + "\n"; } } } CsfileCodeElement = CsfileCodeElement + newElement + "\n"; } return(CsfileCodeElement); } catch (Exception ex) { throw ex; } }