/// <inheritdoc />
        public Models.Commit GetInitialCommit(string owner, string repository)
        {
            List <AltinnCore.Common.Models.Commit> commits = new List <Models.Commit>();
            string localServiceRepoFolder = _settings.GetServicePath(owner, repository, AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext));

            using (var repo = new Repository(localServiceRepoFolder))
            {
                LibGit2Sharp.Commit firstCommit = repo.Commits.First();
                Models.Commit       commit      = new Models.Commit();
                commit.Message      = firstCommit.Message;
                commit.MessageShort = firstCommit.MessageShort;
                commit.Encoding     = firstCommit.Encoding;
                commit.Sha          = firstCommit.Sha;

                commit.Author       = new Models.Signature();
                commit.Author.Email = firstCommit.Author.Email;
                commit.Author.Name  = firstCommit.Author.Name;
                commit.Author.When  = firstCommit.Author.When;

                commit.Comitter       = new Models.Signature();
                commit.Comitter.Name  = firstCommit.Committer.Name;
                commit.Comitter.Email = firstCommit.Committer.Email;
                commit.Comitter.When  = firstCommit.Committer.When;

                return(commit);
            }
        }
        /// <summary>
        /// List commits
        /// </summary>
        /// <param name="owner">The owner of the repository</param>
        /// <param name="repository">The name of the repository</param>
        /// <returns>List of commits</returns>
        public List <AltinnCore.Common.Models.Commit> Log(string owner, string repository)
        {
            List <AltinnCore.Common.Models.Commit> commits = new List <Models.Commit>();
            string localServiceRepoFolder = _settings.GetServicePath(owner, repository, AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext));
            var    watch = System.Diagnostics.Stopwatch.StartNew();

            using (var repo = new Repository(localServiceRepoFolder))
            {
                foreach (LibGit2Sharp.Commit c in repo.Commits.Take(50))
                {
                    Models.Commit commit = new Models.Commit();
                    commit.Message      = c.Message;
                    commit.MessageShort = c.MessageShort;
                    commit.Encoding     = c.Encoding;
                    commit.Sha          = c.Sha;

                    commit.Author       = new Models.Signature();
                    commit.Author.Email = c.Author.Email;
                    commit.Author.Name  = c.Author.Name;
                    commit.Author.When  = c.Author.When;

                    commit.Comitter       = new Models.Signature();
                    commit.Comitter.Name  = c.Committer.Name;
                    commit.Comitter.Email = c.Committer.Email;
                    commit.Comitter.When  = c.Committer.When;

                    commits.Add(commit);
                }
            }

            watch.Stop();
            _logger.Log(Microsoft.Extensions.Logging.LogLevel.Information, "Get commits - {0} ", watch.ElapsedMilliseconds);

            return(commits);
        }
        /// <summary>
        /// List commits
        /// </summary>
        /// <param name="owner">The owner of the repository</param>
        /// <param name="repository">The name of the repository</param>
        /// <returns>List of commits</returns>
        public List <AltinnCore.Common.Models.Commit> Log(string owner, string repository)
        {
            List <AltinnCore.Common.Models.Commit> commits = new List <Models.Commit>();
            string localServiceRepoFolder = _settings.GetServicePath(owner, repository, AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext));

            using (var repo = new Repository(localServiceRepoFolder))
            {
                foreach (LibGit2Sharp.Commit c in repo.Commits.Take(50))
                {
                    Models.Commit commit = new Models.Commit();
                    commit.Message      = c.Message;
                    commit.MessageShort = c.MessageShort;
                    commit.Encoding     = c.Encoding;
                    commit.Sha          = c.Sha;

                    commit.Author       = new Models.Signature();
                    commit.Author.Email = c.Author.Email;
                    commit.Author.Name  = c.Author.Name;
                    commit.Author.When  = c.Author.When;

                    commit.Comitter       = new Models.Signature();
                    commit.Comitter.Name  = c.Committer.Name;
                    commit.Comitter.Email = c.Committer.Email;
                    commit.Comitter.When  = c.Committer.When;

                    commits.Add(commit);
                }
            }

            return(commits);
        }
        public async Task <int> AddCommitAsync(CommitDto commit, User user, Repository repo)
        {
            var commiter = dbContext.Users.Where(x => x.Name == commit.CommiterName).FirstOrDefault();

            if (commiter == null)
            {
                commiter = new Models.User()
                {
                    Name = commit.CommiterName
                };
                await dbContext.Users.AddAsync(commiter);
            }
            ;

            var comm = dbContext.Commits?.Where(x => x.Sha == commit.Sha).FirstOrDefault();

            if (comm == null)
            {
                comm = new Models.Commit()
                {
                    Sha = commit.Sha, Commiter = commiter, Repository = repo, User = user, Message = commit.Message
                };
                await dbContext.Commits.AddAsync(comm);
            }
            else
            {
                comm.Commiter   = commiter;
                comm.Repository = repo;
                comm.User       = user;
                comm.Message    = commit.Message;
                dbContext.Commits.Update(comm);
            }

            return(await dbContext.SaveChangesAsync());
        }
Exemple #5
0
        public async Task AddCommitAsync(Guid repositoryId, Guid branchId, Models.Commit commit)
        {
            var branches = await _branchService.GetBranchFromRepositoryAsync(repositoryId);

            branches.FirstOrDefault(b => b.Id == branchId).Commits.Add(commit);
            var repo = await _repositoriesService.GetRepositoryAsync(repositoryId).ConfigureAwait(false);

            repo.Branches = branches;
            await _context.Repositories.ReplaceOneAsync(r => r.Id == repositoryId, repo);
        }
Exemple #6
0
        public async Task <Tuple <CommitDto, string> > GitCommitAsync(string message, Guid repositoryId, IEnumerable <string> files)
        {
            CommitDto commitDto = new CommitDto();
            var       error     = string.Empty;
            await Task.Run(async() =>
            {
                var repository = await _repositoriesService.GetRepositoryAsync(repositoryId).ConfigureAwait(false);
                var branch     = await _gitService.GetCurrentBranchAsync(repository.Path).ConfigureAwait(false);
                var branchId   = await _branchService.GetBranchIdAsync(repositoryId, branch.Output).ConfigureAwait(false);
                await _gitService.StageAsync(repository.Path, files).ConfigureAwait(false);
                var commit = await _gitService.CommitAsync(repository.Path, message).ConfigureAwait(false);
                error      = await GitParser.ParseError(commit.Error);
                if (string.IsNullOrWhiteSpace(error))
                {
                    var lastCommit = await _gitService.LastCommitAsync(repository.Path).ConfigureAwait(false);
                    var addCommit  = new Models.Commit()
                    {
                        Id          = Guid.NewGuid(),
                        Message     = message,
                        GitCommitId = await GitParser.ParseCommitIdAsync(lastCommit.Output).ConfigureAwait(false),
                        UserEmail   = await GitParser.ParseCommitAuthorAsync(lastCommit.Output).ConfigureAwait(false),
                        Time        = DateTime.Now.Ticks
                    };
                    await _commitService.AddCommitAsync(repositoryId, branchId, addCommit).ConfigureAwait(false);
                    commitDto.Branch      = branch.Output;
                    commitDto.Author      = addCommit.UserEmail;
                    commitDto.Description = addCommit.Message;
                    commitDto.Time        = new DateTime(addCommit.Time);
                    commitDto.Id          = addCommit.GitCommitId;
                    error = null;
                }
                else
                {
                    commitDto = null;
                }
            }).ConfigureAwait(false);

            var result = Tuple.Create(commitDto, error);

            return(result);
        }