public ActionResult Clone(string id, RepositoryDetailModel model) { if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfiguration.Current.AllowUserRepositoryCreation) { return(RedirectToAction("Unauthorized", "Home")); } if (model != null && !String.IsNullOrEmpty(model.Name)) { model.Name = Regex.Replace(model.Name, @"\s", ""); } if (String.IsNullOrEmpty(model.Name)) { ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure); } else if (ModelState.IsValid) { if (RepositoryRepository.Create(ConvertRepositoryDetailModel(model))) { string targetRepositoryPath = Path.Combine(UserConfiguration.Current.Repositories, model.Name); if (!Directory.Exists(targetRepositoryPath)) { string sourceRepositoryPath = Path.Combine(UserConfiguration.Current.Repositories, id); LibGit2Sharp.CloneOptions options = new LibGit2Sharp.CloneOptions() { IsBare = true, Checkout = false }; LibGit2Sharp.Repository.Clone(sourceRepositoryPath, targetRepositoryPath, options); using (var repo = new LibGit2Sharp.Repository(targetRepositoryPath)) { if (repo.Network.Remotes.Any(r => r.Name == "origin")) { repo.Network.Remotes.Remove("origin"); } } TempData["CloneSuccess"] = true; return(RedirectToAction("Index")); } else { RepositoryRepository.Delete(model.Name); ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists); } } else { ModelState.AddModelError("", Resources.Repository_Create_Failure); } } ViewBag.ID = id; PopulateEditData(); return(View(model)); }
private bool TryCreateOnPush(string repositoryName) { DirectoryInfo directory = GetDirectoryInfo(repositoryName); if (directory.Exists) { // We can't create a new repo - there's already a directory with that name Log.Warning("GitC: Can't create {RepositoryName} - directory already exists", repositoryName); return(false); } RepositoryModel repository = new RepositoryModel(); repository.Name = repositoryName; if (!repository.NameIsValid) { // We don't like this name Log.Warning("GitC: Can't create '{RepositoryName}' - name is invalid", repositoryName); return(false); } var user = MembershipService.GetUserModel(User.Id()); repository.Description = "Auto-created by push for " + user.DisplayName; repository.AnonymousAccess = false; repository.Administrators = new[] { user }; if (!RepositoryRepository.Create(repository)) { // We can't add this to the repo store Log.Warning("GitC: Can't create '{RepositoryName}' - RepoRepo.Create failed", repositoryName); return(false); } Repository.Init(Path.Combine(UserConfiguration.Current.Repositories, repository.Name), true); Log.Information("GitC: '{RepositoryName}' created", repositoryName); return(true); }
public ActionResult Create(RepositoryDetailModel model) { if (!RepositoryPermissionService.HasCreatePermission(User.Id())) { return(RedirectToAction("Unauthorized", "Home")); } bool isAControllerPath = false; if (model != null && !string.IsNullOrEmpty(model.Name)) { model.Name = Regex.Replace(model.Name, @"\s", ""); model.Name = model.Name.Replace('/', '\\'); var rootDir = model.Name.Split('\\').FirstOrDefault(); isAControllerPath = DoesControllerExistConstraint.DoesControllerExist(rootDir); } if (model != null && string.IsNullOrEmpty(model.Name)) { ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure); } else if (model != null && !model.Name.Contains(".git")) { ModelState.AddModelError("Name", Resources.Repository_Create_NameExtensionFailure); } else if (model != null && isAControllerPath) { ModelState.AddModelError("Name", Resources.Repository_Create_IsAControllerNameFailure); } else if (ModelState.IsValid) { var repo_model = ConvertRepositoryDetailModel(model); if (RepositoryRepository.Create(repo_model)) { string path = Path.Combine(UserConfiguration.Current.Repositories, model.Name); if (!Directory.Exists(path)) { LibGit2Sharp.Repository.Init(path, true); TempData["CreateSuccess"] = true; TempData["SuccessfullyCreatedRepositoryName"] = model.Name; TempData["SuccessfullyCreatedRepositoryId"] = repo_model.Id; return(RedirectToAction("Index")); } else { RepositoryRepository.Delete(model.Id); ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists); } } else { ModelState.AddModelError("", Resources.Repository_Create_Failure); } } PopulateCheckboxListData(ref model); return(View(model)); }
public ActionResult Create(RepositoryDetailModel model) { if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfiguration.Current.AllowUserRepositoryCreation) { return(RedirectToAction("Unauthorized", "Home")); } if (model != null && !String.IsNullOrEmpty(model.Name)) { model.Name = Regex.Replace(model.Name, @"\s", ""); } if (model != null && String.IsNullOrEmpty(model.Name)) { ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure); } else if (ModelState.IsValid) { var repo_model = ConvertRepositoryDetailModel(model); if (RepositoryRepository.Create(repo_model)) { string path = Path.Combine(UserConfiguration.Current.Repositories, model.Name); if (!Directory.Exists(path)) { LibGit2Sharp.Repository.Init(path, true); TempData["CreateSuccess"] = true; TempData["SuccessfullyCreatedRepositoryName"] = model.Name; TempData["SuccessfullyCreatedRepositoryId"] = repo_model.Id; return(RedirectToAction("Index")); } else { RepositoryRepository.Delete(model.Id); ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists); } } else { ModelState.AddModelError("", Resources.Repository_Create_Failure); } } PopulateCheckboxListData(ref model); return(View(model)); }
public ActionResult Create(RepositoryDetailModel model) { if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfigurationManager.AllowUserRepositoryCreation) { return(RedirectToAction("Unauthorized", "Home")); } if (model != null && !String.IsNullOrEmpty(model.Name)) { model.Name = Regex.Replace(model.Name, @"\s", ""); } if (String.IsNullOrEmpty(model.Name)) { ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure); } else if (ModelState.IsValid) { if (RepositoryRepository.Create(ConvertRepositoryDetailModel(model))) { string path = Path.Combine(UserConfigurationManager.Repositories, model.Name); if (!Directory.Exists(path)) { using (var repository = new GitSharp.Core.Repository(new DirectoryInfo(path))) { repository.Create(true); } TempData["CreateSuccess"] = true; return(RedirectToAction("Index")); } else { RepositoryRepository.Delete(model.Name); ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists); } } else { ModelState.AddModelError("", Resources.Repository_Create_Failure); } } PopulateEditData(); return(View(model)); }
public ActionResult Clone(Guid id, RepositoryDetailModel model) { if (!RepositoryPermissionService.HasCreatePermission(User.Id())) { return(RedirectToAction("Unauthorized", "Home")); } if (model != null && !String.IsNullOrEmpty(model.Name)) { model.Name = Regex.Replace(model.Name, @"\s", ""); } if (model != null && String.IsNullOrEmpty(model.Name)) { ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure); } else if (ModelState.IsValid) { var repo_model = ConvertRepositoryDetailModel(model); if (RepositoryRepository.Create(repo_model)) { string targetRepositoryPath = Path.Combine(UserConfiguration.Current.Repositories, model.Name); if (!Directory.Exists(targetRepositoryPath)) { var source_repo = RepositoryRepository.GetRepository(id); string sourceRepositoryPath = Path.Combine(UserConfiguration.Current.Repositories, source_repo.Name); LibGit2Sharp.CloneOptions options = new LibGit2Sharp.CloneOptions() { IsBare = true, Checkout = false }; LibGit2Sharp.Repository.Clone(sourceRepositoryPath, targetRepositoryPath, options); using (var repo = new LibGit2Sharp.Repository(targetRepositoryPath)) { if (repo.Network.Remotes.Any(r => r.Name == "origin")) { repo.Network.Remotes.Remove("origin"); } } TempData["CloneSuccess"] = true; return(RedirectToAction("Index")); } else { RepositoryRepository.Delete(model.Id); ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists); } } else { ModelState.AddModelError("", Resources.Repository_Create_Failure); } } ViewBag.ID = id; PopulateCheckboxListData(ref model); return(View(model)); }