Example #1
0
        private void UpdateRemoteBranchDropDown()
        {
            RemoteBranch.Items.Clear();

            if (!string.IsNullOrEmpty(_NO_TRANSLATE_Branch.Text))
            {
                RemoteBranch.Items.Add(_NO_TRANSLATE_Branch.Text);
            }

            if (_selectedRemote != null)
            {
                foreach (var head in GetRemoteBranches(_selectedRemote.Name))
                {
                    if (_NO_TRANSLATE_Branch.Text != head.LocalName)
                    {
                        RemoteBranch.Items.Add(head.LocalName);
                    }
                }

                var remoteBranchesSet = GetRemoteBranches(_selectedRemote.Name).ToHashSet(b => b.LocalName);
                var onlyLocalBranches = GetLocalBranches().Where(b => !remoteBranchesSet.Contains(b.LocalName));

                foreach (var head in onlyLocalBranches)
                {
                    if (_NO_TRANSLATE_Branch.Text != head.LocalName)
                    {
                        RemoteBranch.Items.Add(head.LocalName);
                    }
                }
            }

            RemoteBranch.ResizeDropDownWidth(AppSettings.BranchDropDownMinWidth, AppSettings.BranchDropDownMaxWidth);
        }
        /// <summary>Create <see cref="RemoveRemoteBranchDialog"/>.</summary>
        /// <param name="branch"><see cref="RemoteBranch"/> to remove.</param>
        public RemoveRemoteBranchDialog(RemoteBranch branch)
        {
            Verify.Argument.IsNotNull(branch, "branch");
            Verify.Argument.IsFalse(branch.IsDeleted, "branch",
                                    Resources.ExcObjectIsDeleted.UseAsFormat("RemoteBranch"));

            InitializeComponent();

            _branch = branch;
            _remote = branch.Remote;

            Text = Resources.StrRemoveBranch;

            _lblRemoveBranch.Text = Resources.StrsRemoveBranchFrom.UseAsFormat(branch.Name).AddColon();

            _cmdRemoveLocalOnly.Text        = Resources.StrsRemoveLocalOnly;
            _cmdRemoveLocalOnly.Description = Resources.StrsRemoveLocalOnlyDescription;

            _cmdRemoveFromRemote.Text        = Resources.StrsRemoveFromRemote;
            _cmdRemoveFromRemote.Description = Resources.StrsRemoveFromRemoteDescription.UseAsFormat(_remote.Name);
        }
Example #3
0
        /// <summary>Create <see cref="RemoveRemoteBranchDialog"/>.</summary>
        /// <param name="branch"><see cref="RemoteBranch"/> to remove.</param>
        public RemoveRemoteBranchDialog(RemoteBranch branch)
        {
            Verify.Argument.IsNotNull(branch, "branch");
            Verify.Argument.IsFalse(branch.IsDeleted, "branch",
                Resources.ExcObjectIsDeleted.UseAsFormat("RemoteBranch"));

            InitializeComponent();

            _branch = branch;
            _remote = branch.Remote;

            Text = Resources.StrRemoveBranch;

            _lblRemoveBranch.Text = Resources.StrsRemoveBranchFrom.UseAsFormat(branch.Name).AddColon();

            _cmdRemoveLocalOnly.Text = Resources.StrsRemoveLocalOnly;
            _cmdRemoveLocalOnly.Description = Resources.StrsRemoveLocalOnlyDescription;

            _cmdRemoveFromRemote.Text = Resources.StrsRemoveFromRemote;
            _cmdRemoveFromRemote.Description = Resources.StrsRemoveFromRemoteDescription.UseAsFormat(_remote.Name);
        }
