public static string GetUpstreamSource(NGit.Repository repo, string branch) { StoredConfig config = repo.GetConfig(); string remote = config.GetString("branch", branch, "remote"); string rbranch = config.GetString("branch", branch, "merge"); if (string.IsNullOrEmpty(rbranch)) { return(null); } if (rbranch.StartsWith(Constants.R_HEADS, System.StringComparison.Ordinal)) { rbranch = rbranch.Substring(Constants.R_HEADS.Length); } else if (rbranch.StartsWith(Constants.R_TAGS, System.StringComparison.Ordinal)) { rbranch = rbranch.Substring(Constants.R_TAGS.Length); } if (!string.IsNullOrEmpty(remote) && remote != ".") { return(remote + "/" + rbranch); } else { return(rbranch); } }
public static void SetUpstreamSource(NGit.Repository repo, string branch, string remoteBranch) { StoredConfig config = repo.GetConfig(); if (string.IsNullOrEmpty(remoteBranch)) { config.UnsetSection("branch", branch); config.Save(); return; } int i = remoteBranch.IndexOf('/'); string upBranch; if (i == -1) { var tags = repo.GetTags(); if (tags.ContainsKey(remoteBranch)) { upBranch = Constants.R_TAGS + remoteBranch; } else { upBranch = Constants.R_HEADS + remoteBranch; } config.SetString("branch", branch, "remote", "."); } else { upBranch = Constants.R_HEADS + remoteBranch.Substring(i + 1); config.SetString("branch", branch, "remote", remoteBranch.Substring(0, i)); } config.SetString("branch", branch, "merge", upBranch); config.Save(); }
public Stash Create(string message) { UserConfig config = _repo.GetConfig().Get(UserConfig.KEY); RevWalk rw = new RevWalk(_repo); ObjectId headId = _repo.Resolve(Constants.HEAD); var parent = rw.ParseCommit(headId); PersonIdent author = new PersonIdent(config.GetAuthorName() ?? "unknown", config.GetAuthorEmail() ?? "unknown@(none)."); if (string.IsNullOrEmpty(message)) { // Use the commit summary as message message = parent.Abbreviate(7).ToString() + " " + parent.GetShortMessage(); int i = message.IndexOfAny(new char[] { '\r', '\n' }); if (i != -1) { message = message.Substring(0, i); } } // Create the index tree commit ObjectInserter inserter = _repo.NewObjectInserter(); DirCache dc = _repo.ReadDirCache(); var tree_id = dc.WriteTree(inserter); inserter.Release(); string commitMsg = "index on " + _repo.GetBranch() + ": " + message; ObjectId indexCommit = GitUtil.CreateCommit(_repo, commitMsg + "\n", new ObjectId[] { headId }, tree_id, author, author); // Create the working dir commit tree_id = WriteWorkingDirectoryTree(parent.Tree, dc); commitMsg = "WIP on " + _repo.GetBranch() + ": " + message; var wipCommit = GitUtil.CreateCommit(_repo, commitMsg + "\n", new ObjectId[] { headId, indexCommit }, tree_id, author, author); string prevCommit = null; FileInfo sf = StashRefFile; if (sf.Exists) { prevCommit = File.ReadAllText(sf.FullName).Trim(' ', '\t', '\r', '\n'); } Stash s = new Stash(prevCommit, wipCommit.Name, author, commitMsg); FileInfo stashLog = StashLogFile; File.AppendAllText(stashLog.FullName, s.FullLine + "\n"); File.WriteAllText(sf.FullName, s.CommitId + "\n"); // Wipe all local changes GitUtil.HardReset(_repo, Constants.HEAD); s.StashCollection = this; return(s); }