Return patch changes in a specific file between commits in a repository
Git
$ git diff
LibGit2Sharp
stringresult;using(varrepo=newRepository("path/to/your/repo")){List<Commit>CommitList=newList<Commit>();foreach(LogEntryentryinrepo.Commits.QueryBy("relative/path/to/your/file").ToList())CommitList.Add(entry.Commit);CommitList.Add(null);// Added to show correct initial addintChangeDesired=0;// Change difference desiredvarrepoDifferences=repo.Diff.Compare<Patch>((Equals(CommitList[ChangeDesired+1],null))?null:CommitList[ChangeDesired+1].Tree,(Equals(CommitList[ChangeDesired],null))?null:CommitList[ChangeDesired].Tree);PatchEntryChangesfile=null;try{file=repoDifferences.First(e =>e.Path=="relative/path/to/your/file");}catch{}// If the file has been renamed in the past- this search will failif(!Equals(file,null)){result=file.Patch;}}