Example #4
0
        public override int GetHashCode()
        {
            if (hashcode.HasValue)
            {
                return(hashcode.Value);
            }

            unchecked
            {
                hashcode = (int)2166136261;
                hashcode = hashcode * 1677619 + (LocalBranch?.GetHashCode() ?? 0);
                hashcode = hashcode * 1677619 + (RemoteBranch?.GetHashCode() ?? 0);
                hashcode = hashcode * 1677619 + Ahead.GetHashCode();
                hashcode = hashcode * 1677619 + Behind.GetHashCode();
                foreach (var entry in Entries)
                {
                    hashcode = hashcode * 1677619 + entry.GetHashCode();
                }
                return(hashcode.Value);
            }
        }
Example #5
0
 private RemoteListItem GetRemoteListItem(RemoteBranch branch)
 {
     lock (Repository.Remotes.SyncRoot)
     {
         foreach (var remote in Repository.Remotes)
         {
             if (branch.Name.StartsWith(remote.Name + "/"))
             {
                 RemoteListItem ritem = null;
                 foreach (var i in _remotes)
                 {
                     if (i.DataContext.Name == remote.Name)
                     {
                         ritem = i;
                         break;
                     }
                 }
                 if (ritem == null)
                 {
                     ritem = new RemoteListItem(remote);
                     ritem.Items.Comparison = RemoteBranchListItem.CompareByName;
                     _remotes.Add(ritem);
                     CustomListBoxItemsCollection host;
                     if (Remotes == null)
                     {
                         host = _itemHost;
                     }
                     else
                     {
                         host = Remotes.Items;
                     }
                     host.AddSafe(ritem);
                 }
                 return(ritem);
             }
         }
     }
     return(null);
 }
Example #6
0
 /// <summary>Creates a <see cref="GitPush"/> to push from a local branch to a remote branch.</summary>
 public GitPush CreatePush(RemoteBranch remoteBranch, string localBranch)
 {
     return new GitPush(Name, localBranch, remoteBranch.FullRefName);
 }
