public static byte[] GetResetUnstagedLinesAsPatch(GitModule module, string text, int selectionPosition, int selectionLength, bool staged, Encoding fileContentEncoding) { string header; ChunkList selectedChunks = ChunkList.GetSelectedChunks(text, selectionPosition, selectionLength, staged, out header); if (selectedChunks == null) { return(null); } string body = selectedChunks.ToResetUnstagedLinesPatch(); //git apply has problem with dealing with autocrlf //I noticed that patch applies when '\r' chars are removed from patch if autocrlf is set to true if (body != null && "true".Equals(module.GetEffectiveSetting("core.autocrlf"), StringComparison.InvariantCultureIgnoreCase)) { body = body.Replace("\r", ""); } if (header == null || body == null) { return(null); } else { return(GetPatchBytes(header, body, fileContentEncoding)); } }
public static Chunk FromNewFile(GitModule module, string fileText, int selectionPosition, int selectionLength, bool reset) { Chunk result = new Chunk(); result.StartLine = 0; int currentPos = 0; string gitEol = module.GetEffectiveSetting("core.eol"); string eol; if ("crlf".Equals(gitEol)) { eol = "\r\n"; } else if ("native".Equals(gitEol)) { eol = Environment.NewLine; } else { eol = "\n"; } int eolLength = eol.Length; string[] lines = fileText.Split(new string[] { eol }, StringSplitOptions.None); int i = 0; while (i < lines.Length) { string line = lines[i]; PatchLine patchLine = new PatchLine() { Text = (reset ? "-" : "+") + line }; //do not refactor, there are no breakpoints condition in VS Experss if (currentPos <= selectionPosition + selectionLength && currentPos + line.Length >= selectionPosition) { patchLine.Selected = true; } if (i == lines.Length - 1) { if (!line.Equals(string.Empty)) { result.CurrentSubChunk.IsNoNewLineAtTheEnd = "\\ No newline at end of file"; result.AddDiffLine(patchLine, reset); } } else { result.AddDiffLine(patchLine, reset); } currentPos += line.Length + eolLength; i++; } return(result); }
public static byte[] GetSelectedLinesAsNewPatch(GitModule module, string newFileName, string text, int selectionPosition, int selectionLength, Encoding fileContentEncoding, bool reset) { StringBuilder sb = new StringBuilder(); string fileMode = "100000";//given fake mode to satisfy patch format, git will override this sb.Append(string.Format("diff --git a/{0} b/{0}", newFileName)); sb.Append("\n"); if (!reset) { sb.Append("new file mode " + fileMode); sb.Append("\n"); } sb.Append("index 0000000..0000000"); sb.Append("\n"); if (reset) { sb.Append("--- a/" + newFileName); } else { sb.Append("--- /dev/null"); } sb.Append("\n"); sb.Append("+++ b/" + newFileName); sb.Append("\n"); string header = sb.ToString(); ChunkList selectedChunks = ChunkList.FromNewFile(module, text, selectionPosition, selectionLength, reset); if (selectedChunks == null) { return(null); } string body = selectedChunks.ToStagePatch(false); //git apply has problem with dealing with autocrlf //I noticed that patch applies when '\r' chars are removed from patch if autocrlf is set to true if (reset && body != null && "true".Equals(module.GetEffectiveSetting("core.autocrlf"), StringComparison.InvariantCultureIgnoreCase)) { body = body.Replace("\r", ""); } if (header == null || body == null) { return(null); } else { return(GetPatchBytes(header, body, fileContentEncoding)); } }
/// <summary> /// Determine what file contains the global ignores. /// </summary> /// <remarks> /// According to https://git-scm.com/docs/git-config, the following are checked in order: /// - core.excludesFile configuration, /// - $XDG_CONFIG_HOME/git/ignore, if XDG_CONFIG_HOME is set and not empty, /// - $HOME/.config/git/ignore. /// </remarks> private string DetermineGlobalIgnoreFilePath() { string globalExcludeFile = Module.GetEffectiveSetting("core.excludesFile"); if (!string.IsNullOrWhiteSpace(globalExcludeFile)) { return(Path.GetFullPath(globalExcludeFile)); } string xdgConfigHome = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME"); if (!string.IsNullOrWhiteSpace(xdgConfigHome)) { return(Path.GetFullPath(Path.Combine(xdgConfigHome, "git/ignore"))); } return(Path.GetFullPath(Path.Combine(GitCommandHelpers.GetHomeDir(), ".config/git/ignore"))); }
public bool ProcessRevisionSelectionChange(GitModule currentModule, IReadOnlyCollection <GitRevision> selectedRevisions) { if (selectedRevisions.Count > 1) { return(false); } var revision = selectedRevisions.FirstOrDefault(); var changed = !string.Equals(revision?.AuthorEmail, AuthorEmailToHighlight, StringComparison.OrdinalIgnoreCase); if (changed) { AuthorEmailToHighlight = revision != null ? revision.AuthorEmail : currentModule.GetEffectiveSetting(SettingKeyString.UserEmail); return(true); } return(false); }
public SelectionChangeAction ProcessRevisionSelectionChange(GitModule currentModule, IReadOnlyCollection <GitRevision> selectedRevisions) { if (selectedRevisions.Count > 1) { return(SelectionChangeAction.NoAction); } var newSelectedRevision = selectedRevisions.FirstOrDefault(); bool differentRevisionAuthorSelected = !AuthorEmailEqualityComparer.Instance.Equals(_currentSelectedRevision, newSelectedRevision); if (differentRevisionAuthorSelected) { AuthorEmailToHighlight = newSelectedRevision != null ? newSelectedRevision.AuthorEmail : currentModule.GetEffectiveSetting(SettingKeyString.UserEmail); } _currentSelectedRevision = newSelectedRevision; return(differentRevisionAuthorSelected ? SelectionChangeAction.RefreshUserInterface : SelectionChangeAction.NoAction); }
public static Chunk FromNewFile([NotNull] GitModule module, [NotNull] string fileText, int selectionPosition, int selectionLength, bool reset, [NotNull] byte[] filePreamble, [NotNull] Encoding fileContentEncoding) { var result = new Chunk { _startLine = 0 }; int currentPos = 0; string gitEol = module.GetEffectiveSetting("core.eol"); string eol; if (gitEol == "crlf") { eol = "\r\n"; } else if (gitEol == "native") { eol = Environment.NewLine; } else { eol = "\n"; } int eolLength = eol.Length; string[] lines = fileText.Split(new[] { eol }, StringSplitOptions.None); int i = 0; while (i < lines.Length) { string line = lines[i]; string preamble = i == 0 ? new string(fileContentEncoding.GetChars(filePreamble)) : string.Empty; var patchLine = new PatchLine((reset ? "-" : "+") + preamble + line); // do not refactor, there are no breakpoints condition in VS Express if (currentPos <= selectionPosition + selectionLength && currentPos + line.Length >= selectionPosition) { patchLine.Selected = true; } if (i == lines.Length - 1) { if (line != string.Empty) { result.CurrentSubChunk.IsNoNewLineAtTheEnd = GitModule.NoNewLineAtTheEnd; result.AddDiffLine(patchLine, reset); if (reset && patchLine.Selected) { // if the last line is selected to be reset and there is no new line at the end of file // then we also have to remove the last not selected line in order to add it right again with the "No newline.." indicator PatchLine lastNotSelectedLine = result.CurrentSubChunk.RemovedLines.LastOrDefault(l => !l.Selected); if (lastNotSelectedLine != null) { lastNotSelectedLine.Selected = true; PatchLine clonedLine = lastNotSelectedLine.Clone(); clonedLine.SetOperation("+"); result.CurrentSubChunk.AddedLines.Add(clonedLine); } result.CurrentSubChunk.WasNoNewLineAtTheEnd = GitModule.NoNewLineAtTheEnd; } } } else { result.AddDiffLine(patchLine, reset); } currentPos += line.Length + eolLength; i++; } return(result); }