public ActionResult Edit(RepositoryDetailModel model)
 {
     if (ModelState.IsValid)
     {
         if (model.Administrators.Contains(User.Id(), StringComparer.OrdinalIgnoreCase))
         {
             RepositoryRepository.Update(ConvertRepositoryDetailModel(model));
             ViewBag.UpdateSuccess = true;
         }
         else
         {
             ModelState.AddModelError("Administrators", Resources.Repository_Edit_CantRemoveYourself);
         }
     }
     PopulateEditData();
     return View(model);
 }
        public ActionResult Create()
        {
            if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfiguration.Current.AllowUserRepositoryCreation)
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            var model = new RepositoryDetailModel
            {
                Administrators = new string[] { User.Id() },
            };
            PopulateEditData();
            return View(model);
        }
 private RepositoryModel ConvertRepositoryDetailModel(RepositoryDetailModel model)
 {
     return model == null ? null : new RepositoryModel
     {
         Name = model.Name,
         Group = model.Group,
         Description = model.Description,
         Users = model.Users,
         Administrators = model.Administrators,
         Teams = model.Teams,
         AnonymousAccess = model.AllowAnonymous,
         AuditPushUser = model.AuditPushUser,
         Logo = model.Logo != null ? model.Logo.BinaryData : null,
         RemoveLogo = model.Logo != null && model.Logo.RemoveLogo
     };
 }
        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);
        }
 public ActionResult Delete(RepositoryDetailModel model)
 {
     if (model != null && !String.IsNullOrEmpty(model.Name))
     {
         string path = Path.Combine(UserConfiguration.Current.Repositories, model.Name);
         if (Directory.Exists(path))
         {
             DeleteFileSystemInfo(new DirectoryInfo(path));
         }
         RepositoryRepository.Delete(model.Name);
         TempData["DeleteSuccess"] = true;
     }
     return RedirectToAction("Index");
 }
        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 (String.IsNullOrEmpty(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure);
            }
            else if (ModelState.IsValid)
            {
                if (RepositoryRepository.Create(ConvertRepositoryDetailModel(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;
                        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);
        }