예제 #1
0
        public static GitBranch GitCreateBranch(
            this ICakeContext context,
            DirectoryPath repositoryDirectoryPath,
            string branchName)
        {
            var defaultSettings = new GitBranchSettings();

            return(GitCreateBranch(context, repositoryDirectoryPath, branchName, defaultSettings));
        }
예제 #2
0
        public static GitBranch GitCreateBranch(
            this ICakeContext context,
            DirectoryPath repositoryDirectoryPath,
            string branchName,
            GitBranchSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (repositoryDirectoryPath == null)
            {
                throw new ArgumentNullException(nameof(repositoryDirectoryPath));
            }

            return(context.UseRepository(
                       repositoryDirectoryPath,
                       repository => {
                var head = repository.Head.Tip;

                context.Log.Write(
                    Core.Diagnostics.Verbosity.Normal,
                    Core.Diagnostics.LogLevel.Information,
                    "Going to create a new branch with name {0}",
                    branchName);

                //foreach(var branch in repository.Branches)
                //{
                //    context.Log.Write(
                //        Core.Diagnostics.Verbosity.Normal,
                //        Core.Diagnostics.LogLevel.Information,
                //        "Existing branch: {0}",
                //        branch.CanonicalName);
                //}

                var newBranch = repository
                                .Branches
                                .Add(branchName, head, settings.AllowOverwrite);

                context.Log.Write(
                    Core.Diagnostics.Verbosity.Normal,
                    Core.Diagnostics.LogLevel.Information,
                    "Branch with name {0} created",
                    branchName);

                if (settings.TrackOnRemote)
                {
                    repository
                    .Branches
                    .Update(newBranch, updater =>
                    {
                        updater.TrackedBranch = $"refs/remotes/{settings.Remote.Name}/{settings.Remote.BranchName ?? branchName}";
                    });
                }

                if (settings.CheckoutNewBranch)
                {
                    Commands.Checkout(repository, newBranch);
                }

                return new GitBranch(repository);
            }));
        }