public void EditIngoreFile() { var ignoreFile = Path.Combine(WorkingDirectory, ".gitignore"); var ret = GitBash.Run("config core.editor", WorkingDirectory); if (!ret.HasError && ret.Output.Trim() != "") { var editor = ret.Output.Trim(); if (editor.Length == 0) { editor = "notepad.exe"; } var cmd = string.Format("{0} \"{1}\"", editor, ignoreFile); cmd = cmd.Replace("/", "\\"); var pinfo = new ProcessStartInfo("cmd.exe") { Arguments = "/C \"" + cmd + "\"", UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = this.WorkingDirectory, }; Process.Start(pinfo); } }
public void CheckOutBranch(string branch, bool createNew = false) { if (!this.HasGitRepository) { return; } if (GitBash.Exists) { try { GitBash.Run(string.Format("checkout {0} {1}", (createNew ? "-b" : ""), branch), this.GitWorkingDirectory); } catch (Exception ex) { if (!ex.Message.StartsWith("Switched to a new branch")) { throw; } } } else { Git git = new Git(this.repository); git.Checkout().SetName(branch) .SetCreateBranch(createNew) .Call(); } }
public string AmendCommit(string message) { if (!HasGitRepository) { return(null); } if (string.IsNullOrEmpty(message)) { throw new ArgumentException("Commit message must not be null or empty!", "message"); } string msg = ""; if (GitBash.Exists) { var msgFile = Path.Combine(this.repository.Directory, "COMMITMESSAGE"); File.WriteAllText(msgFile, message); msg = GitBash.Run(string.Format("commit --amend -F \"{0}\"", msgFile), this.GitWorkingDirectory); if (msg.IndexOf('\n') > 0) { msg = msg.Split('\n')[0]; } File.Delete(msgFile); } else { var git = new Git(this.repository); var rev = git.Commit().SetAmend(true).SetMessage(message).Call(); msg = rev.Name; } Refresh(); return(msg); }
/// <summary> /// Requires absolute path /// </summary> /// <param name="fileName"></param> public void UnStageFile(string fileName) { if (!this.HasGitRepository) { return; } var fileNameRel = GetRelativeFileName(fileName); if (GitBash.Exists) { if (head == null) { GitBash.Run(string.Format("rm --cached -- \"{0}\"", fileNameRel), this.GitWorkingDirectory); } else { GitBash.Run(string.Format("reset -- \"{0}\"", fileNameRel), this.GitWorkingDirectory); } } else { ResetCommand resetCommand = new Git(repository).Reset(); resetCommand.AddPath(GetRelativeFileNameForGit(fileName)); resetCommand.SetRef(Constants.HEAD); resetCommand.Call(); } this.cache.Remove(GetCacheKey(fileName)); this.changedFiles = null; }
internal IList <GitFile> GetChangedFiles() { if (!HasGitRepository) { return(new List <GitFile>()); } if (GitBash.Exists) { var output = GitBash.Run("status --porcelain -z --untracked-files", this.GitWorkingDirectory); return(ParseGitStatus(output)); } else { var list = new List <GitFile>(); var treeWalk = new TreeWalk(this.repository); treeWalk.Recursive = true; treeWalk.Filter = TreeFilter.ANY_DIFF; var id = repository.Resolve(Constants.HEAD); if (id != null) { treeWalk.AddTree(new RevWalk(repository).ParseTree(id)); } else { treeWalk.AddTree(new EmptyTreeIterator()); } treeWalk.AddTree(new DirCacheIterator(this.repository.ReadDirCache())); treeWalk.AddTree(new FileTreeIterator(this.repository)); var filters = new TreeFilter[] { new SkipWorkTreeFilter(INDEX), new IndexDiffFilter(INDEX, WORKDIR) }; treeWalk.Filter = AndTreeFilter.Create(filters); while (treeWalk.Next()) { WorkingTreeIterator workingTreeIterator = treeWalk.GetTree <WorkingTreeIterator>(WORKDIR); if (workingTreeIterator != null && workingTreeIterator.IsEntryIgnored()) { continue; } var fileName = GetFullPath(treeWalk.PathString); if (Directory.Exists(fileName)) { continue; // this excludes sub modules } var status = GetFileStatus(treeWalk); list.Add(new GitFile { FileName = GetRelativeFileName(fileName), Status = status }); this.cache[GetCacheKey(fileName)] = status; } return(list); } }
internal string GetBranchId(string name) { string id = null; var result = GitBash.Run("rev-parse " + name, this.WorkingDirectory); if (!result.HasError && !result.Output.StartsWith("fatal:")) { id = result.Output.Trim(); } return(id); }
public bool CurrentCommitHasRefs() { if (this.head == null) { return(false); } var result = GitBash.Run("show-ref --head --dereference", this.GitWorkingDirectory); var refs = result.Output.Split('\n') .Where(t => t.IndexOf(this.head.Name) >= 0); return(refs.Count() > 2); }
public GitRepository(string directory) { this.workingDirectory = directory; Refresh(); this.isGit = false; var result = GitBash.Run("rev-parse --show-toplevel", WorkingDirectory); if (!result.HasError && !result.Output.StartsWith("fatal:")) { this.workingDirectory = result.Output.Trim(); result = GitBash.Run("rev-parse --is-inside-work-tree", WorkingDirectory); isGit = string.Compare("true", result.Output.Trim(), true) == 0; } }
public static void Init(string folderName) { if (GitBash.Exists) { GitBash.Run("init", folderName); } else { var gitFolder = Path.Combine(folderName, Constants.DOT_GIT); var repo = new FileRepository(gitFolder); repo.Create(); var dir = Directory.CreateDirectory(gitFolder); dir.Attributes = FileAttributes.Directory | FileAttributes.Hidden; } }
public IEnumerable <string> GetCommitsForFile(string fileName) { if (!this.HasGitRepository || !GitBash.Exists) { return(new string[0]); } var fileNameRel = GetRelativeFileName(fileName); var result = GitBash.Run(string.Format("log -z --ignore-space-change --pretty=format:%H -- \"{0}\"", fileNameRel), this.GitWorkingDirectory); if (!result.HasError) { return(result.Output.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries)); } return(new string[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); } }
public void Refresh() { this.repositoryGraph = null; this.changedFiles = null; this.isGit = false; this.branch = null; this.remotes = null; this.configs = null; var result = GitBash.Run("rev-parse --show-toplevel", WorkingDirectory); if (!result.HasError && !result.Output.StartsWith("fatal:")) { this.workingDirectory = result.Output.Trim(); result = GitBash.Run("rev-parse --is-inside-work-tree", WorkingDirectory); isGit = string.Compare("true", result.Output.Trim(), true) == 0; } }
public bool CurrentCommitHasRefs() { var head = GetBranchId("HEAD"); if (head == null) { return(false); } var result = GitBash.Run("show-ref --head --dereference", WorkingDirectory); if (!result.HasError && !result.Output.StartsWith("fatal:")) { var refs = result.Output.Split('\n') .Where(t => t.IndexOf(head) >= 0); return(refs.Count() > 2); } return(false); }
private string GitRun(string cmd) { if (!this.IsGit) { return(null); } var result = GitBash.Run(cmd, this.WorkingDirectory); if (result.HasError) { throw new GitException(result.Error); } if (result.Output.StartsWith("fatal:")) { throw new GitException(result.Output); } return(result.Output); }
/// <summary> /// Requires absolute path /// </summary> /// <param name="fileName"></param> public void UnStageFile(string fileName) { if (!this.HasGitRepository) { return; } var fileNameRel = GetRelativeFileName(fileName); if (GitBash.Exists) { if (head == null) { GitBash.Run(string.Format("rm --cached -- \"{0}\"", fileNameRel), this.GitWorkingDirectory); } else { GitBash.Run(string.Format("reset -- \"{0}\"", fileNameRel), this.GitWorkingDirectory); } } else { TreeEntry treeEntry = null; if (commitTree != null) { treeEntry = commitTree.FindBlobMember(fileNameRel); } //var index = repository.GetIndex(); //index.RereadIfNecessary(); index.Remove(repository.WorkTree, fileName); if (treeEntry != null) { index.AddEntry(treeEntry); } index.Write(); } this.cache.Remove(GetCacheKey(fileName)); this.changedFiles = null; }
private void VerifyGit() { var isValid = false; if (GitBash.Exists) { var name = GitBash.Run("config --global user.name", "").Output; var email = GitBash.Run("config --global user.email", "").Output; isValid = !string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(email); } if (!isValid) { Settings.Show(); } else { Settings.Hide(); } }
/// <summary> /// Requires absolute path /// </summary> /// <param name="fileName"></param> public void StageFile(string fileName) { if (!this.HasGitRepository) { return; } //var index = repository.GetIndex(); //index.RereadIfNecessary(); if (GitBash.Exists) { if (File.Exists(fileName)) { GitBash.Run(string.Format("add \"{0}\"", GetRelativeFileName(fileName)), this.GitWorkingDirectory); } else { GitBash.Run(string.Format("rm --cached -- \"{0}\"", GetRelativeFileName(fileName)), this.GitWorkingDirectory); } } else { index.Remove(repository.WorkTree, fileName); if (File.Exists(fileName)) { var content = File.ReadAllBytes(fileName); index.Add(repository.WorkTree, fileName, content); } else { //stage deleted index.Remove(repository.WorkTree, fileName); } index.Write(); } this.cache.Remove(GetCacheKey(fileName)); this.changedFiles = null; }
/// <summary> /// Requires absolute path /// </summary> /// <param name="fileName"></param> public void StageFile(string fileName) { if (!this.HasGitRepository) { return; } //var index = repository.GetIndex(); //index.RereadIfNecessary(); if (GitBash.Exists) { if (File.Exists(fileName)) { GitBash.Run(string.Format("add \"{0}\"", GetRelativeFileName(fileName)), this.GitWorkingDirectory); } else { GitBash.Run(string.Format("rm --cached -- \"{0}\"", GetRelativeFileName(fileName)), this.GitWorkingDirectory); } } else { if (File.Exists(fileName)) { AddCommand addCommand = new Git(repository).Add(); addCommand.AddFilepattern(GetRelativeFileNameForGit(fileName)); addCommand.Call(); } else { RmCommand rmCommand = new Git(repository).Rm(); rmCommand.AddFilepattern(GetRelativeFileNameForGit(fileName)); rmCommand.Call(); } } this.cache.Remove(GetCacheKey(fileName)); this.changedFiles = null; }
public void CheckOutFile(string fileName) { if (!this.HasGitRepository || this.head == null) { return; } string fileNameRel = GetRelativeFileName(fileName); if (GitBash.Exists) { GitBash.Run(string.Format("checkout -- \"{0}\"", fileNameRel), this.GitWorkingDirectory); } else { GitFileStatus status = GetFileStatus(fileName); SaveFileFromRepository(fileName, fileName); if (status == GitFileStatus.Staged || status == GitFileStatus.Removed) { UnStageFile(fileName); } } }
public void SetConfig(string name, string value) { GitBash.Run($"config {name} {value}", WorkingDirectory); }
public void RemveConfig(string name) { GitBash.Run($"config --unset {name}", WorkingDirectory); }
public string GetConfig(string name) { var result = GitBash.Run($"config --get {name}", WorkingDirectory); return(result.Output.Trim()); }
public GitCommitResult Commit(string message, bool amend = false, bool signoff = false) { if (!this.HasGitRepository) { return(null); } if (string.IsNullOrEmpty(message)) { throw new ArgumentException("Commit message must not be null or empty!", "message"); } GitCommitResult result = new GitCommitResult(); if (GitBash.Exists) { var msgFile = Path.Combine(this.repository.Directory, "COMMITMESSAGE"); File.WriteAllText(msgFile, message); try { string opt = ""; if (amend) { opt += "--amend "; } if (signoff) { opt += "--signoff "; } var bashResult = GitBash.Run(string.Format("commit -F \"{0}\" {1}", msgFile, opt), this.GitWorkingDirectory); if (bashResult.HasError) { result.Message = bashResult.Error; } else { result.Message = bashResult.Output; result.IsSha1 = true; } } finally { File.Delete(msgFile); } } else { try { var git = new Git(this.repository); var rev = git.Commit().SetMessage(message).SetAmend(amend).Call(); result.Message = rev.Name; result.IsSha1 = true; } catch (Exception ex) { result.Message = ex.Message; } } Refresh(); return(result); }
public static void Init(string folderName) { GitBash.Run("init", folderName); GitBash.Run("config core.ignorecase true", folderName); }