protected Classes.GitBatch WriteFeatureBranchGit(RepositoryVM repo, string startingFolder, string branchName) { var mainBat = startingFolder + @"\PullForFeatureBranch.bat"; if (System.IO.File.Exists(mainBat)) { System.IO.File.Delete(mainBat); } // see if folder for REPO is there and if not, pull it - if it is recheck out master and release var bat = new Classes.GitBatch(mainBat) { Redirect = true, RedirectError = $@"{startingFolder.Replace(@"\\", @"\")}\ErrorFB.txt", RedirectFile = $@"{startingFolder.Replace(@"\\", @"\")}\OutputFB.txt" }; bat.AddLine($"echo start"); var theFile = repo.Name; var featurePrefix = _settings.Setting.GetSection("FeatureBranchPrefix").Value ?? "feature/"; var releasePrefix = _settings.Setting.GetSection("ReleaseBranchName").Value ?? "release"; var masterPrefix = _settings.Setting.GetSection("MasterBranchName").Value ?? "master"; var featureFile = $"{featurePrefix}{branchName}"; if (featureFile.Length > 60) { featureFile = featureFile.Substring(0, 60); } bat.AddLine($"echo Working on '{theFile}'"); bat.AddCD($@"{startingFolder}\{theFile}"); bat.AddReset(); if (repo.OffRelease) { bat.AddCheckout(releasePrefix, $"origin/{releasePrefix}"); bat.AddLine($"git checkout -b {featureFile} {releasePrefix}"); } else { bat.AddCheckout(masterPrefix, $"origin/{masterPrefix}"); bat.AddLine($"git checkout -b {featureFile} {masterPrefix}"); } bat.AddLine($"git push -u origin {featureFile}"); bat.AddCD(startingFolder); bat.Write(); return(bat); }
public JsonResult PullRepository(string guid) { // see if starting folder from login exists var startingFolder = string.Empty; var repoName = string.Empty; try { CreateStartingFolder(ref startingFolder); // See or create sub-folder based on PROJECT name if (System.IO.Directory.Exists(startingFolder)) { CreateStartingProjectFolder(ref startingFolder); var mainBat = startingFolder + @"\PullRepository.bat"; if (System.IO.File.Exists(mainBat)) { System.IO.File.Delete(mainBat); } var bat = new Classes.GitBatch(mainBat) { Redirect = true, RedirectError = $@"{startingFolder.Replace(@"\\", @"\")}\ErrorPull.txt", RedirectFile = $@"{startingFolder.Replace(@"\\", @"\")}\OutputPull.txt" }; var vsts = new Classes.VSTS.Vsts(HttpContext.Session.GetString(Session.TOKEN)); var repo = vsts.GetRepositoryOnGuid(guid); repoName = repo.Name; // Now clear the repo folder if there if (System.IO.Directory.Exists(startingFolder)) { ClearFolder(startingFolder + $"\\{repoName}"); } if (System.IO.Directory.Exists(startingFolder + $"\\{repoName}")) { System.IO.Directory.Delete(startingFolder + $"\\{repoName}"); } int ndx = repo.RemoteUrl.LastIndexOf(@"/"); string theFile = repo.RemoteUrl.Substring(ndx + 1); bat.AddLine($"echo start"); bat.AddLine($"echo Working on '{theFile}'"); if (!System.IO.Directory.Exists($@"{startingFolder}\{theFile}")) { bat.AddCD(startingFolder); bat.AddLine($@"git clone {repo.RemoteUrl}"); } else { bat.AddCD(startingFolder); bat.AddLine($@"git clone --bare {repo.RemoteUrl}"); bat.AddLine("git config core.bare false"); } bat.AddCD($@"{startingFolder}\{theFile}"); bat.AddCheckout("master", "origin/master"); bat.AddCheckout("release", "origin/release"); //bat.AddLine($"git checkout -b master origin/master"); bat.AddCD(startingFolder); bat.Write(); bat.GitRun(startingFolder, HttpContext.Session.GetString(Session.USER), HttpContext.Session.GetString(Session.GIT)); } return(Json(new JsonMessage(true, repoName + " : Pulled"))); } catch (System.Exception ex) { return(Json(new JsonMessage(false, $"{repoName} - Failed to clone - [{ex.Message}]"))); } }
public JsonResult PullAllRepositories(int pos) { var repoName = string.Empty; try { // get starting folder from login var startingFolder = string.Empty; CreateStartingFolder(ref startingFolder); if (System.IO.Directory.Exists(startingFolder)) { CreateStartingProjectFolder(ref startingFolder); if (System.IO.Directory.Exists(startingFolder)) { var mainBat = startingFolder + @"\PullAllRepositories.bat"; if (System.IO.File.Exists(mainBat)) { System.IO.File.Delete(mainBat); } var vsts = new Classes.VSTS.Vsts(HttpContext.Session.GetString(Session.TOKEN)); var repos = vsts.GetRepos(HttpContext.Session.GetString(Session.SELECTED_PROJ)); var repo = repos.Repositories[pos]; repoName = repo.Name; int ndx = repo.RemoteUrl.LastIndexOf(@"/"); string theFile = repo.RemoteUrl.Substring(ndx + 1); if (System.IO.Directory.Exists($@"{startingFolder}\{theFile}")) { return(Json(new JsonMessage(true, "Already exists"))); } else { var bat = new Classes.GitBatch(mainBat) { Redirect = true, RedirectError = $@"{startingFolder.Replace(@"\\", @"\")}\ErrorPullAll{pos}.txt", RedirectFile = $@"{startingFolder.Replace(@"\\", @"\")}\OutputPullAll{pos}.txt" }; bat.AddLine($"echo start"); bat.AddLine($"echo Working on '{theFile}'"); bat.AddCD(startingFolder); bat.AddLine($@"git clone {repo.RemoteUrl}"); bat.AddCD($@"{startingFolder}\{theFile}"); //bat.AddLine($"git checkout -b master origin/master"); //bat.AddCheckout("master", "origin/master"); //bat.AddCheckout("release", "origin/release"); bat.AddCD(startingFolder); bat.Write(); bat.GitRun(startingFolder, HttpContext.Session.GetString(Session.USER), HttpContext.Session.GetString(Session.GIT)); return(Json(new JsonMessage(true, "Success"))); } } else { return(Json(new JsonMessage(false, $"Unable to find starting folder {startingFolder}"))); } } else { return(Json(new JsonMessage(false, $"Unable to find starting folder {startingFolder}"))); } } catch (System.Exception ex) { return(Json(new JsonMessage(false, $"{repoName} - Failed to clone - [{ex.Message}]"))); } }
public JsonResult DeleteBranch([DataSourceRequest] DataSourceRequest dsRequest, RepoBranchVM branch) { if (branch != null && ModelState.IsValid) { var branchPath = branch.Name.Split("/"); var canContinue = true; foreach (var bp in branchPath) { if (bp == "release" || bp == "master" || bp == "develop" || bp == "tags") { canContinue = false; break; } } if (!canContinue) { return(Json(new JsonMessage(true, "Cannot delete this branch"))); } else { var startingFolder = string.Empty; CreateStartingFolder(ref startingFolder); if (System.IO.Directory.Exists(startingFolder)) { CreateStartingProjectFolder(ref startingFolder); if (System.IO.Directory.Exists(startingFolder)) { var vsts = new Classes.VSTS.Vsts(HttpContext.Session.GetString(Session.TOKEN)); var selectedRepo = vsts.GetRepositoryOnGuid(branch.RepoGuid); var repoName = selectedRepo.Name; var mainBat = startingFolder + @"\DeleteFeatureBranch.bat"; if (System.IO.File.Exists(mainBat)) { System.IO.File.Delete(mainBat); } var bat = new Classes.GitBatch(mainBat) { Redirect = true, RedirectError = $@"{startingFolder.Replace(@"\\", @"\")}\ErrorDelete.txt", RedirectFile = $@"{startingFolder.Replace(@"\\", @"\")}\OutputDelete.txt" }; bat.AddLine($"echo start"); var newName = branch.Name.Replace("refs/heads/", string.Empty); bat.AddCD($@"{startingFolder}\{repoName}"); bat.AddReset(); bat.AddCheckout("master", "master"); bat.AddLine($@"git branch -D {newName}"); // Remove from local bat.AddLine($@"git push origin --delete {newName}"); // Remove from origin bat.AddCD(startingFolder); bat.Write(); bat.GitRun(startingFolder, HttpContext.Session.GetString(Session.USER), HttpContext.Session.GetString(Session.GIT)); return(Json(new JsonMessage(true, "Branch Deleted"))); } else { return(Json(new JsonMessage(true, $"Unable to find starting folder {startingFolder}"))); } } else { return(Json(new JsonMessage(true, $"Unable to find starting folder {startingFolder}"))); } } } else { return(Json(new JsonMessage(true, "Invalid Model State"))); } }
public IActionResult GetRepoBranches([DataSourceRequest] DataSourceRequest request, string id) { var r = GetRepositoryBranchesAndStats(id); if (r == null) { // var asd = "Stop here"; } var startingFolder = string.Empty; var repoName = string.Empty; var currentBranch = string.Empty; try { CreateStartingFolder(ref startingFolder); // See or create sub-folder based on PROJECT name if (System.IO.Directory.Exists(startingFolder)) { CreateStartingProjectFolder(ref startingFolder); var mainBat = startingFolder + @"\WhichBranch.bat"; if (System.IO.File.Exists(mainBat)) { System.IO.File.Delete(mainBat); } var bat = new Classes.GitBatch(mainBat) { Redirect = true, RedirectError = $@"{startingFolder.Replace(@"\\", @"\")}\ErrorWB.txt", RedirectFile = $@"{startingFolder.Replace(@"\\", @"\")}\OutputFoo.txt" }; var vsts = new Classes.VSTS.Vsts(HttpContext.Session.GetString(Session.TOKEN)); var repo = vsts.GetRepositoryOnGuid(id); repoName = repo.Name; int ndx = repo.RemoteUrl.LastIndexOf(@"/"); string theFile = repo.RemoteUrl.Substring(ndx + 1); bat.AddLine($"echo Working on '{theFile}'"); bat.AddCD(startingFolder); bat.AddLine($@"git clone {repo.RemoteUrl}"); bat.AddCD($@"{startingFolder}\{theFile}"); bat.AddLine("git branch"); bat.Write(); bat.GitRun(startingFolder, HttpContext.Session.GetString(Session.USER), HttpContext.Session.GetString(Session.GIT)); // No error so parse file var lines = System.IO.File.ReadAllLines($@"{startingFolder.Replace(@"\\", @"\")}\OutputFoo.txt"); foreach (var line in lines) { if (line.StartsWith("*")) { currentBranch = line.Substring(2); } } } } catch (System.Exception ex) { System.Diagnostics.Debug.Write(ex.Message); currentBranch = string.Empty; } List <RepoBranchVM> branches = new List <RepoBranchVM>(); foreach (var b in r.Branches) { var item = new RepoBranchVM() { Statuses = new List <RepoBranchStatusVM>(), RepoGuid = id, Guid = b.Guid, Name = b.Name.Replace("refs/heads/", string.Empty), Url = b.Url, Category = b.Name.Contains(@"tags/") ? "TAG" : b.Name.Contains(@"pull/") ? "PULL" : "BRANCH", IsLocked = false, IsCurrentBranch = false, LockedBy = string.Empty }; if (!string.IsNullOrEmpty(b.IsLocked)) { item.IsLocked = b.IsLocked.ToUpper() == "TRUE"; item.LockedBy = b.Locked.Email; } item.HasStats = (b.Statuses.Count != 0); if (!string.IsNullOrEmpty(currentBranch)) { if (item.Name.ToUpper() == currentBranch.ToUpper()) { item.IsCurrentBranch = true; } } foreach (var s in b.Statuses) { var stat = new RepoBranchStatusVM() { CreatedBy = s.CreatedBy.By, CreatedDate = System.Convert.ToDateTime(s.CreationDate).ToShortDateString(), Description = s.Description, Id = s.Id, State = s.State }; item.Statuses.Add(stat); } branches.Add(item); } return(Json(branches.ToDataSourceResult(request))); }
public JsonResult CheckOutBranch(string name, string guid) { var startingFolder = string.Empty; var repoName = string.Empty; name = name.Replace("refs/heads/", string.Empty); var vsts = new Classes.VSTS.Vsts(HttpContext.Session.GetString(Session.TOKEN)); var selectedRepo = vsts.GetRepositoryOnGuid(guid); repoName = selectedRepo.Name; try { CreateStartingFolder(ref startingFolder); if (System.IO.Directory.Exists(startingFolder)) { CreateStartingProjectFolder(ref startingFolder); var mainBat = startingFolder + @"\CheckOutBranch.bat"; if (System.IO.File.Exists(mainBat)) { System.IO.File.Delete(mainBat); } var bat = new Classes.GitBatch(mainBat) { Redirect = true, RedirectError = $@"{startingFolder.Replace(@"\\", @"\")}\Error.txt", RedirectFile = $@"{startingFolder.Replace(@"\\", @"\")}\output.txt" }; bat.AddLine($"echo start"); bat.AddLine($"echo Working on '{name}'"); if (System.IO.Directory.Exists($"{startingFolder}\\{repoName}")) { bat.AddCD($@"{startingFolder}\{repoName}"); bat.AddReset(); bat.AddCheckout(name, $"origin/{name}"); bat.AddCD(startingFolder); bat.Write(); bat.GitRun(startingFolder, HttpContext.Session.GetString(Session.USER), HttpContext.Session.GetString(Session.GIT)); return(Json(new JsonMessage(true, name + " : Pulled"))); } else { return(Json(new JsonMessage(false, " : FAILED - you need to clone repository first"))); } } else { return(Json(new JsonMessage(false, " : FAILED - you need to clone repository first"))); } } catch (System.Exception ex) { return(Json(new JsonMessage(false, $"{repoName} - Failed to check out - [{ex.Message}]"))); } }