コード例 #1
0
        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));
        }
コード例 #2
0
        private void CheckForNewRepositories()
        {
            if (!Directory.Exists(UserConfiguration.Current.Repositories))
            {
                Log.Error($"Repo root doesn't exist: {UserConfiguration.Current.Repositories}");
                // We don't want an exception if the repo dir no longer exists,
                // as this would make it impossible to start the server
                return;
            }
            IEnumerable <string> directories = Directory.EnumerateDirectories(UserConfiguration.Current.Repositories, "*.git", SearchOption.AllDirectories);

            foreach (string directory in directories)
            {
                var repoPath = directory.Remove(0, UserConfiguration.Current.Repositories.Length).TrimStart('\\');
                var rootDir  = repoPath.Split('\\').FirstOrDefault();

                Log.Debug($"Repo {repoPath}");

                if (DoesControllerExistConstraint.DoesControllerExist(rootDir))
                {
                    continue; //Do not load as a valid repo
                }
                RepositoryModel repository = _repositoryRepository.GetRepository(repoPath);
                if (repository == null)
                {
                    if (LibGit2Sharp.Repository.IsValid(directory))
                    {
                        repository                 = new RepositoryModel();
                        repository.Id              = Guid.NewGuid();
                        repository.Description     = "Discovered in file system.";
                        repository.Name            = repoPath;
                        repository.AnonymousAccess = false;
                        repository.Users           = new UserModel[0];
                        repository.Teams           = new TeamModel[0];
                        repository.Administrators  = new UserModel[0];
                        if (repository.NameIsValid)
                        {
                            _repositoryRepository.Create(repository);
                        }
                    }
                }
            }
        }