Example #7
0
        /// <summary>
        /// Synchronize
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        private async IAsyncEnumerable <GitSyncJob> SyncAsync(GitSyncJob job)
        {
            string jobName = job.Name ?? "ROOT";
            // Sync current folder
            Repository repo   = new Repository(job.LocalFolder);
            Remote     origin = repo.Remotes.FirstOrDefault(r => r.Name == "origin");

            if (origin == null)
            {
                throw new InvalidOperationException($"[{jobName}] Cannot find remote origin for {job.Name}");
            }

            if (job.TargetUrl != null)
            {
                origin.FetchUrl = origin.PushUrl = job.TargetUrl;
            }

            Console.WriteLine($"[{jobName}] Creating source link => {job.SourceUrl} ");
            Uri    sourceUri = new Uri(job.SourceUrl);
            string schema    = sourceUri.Scheme.ToUpperInvariant();

            if (!string.IsNullOrEmpty(this.SourceToken) && (schema == "HTTP" || schema == "HTTPS"))
            {
                sourceUri = new Uri($"{sourceUri.Scheme}://{this.SourceToken}@{sourceUri.Host}:{sourceUri.Port}{sourceUri.PathAndQuery}");
            }
            Remote source = repo.Remotes.Add("source", sourceUri.ToString(), true);

            Console.WriteLine($"[{jobName}] Fetching from source repository...");
            RemoteBranch sourceBranch = source.Branches[job.Branch];

            if (sourceBranch == null)
            {
                Console.WriteLine($"[{jobName}] Failed to find branch source: {job.Branch}...");
                yield break;
            }
            await sourceBranch.FetchAsync();

            // for sub-modules, need to check correct branch first.
            if (job.Name != null)
            {
                string       targetBranchName = this.GetTargetBranch(job.Branch);
                RemoteBranch targetBranch     = origin.Branches[targetBranchName];
                if (targetBranch != null)   // Could be empty when syncing for first time.
                {
                    await targetBranch.FetchAsync();

                    Console.WriteLine($"[{jobName}] Checking out branch: {targetBranch} ...");
                    await repo.CheckoutAsync(targetBranch, true);

                    Console.WriteLine($"[{jobName}] Merging from branch: {sourceBranch} ...");
                    if (sourceBranch == null)
                    {
                        throw new InvalidOperationException($"[{jobName}] Failed to find remote branch on source: {sourceBranch}");
                    }
                    await repo.MergeAsync(sourceBranch, true);

                    await repo.PushAsync(origin, targetBranchName);
                }
            }
            else
            {
                string targetBranchName = this.GetTargetBranch(job.Branch);
                Console.WriteLine($"[{jobName}] Merging branch: {targetBranchName} ...");
                if (sourceBranch == null)
                {
                    throw new InvalidOperationException($"[{jobName}] Failed to find remote branch on target: {job.Branch}");
                }
                RemoteBranch targetBranch = origin.Branches[targetBranchName];
                if (targetBranch != null)
                {
                    await targetBranch.PullAsync();
                }
                await repo.MergeAsync(sourceBranch, true);

                await repo.PushAsync(origin, targetBranchName);
            }

            // Push for unmapped branches
            Console.WriteLine($"[{jobName}] Create un-mapped branches ...");
            RemoteBranch[] sourceBranches   = source.Branches.Where <RemoteBranch>(b => BRANCH_PATTERN.IsMatch(b.Name)).ToArray();
            RemoteBranch[] targetBranches   = origin.Branches.Where <RemoteBranch>(b => sourceBranches.Select(b => this.GetTargetBranch(b.Name)).Contains(b.Name)).ToArray();
            RemoteBranch[] unmappedBranches = sourceBranches.Where(b => !targetBranches.Any(t => t.Name == this.GetTargetBranch(b.Name))).ToArray();

            if (unmappedBranches.Length > 0)
            {
                Console.WriteLine($"[{jobName}] Fetching branches from source: {string.Join(';', unmappedBranches.Select(b => b.Name))} ...");
                await source.FetchAsync(unmappedBranches);

                Console.WriteLine($"[{jobName}] Pushing branches to origin: {string.Join(';', unmappedBranches.Select(b => b.Name))} ...");
                await origin.PushAsync(unmappedBranches, (b) => this.GetTargetBranch(b.Name));
            }

            // Check for submodules
            if (repo.Submodules.Configured)
            {
                foreach (Submodule module in repo.Submodules)
                {
                    string sourceUrl = module["source-url"];
                    if (string.Equals(module["ignore-mirror"], "TRUE", StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }
                    if (string.IsNullOrWhiteSpace(sourceUrl))
                    {
                        Console.WriteLine($"[{jobName}] Skipping for {module.Name}: source-url not yet configured");
                        continue;
                    }

                    await module.InitAsync();

                    await module.UpdateAsync();

                    yield return(new GitSyncJob(module.Name, Path.Combine(job.LocalFolder, module.Path),
                                                module["source-url"], module.Url, module.Branch));
                }
            }
        }
Example #8
0
 private RemoteListItem GetRemoteListItem(RemoteBranch branch)
 {
     lock(_repository.Remotes.SyncRoot)
     {
         foreach(var remote in _repository.Remotes)
         {
             if(branch.Name.StartsWith(remote.Name + "/"))
             {
                 RemoteListItem ritem = null;
                 foreach(var i in _remotes)
                 {
                     if(i.DataContext.Name == remote.Name)
                     {
                         ritem = i;
                         break;
                     }
                 }
                 if(ritem == null)
                 {
                     ritem = new RemoteListItem(remote);
                     ritem.Items.Comparison = RemoteBranchListItem.CompareByName;
                     _remotes.Add(ritem);
                     CustomListBoxItemsCollection host;
                     if(_refsRemotes == null)
                     {
                         host = _itemHost;
                     }
                     else
                     {
                         host = _refsRemotes.Items;
                     }
                     host.AddSafe(ritem);
                 }
                 return ritem;
             }
         }
     }
     return null;
 }
Example #9
0
 /// <summary>Creates a <see cref="GitPush"/> to push from a local branch to a remote branch.</summary>
 public GitPush CreatePush(RemoteBranch remoteBranch, string localBranch)
 {
     return(new GitPush(Name, localBranch, remoteBranch.FullRefName));
 }