/// <summary>
 /// svn update localPath --depth empty --revision <sourceRevision> --xml --username lin --password ideafix --non-interactive [--trust-server-cert]
 /// </summary>
 /// <param name="mapping"></param>
 /// <returns></returns>
 private async Task UpdateAsync(SvnMappingDetails mapping)
 {
     _context.Debug($@"Update '{mapping.LocalPath}'.");
     await RunCommandAsync(
         "update",
         mapping.LocalPath,
         "--revision", mapping.Revision,
         "--depth", ToDepthArgument(mapping.Depth),
         mapping.IgnoreExternals? "--ignore-externals" : null);
 }
 /// <summary>
 /// svn checkout localPath --depth empty --revision <sourceRevision> --xml --username lin --password ideafix --non-interactive [--trust-server-cert]
 /// </summary>
 /// <param name="mapping"></param>
 /// <returns></returns>
 private async Task CheckoutAsync(SvnMappingDetails mapping)
 {
     _context.Debug($@"Checkout '{mapping.ServerPath}' to '{mapping.LocalPath}'.");
     await RunCommandAsync(
         "checkout",
         mapping.ServerPath,
         mapping.LocalPath,
         "--revision", mapping.Revision,
         "--depth", ToDepthArgument(mapping.Depth),
         mapping.IgnoreExternals? "--ignore-externals" : null);
 }
 /// <summary>
 /// svn switch localPath --depth empty --revision <sourceRevision> --xml --username lin --password ideafix --non-interactive [--trust-server-cert]
 /// </summary>
 /// <param name="mapping"></param>
 /// <returns></returns>
 private async Task SwitchAsync(SvnMappingDetails mapping)
 {
     _context.Debug($@"Switch '{mapping.LocalPath}' to '{mapping.ServerPath}'.");
     await RunCommandAsync(
         "switch",
         $"^/{mapping.ServerPath}",
         mapping.LocalPath,
         "--ignore-ancestry",
         "--revision", mapping.Revision,
         "--depth", ToDepthArgument(mapping.Depth),
         mapping.IgnoreExternals? "--ignore-externals" : null);
 }
        private async Task UpdateToRevisionAsync(Dictionary <string, Uri> oldMappings, Dictionary <string, SvnMappingDetails> newMappings, long maxRevision)
        {
            foreach (KeyValuePair <string, SvnMappingDetails> mapping in newMappings)
            {
                string            localPath      = mapping.Key;
                SvnMappingDetails mappingDetails = mapping.Value;

                string effectiveServerUri = BuildSvnUri(mappingDetails.ServerPath);
                string effectiveRevision  = EffectiveRevision(mappingDetails.Revision, maxRevision);

                mappingDetails.Revision = effectiveRevision;

                if (!Directory.Exists(Path.Combine(localPath, ".svn")))
                {
                    _context.Debug(String.Format(
                                       "Checking out with depth: {0}, revision: {1}, ignore externals: {2}",
                                       mappingDetails.Depth,
                                       effectiveRevision,
                                       mappingDetails.IgnoreExternals));

                    mappingDetails.ServerPath = effectiveServerUri;
                    await CheckoutAsync(mappingDetails);
                }
                else if (oldMappings.ContainsKey(localPath) && oldMappings[localPath].Equals(new Uri(effectiveServerUri)))
                {
                    _context.Debug(String.Format(
                                       "Updating with depth: {0}, revision: {1}, ignore externals: {2}",
                                       mappingDetails.Depth,
                                       mappingDetails.Revision,
                                       mappingDetails.IgnoreExternals));

                    await UpdateAsync(mappingDetails);
                }
                else
                {
                    _context.Debug(String.Format(
                                       "Switching to {0}  with depth: {1}, revision: {2}, ignore externals: {3}",
                                       mappingDetails.ServerPath,
                                       mappingDetails.Depth,
                                       mappingDetails.Revision,
                                       mappingDetails.IgnoreExternals));

                    await SwitchAsync(mappingDetails);
                }
            }
        }
        private Dictionary <string, SvnMappingDetails> BuildNewMappings(string rootPath, string sourceBranch, Dictionary <string, SvnMappingDetails> distinctMappings)
        {
            Dictionary <string, SvnMappingDetails> mappings = new Dictionary <string, SvnMappingDetails>();

            if (distinctMappings != null && distinctMappings.Count > 0)
            {
                foreach (KeyValuePair <string, SvnMappingDetails> mapping in distinctMappings)
                {
                    SvnMappingDetails mappingDetails = mapping.Value;

                    string localPath         = mappingDetails.LocalPath;
                    string absoluteLocalPath = Path.Combine(rootPath, localPath);

                    SvnMappingDetails newMappingDetails = new SvnMappingDetails();

                    newMappingDetails.ServerPath      = mappingDetails.ServerPath;
                    newMappingDetails.LocalPath       = absoluteLocalPath;
                    newMappingDetails.Revision        = mappingDetails.Revision;
                    newMappingDetails.Depth           = mappingDetails.Depth;
                    newMappingDetails.IgnoreExternals = mappingDetails.IgnoreExternals;

                    mappings.Add(absoluteLocalPath, newMappingDetails);
                }
            }
            else
            {
                SvnMappingDetails newMappingDetails = new SvnMappingDetails();

                newMappingDetails.ServerPath      = sourceBranch;
                newMappingDetails.LocalPath       = rootPath;
                newMappingDetails.Revision        = "HEAD";
                newMappingDetails.Depth           = 3; //Infinity
                newMappingDetails.IgnoreExternals = true;

                mappings.Add(rootPath, newMappingDetails);
            }

            return(mappings);
        }
 /// <summary>
 /// svn checkout localPath --depth empty --revision <sourceRevision> --xml --username lin --password ideafix --non-interactive [--trust-server-cert]
 /// </summary>
 /// <param name="mapping"></param>
 /// <returns></returns>
 private async Task CheckoutAsync(SvnMappingDetails mapping)
 {
     Trace.Verbose($@"Checkout '{mapping.ServerPath}' to '{mapping.LocalPath}'.");
     await RunCommandAsync(
         "checkout",
         mapping.ServerPath,
         mapping.LocalPath,
         "--revision", mapping.Revision,
         "--depth", ToDepthArgument(mapping.Depth),
         mapping.IgnoreExternals ? "--ignore-externals" : null);
 }
 /// <summary>
 /// svn switch localPath --depth empty --revision <sourceRevision> --xml --username lin --password ideafix --non-interactive [--trust-server-cert]
 /// </summary>
 /// <param name="mapping"></param>
 /// <returns></returns>
 private async Task SwitchAsync(SvnMappingDetails mapping)
 {
     Trace.Verbose($@"Switch '{mapping.LocalPath}' to '{mapping.ServerPath}'.");
     await RunCommandAsync(
         "switch",
         $"^/{mapping.ServerPath}",
         mapping.LocalPath,
         "--ignore-ancestry",
         "--revision", mapping.Revision,
         "--depth", ToDepthArgument(mapping.Depth),
         mapping.IgnoreExternals ? "--ignore-externals" : null);
 }
 /// <summary>
 /// svn update localPath --depth empty --revision <sourceRevision> --xml --username lin --password ideafix --non-interactive [--trust-server-cert]
 /// </summary>
 /// <param name="mapping"></param>
 /// <returns></returns>
 private async Task UpdateAsync(SvnMappingDetails mapping)
 {
     Trace.Verbose($@"Update '{mapping.LocalPath}'.");
     await RunCommandAsync(
         "update", 
         mapping.LocalPath,
         "--revision", mapping.Revision,
         "--depth", ToDepthArgument(mapping.Depth),
         mapping.IgnoreExternals ? "--ignore-externals" : null);
 }
        private Dictionary<string, SvnMappingDetails> BuildNewMappings(string rootPath, string sourceBranch, Dictionary<string, SvnMappingDetails> distinctMappings)
        {
            Dictionary<string, SvnMappingDetails> mappings = new Dictionary<string, SvnMappingDetails>();

            if (distinctMappings != null && distinctMappings.Count > 0)
            {
                foreach (KeyValuePair<string, SvnMappingDetails> mapping in distinctMappings)
                {
                    SvnMappingDetails mappingDetails = mapping.Value;

                    string localPath = mappingDetails.LocalPath;
                    string absoluteLocalPath = Path.Combine(rootPath, localPath);

                    SvnMappingDetails newMappingDetails = new SvnMappingDetails();

                    newMappingDetails.ServerPath = mappingDetails.ServerPath;
                    newMappingDetails.LocalPath = absoluteLocalPath;
                    newMappingDetails.Revision = mappingDetails.Revision;
                    newMappingDetails.Depth = mappingDetails.Depth;
                    newMappingDetails.IgnoreExternals = mappingDetails.IgnoreExternals;

                    mappings.Add(absoluteLocalPath, newMappingDetails);
                }
            }
            else
            {
                SvnMappingDetails newMappingDetails = new SvnMappingDetails();

                newMappingDetails.ServerPath = sourceBranch;
                newMappingDetails.LocalPath = rootPath;
                newMappingDetails.Revision = "HEAD";
                newMappingDetails.Depth = 3;  //Infinity
                newMappingDetails.IgnoreExternals = true;

                mappings.Add(rootPath, newMappingDetails);
            }

            return mappings;
        }