示例#1
0
        private GitCommit CreateNewCommit(GitCommit parentCommit, string baseTreeSha, List<GitTreeItem> treeItems, string commitMessage)
        {
            // create new tree
            GitCreateTree newTree = new GitCreateTree
            {
                BaseTree = baseTreeSha,
                Tree = treeItems.ToArray()
            };
            GitTree tree = m_gitHubClient.CreateTree(m_user, m_repo, newTree);
            if (tree == null)
                throw new WatcherException("Couldn't create new tree.");
            Log.Debug("Created new tree: {0}.", tree.Sha);

            // create a commit
            GitCreateCommit createCommit = new GitCreateCommit
            {
                Message = commitMessage,
                Parents = new[] { parentCommit.Sha },
                Tree = tree.Sha,
            };
            GitCommit newCommit = m_gitHubClient.CreateCommit(m_user, m_repo, createCommit);
            if (newCommit == null)
                throw new WatcherException("Couldn't create new commit.");
            return newCommit;
        }
示例#2
0
        private void UpdateSubmodules(GitCommit buildRepoCommit, GitTree buildRepoTree, bool rewriteGitModules)
        {
            // copy most items from the existing commit tree
            const string gitModulesPath = ".gitmodules";
            List<GitTreeItem> treeItems = buildRepoTree.Items
                .Where(x => (x.Type != "commit" && x.Type != "blob") || (x.Type == "blob" && (!rewriteGitModules || x.Path != gitModulesPath))).ToList();

            // add all the submodules
            treeItems.AddRange(m_submodules.Select(x => new GitTreeItem
            {
                Mode = "160000",
                Path = x.Key,
                Sha = x.Value.LatestCommitId,
                Type = "commit",
            }));

            if (rewriteGitModules)
            {
                // create the contents of the .gitmodules file
                string gitModules;
                using (StringWriter sw = new StringWriter { NewLine = "\n" })
                {
                    foreach (var pair in m_submodules.OrderBy(x => x.Key, StringComparer.Ordinal))
                    {
                        sw.WriteLine("[submodule \"{0}\"]", pair.Key);
                        sw.WriteLine("\tpath = {0}", pair.Key);
                        sw.WriteLine("\turl = {0}", pair.Value.Url);
                    }
                    gitModules = sw.ToString();
                }

                // create the blob
                GitBlob gitModulesBlob = CreateBlob(gitModules);
                treeItems.Add(new GitTreeItem
                {
                    Mode = "100644",
                    Path = gitModulesPath,
                    Sha = gitModulesBlob.Sha,
                    Type = "blob",
                });
            }

            const string commitMessage = "Update submodules to match Leeroy configuration.";
            GitCommit newCommit = CreateNewCommit(buildRepoCommit, null, treeItems, commitMessage);
            Log.Info("Created new commit for synced submodules: {0}; moving branch.", newCommit.Sha);

            // advance the branch pointer to the new commit
            if (TryAdvanceBranch(newCommit.Sha))
            {
                Log.Info("Build repo updated successfully to commit {0}.", newCommit.Sha);
                m_lastBuildCommitId = newCommit.Sha;
            }
        }
示例#3
0
        // Loads the desired list of submodules from the configuration file.
        private void SyncSubmodules(GitCommit buildRepoCommit, GitTree buildRepoTree)
        {
            ReadSubmodulesFromConfig();

            // find submodules that should be present in the build repo, but aren't
            bool updateSubmodules = false;
            foreach (KeyValuePair<string, Submodule> pair in m_submodules)
            {
                string path = pair.Key;
                Submodule submodule = pair.Value;

                GitTreeItem submoduleItem = buildRepoTree.Items.FirstOrDefault(x => x.Type == "commit" && x.Mode == "160000" && x.Path == path);
                if (submoduleItem != null)
                {
                    submodule.LatestCommitId = submoduleItem.Sha;
                }
                else
                {
                    submodule.LatestCommitId = m_gitHubClient.GetLatestCommitId(submodule.User, submodule.Repo, submodule.Branch);
                    if (submodule.LatestCommitId == null)
                        throw new WatcherException("Submodule '{0}' doesn't have a latest commit for branch '{1}'; will stop monitoring project.".FormatInvariant(pair.Key, submodule.Branch));
                    Log.Info("Submodule at path '{0}' is missing; it will be added.", pair.Key);
                    updateSubmodules = true;
                }
            }

            // find extra submodules that aren't in the configuration file
            List<string> extraSubmodulePaths = buildRepoTree.Items
                .Where(x => x.Type == "commit" && x.Mode == "160000" && !m_submodules.ContainsKey(x.Path))
                .Select(x => x.Path)
                .ToList();
            if (extraSubmodulePaths.Count != 0)
            {
                Log.Info("Extra submodule paths: {0}".FormatInvariant(string.Join(", ", extraSubmodulePaths)));
                updateSubmodules = true;
            }

            // determine if .gitmodules needs to be updated
            bool rewriteGitModules = false;
            string gitModulesBlob = ReadGitModulesBlob(buildRepoTree);
            using (StringReader reader = new StringReader(gitModulesBlob ?? ""))
            {
                var existingGitModules = ParseConfigFile(reader)
                    .Where(x => x.Key.StartsWith("submodule ", StringComparison.Ordinal))
                    .Select(x => new { Path = x.Value["path"], Url = x.Value["url"] })
                    .OrderBy(x => x.Path, StringComparer.Ordinal)
                    .ToList();
                var desiredGitModules = m_submodules.Select(x => new { Path = x.Key, x.Value.Url }).OrderBy(x => x.Path, StringComparer.Ordinal).ToList();

                if (!existingGitModules.SequenceEqual(desiredGitModules))
                {
                    Log.Info(".gitmodules needs to be updated.");
                    rewriteGitModules = true;
                }
            }

            if (updateSubmodules || rewriteGitModules)
                UpdateSubmodules(buildRepoCommit, buildRepoTree, rewriteGitModules);
        }