Ejemplo n.º 1
0
 /// <summary>Execute the SubmoduleUpdateCommand command.</summary>
 /// <remarks>Execute the SubmoduleUpdateCommand command.</remarks>
 /// <returns>a collection of updated submodule paths</returns>
 /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException">NGit.Api.Errors.ConcurrentRefUpdateException
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.CheckoutConflictException">NGit.Api.Errors.CheckoutConflictException
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.InvalidMergeHeadsException">NGit.Api.Errors.InvalidMergeHeadsException
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.InvalidConfigurationException">NGit.Api.Errors.InvalidConfigurationException
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.NoHeadException">NGit.Api.Errors.NoHeadException
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.NoMessageException">NGit.Api.Errors.NoMessageException
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.RefNotFoundException">NGit.Api.Errors.RefNotFoundException
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.WrongRepositoryStateException">NGit.Api.Errors.WrongRepositoryStateException
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.GitAPIException">NGit.Api.Errors.GitAPIException
 ///     </exception>
 public override ICollection <string> Call()
 {
     CheckCallable();
     try
     {
         SubmoduleWalk generator = SubmoduleWalk.ForIndex(repo);
         if (!paths.IsEmpty())
         {
             generator.SetFilter(PathFilterGroup.CreateFromStrings(paths));
         }
         IList <string> updated = new AList <string>();
         while (generator.Next())
         {
             // Skip submodules not registered in .gitmodules file
             if (generator.GetModulesPath() == null)
             {
                 continue;
             }
             // Skip submodules not registered in parent repository's config
             string url = generator.GetConfigUrl();
             if (url == null)
             {
                 continue;
             }
             Repository submoduleRepo = generator.GetRepository();
             // Clone repository is not present
             if (submoduleRepo == null)
             {
                 CloneCommand clone = Git.CloneRepository();
                 Configure(clone);
                 clone.SetURI(url);
                 clone.SetDirectory(generator.GetDirectory());
                 if (monitor != null)
                 {
                     clone.SetProgressMonitor(monitor);
                 }
                 submoduleRepo = clone.Call().GetRepository();
             }
             try
             {
                 RevWalk   walk   = new RevWalk(submoduleRepo);
                 RevCommit commit = walk.ParseCommit(generator.GetObjectId());
                 string    update = generator.GetConfigUpdate();
                 if (ConfigConstants.CONFIG_KEY_MERGE.Equals(update))
                 {
                     MergeCommand merge = new MergeCommand(submoduleRepo);
                     merge.Include(commit);
                     merge.Call();
                 }
                 else
                 {
                     if (ConfigConstants.CONFIG_KEY_REBASE.Equals(update))
                     {
                         RebaseCommand rebase = new RebaseCommand(submoduleRepo);
                         rebase.SetUpstream(commit);
                         rebase.Call();
                     }
                     else
                     {
                         // Checkout commit referenced in parent repository's
                         // index as a detached HEAD
                         DirCacheCheckout co = new DirCacheCheckout(submoduleRepo, submoduleRepo.LockDirCache
                                                                        (), commit.Tree);
                         co.SetFailOnConflict(true);
                         co.Checkout();
                         RefUpdate refUpdate = submoduleRepo.UpdateRef(Constants.HEAD, true);
                         refUpdate.SetNewObjectId(commit);
                         refUpdate.ForceUpdate();
                     }
                 }
             }
             finally
             {
                 submoduleRepo.Close();
             }
             updated.AddItem(generator.GetPath());
         }
         return(updated);
     }
     catch (IOException e)
     {
         throw new JGitInternalException(e.Message, e);
     }
     catch (ConfigInvalidException e)
     {
         throw new InvalidConfigurationException(e.Message, e);
     }
 }
Ejemplo n.º 2
0
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        public override Repository Call()
        {
            CheckCallable();
            if (path == null || path.Length == 0)
            {
                throw new ArgumentException(JGitText.Get().pathNotConfigured);
            }
            if (uri == null || uri.Length == 0)
            {
                throw new ArgumentException(JGitText.Get().uriNotConfigured);
            }
            try
            {
                if (SubmoduleExists())
                {
                    throw new JGitInternalException(MessageFormat.Format(JGitText.Get().submoduleExists
                                                                         , path));
                }
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            string resolvedUri;

            try
            {
                resolvedUri = SubmoduleWalk.GetSubmoduleRemoteUrl(repo, uri);
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            // Clone submodule repository
            FilePath     moduleDirectory = SubmoduleWalk.GetSubmoduleDirectory(repo, path);
            CloneCommand clone           = Git.CloneRepository();

            Configure(clone);
            clone.SetDirectory(moduleDirectory);
            clone.SetURI(resolvedUri);
            if (monitor != null)
            {
                clone.SetProgressMonitor(monitor);
            }
            Repository subRepo = clone.Call().GetRepository();
            // Save submodule URL to parent repository's config
            StoredConfig config = repo.GetConfig();

            config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.
                             CONFIG_KEY_URL, resolvedUri);
            try
            {
                config.Save();
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            // Save path and URL to parent repository's .gitmodules file
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(repo.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), repo.FileSystem);

            try
            {
                modulesConfig.Load();
                modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                        .CONFIG_KEY_PATH, path);
                modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                        .CONFIG_KEY_URL, uri);
                modulesConfig.Save();
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            catch (ConfigInvalidException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            AddCommand add = new AddCommand(repo);

            // Add .gitmodules file to parent repository's index
            add.AddFilepattern(Constants.DOT_GIT_MODULES);
            // Add submodule directory to parent repository's index
            add.AddFilepattern(path);
            try
            {
                add.Call();
            }
            catch (NoFilepatternException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            return(subRepo);
        }