示例#1
0
        public static void PushCurrentProfile(bool firstAttempt)
        {
            Repository repo = new Repository(Preferences.Profiles[SelectedProfileIndex].RepositoryPath);

            // Try to pull repository
            try
            {
                Preferences.Profiles[SelectedProfileIndex].Push(new UsernamePasswordCredentials()
                {
                    Username = Preferences.Profiles[SelectedProfileIndex].Username, Password = Encoding.Unicode.GetString(ProtectedData.Unprotect(Preferences.Profiles[SelectedProfileIndex].Password, null, DataProtectionScope.CurrentUser))
                });
            }
            catch (LibGit2SharpException ex)
            {
                if (ex.Message == "too many redirects or authentication replays")
                {
                    if (firstAttempt)
                    {
                        Application.Current.Dispatcher.Invoke(() => { MessageBox.Show("Please type in your git credentials.", "Git credentials", MessageBoxButton.OK, MessageBoxImage.Warning); });
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke(() => MessageBox.Show("Incorrect username or password. Please try again.", "Username or password incorrect", MessageBoxButton.OK, MessageBoxImage.Information));
                    }
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        GitCredentialsWindow gitCredentialsWindow = new GitCredentialsWindow();
                        gitCredentialsWindow.ShowDialog();
                    });
                }
                else if (ex.Message == "cannot push non-fastforwardable reference")
                {
                    Application.Current.Dispatcher.Invoke(() => MessageBox.Show("The changes pulled from the remote have conflicted with the local changes.\r\nResolve these confilcts. Then, proceed by clicking 'OK'.", "Merge conflicts", MessageBoxButton.OK, MessageBoxImage.Information));
                    if (repo.RetrieveStatus().IsDirty)
                    {
                        // Commit changes
                        string commitMessage = "";
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            CommitMessageWindow commitMessageWindow   = new CommitMessageWindow();
                            commitMessageWindow.btnNoCommit.IsEnabled = false;
                            commitMessageWindow.txtCommitMessage.Text = "Merge branch '" + repo.Head.FriendlyName + "' of " + repo.Network.Remotes.First().Url;;
                            commitMessageWindow.ShowDialog();
                            commitMessage = commitMessageWindow.Message;
                        });
                        Commands.Stage(repo, "*");
                        DateTime signatureDate = DateTime.Now;
                        Commit   commit        = repo.Commit(commitMessage, GetSignature(signatureDate), GetSignature(signatureDate));
                    }
                    // Backup zip file and delete unnecessary backups
                    Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(Preferences.Profiles[SelectedProfileIndex].ZipFilePath), Path.GetFileNameWithoutExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath) + ".bks"));
                    File.Copy(Preferences.Profiles[SelectedProfileIndex].ZipFilePath, Path.Combine(Path.GetDirectoryName(Preferences.Profiles[SelectedProfileIndex].ZipFilePath), Path.GetFileNameWithoutExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath) + ".bks", DateTime.UtcNow.ToString("yyyy-MM-dd HH-mm-ss") + Path.GetExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath)));
                    while (Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Preferences.Profiles[SelectedProfileIndex].ZipFilePath), Path.GetFileNameWithoutExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath)) + ".bks").Length > 6)
                    {
                        string[] files = Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Preferences.Profiles[SelectedProfileIndex].ZipFilePath), Path.GetFileNameWithoutExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath)) + ".bks");
                        Array.Sort(files);
                        File.Delete(files[0]);
                    }
                    // Replace zip file
                    DeleteZipFile();
                    using (ZipArchive zipArchive = ZipFile.Open(Preferences.Profiles[SelectedProfileIndex].ZipFilePath, ZipArchiveMode.Create))
                    {
                        foreach (string f in DirSearch(Preferences.Profiles[SelectedProfileIndex].RepositoryPath))
                        {
                            zipArchive.CreateEntryFromFile(f, f.Replace(Preferences.Profiles[SelectedProfileIndex].RepositoryPath + @"\", ""));
                        }
                    }
                    // Update latest commit time and zip file hash values in Profile
                    if (repo.Head.Tip != null)
                    {
                        Preferences.Profiles[SelectedProfileIndex].LatestCommitTime = repo.Head.Tip.Committer.When;
                    }
                    else
                    {
                        Preferences.Profiles[SelectedProfileIndex].LatestCommitTime = DateTime.MinValue;
                    }
                    Preferences.Profiles[SelectedProfileIndex].ZipFileHash = HashZipFile();
                    Preferences.Save();
                }
                else if (ex.Message == "There is no tracking information for the current branch.")
                {
                    throw;
                }
                else
                {
                    Application.Current.Dispatcher.Invoke(() => MessageBox.Show(ex.Message, "Push error", MessageBoxButton.OK, MessageBoxImage.Information));
                }
                PushCurrentProfile(false);
            }
        }
示例#2
0
        public static void PullCurrentProfile(bool firstAttempt)
        {
            Repository repo = new Repository(Preferences.Profiles[SelectedProfileIndex].RepositoryPath);

            if (firstAttempt)
            {
                // Backup zip file and delete unnecessary backups
                Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(Preferences.Profiles[SelectedProfileIndex].ZipFilePath), Path.GetFileNameWithoutExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath) + ".bks"));
                CopyZipFile(Path.Combine(Path.GetDirectoryName(Preferences.Profiles[SelectedProfileIndex].ZipFilePath), Path.GetFileNameWithoutExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath) + ".bks", DateTime.UtcNow.ToString("yyyy-MM-dd HH-mm-ss") + Path.GetExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath)));
                while (Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Preferences.Profiles[SelectedProfileIndex].ZipFilePath), Path.GetFileNameWithoutExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath)) + ".bks").Length > 6)
                {
                    string[] files = Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Preferences.Profiles[SelectedProfileIndex].ZipFilePath), Path.GetFileNameWithoutExtension(Preferences.Profiles[SelectedProfileIndex].ZipFilePath)) + ".bks");
                    Array.Sort(files);
                    File.Delete(files[0]);
                }
            }
            // Try to pull repository
            try
            {
                Preferences.Profiles[SelectedProfileIndex].Pull(new UsernamePasswordCredentials()
                {
                    Username = Preferences.Profiles[SelectedProfileIndex].Username, Password = Encoding.Unicode.GetString(ProtectedData.Unprotect(Preferences.Profiles[SelectedProfileIndex].Password, null, DataProtectionScope.CurrentUser))
                }, GetSignature(DateTime.Now));
                // Update latest commit time and zip file hash values in Profile
                if (repo.Head.Tip != null)
                {
                    Preferences.Profiles[SelectedProfileIndex].LatestCommitTime = repo.Head.Tip.Committer.When;
                }
                else
                {
                    Preferences.Profiles[SelectedProfileIndex].LatestCommitTime = DateTime.MinValue;
                }
                Preferences.Profiles[SelectedProfileIndex].ZipFileHash = HashZipFile();
                Preferences.Save();
            }
            catch (LibGit2SharpException ex)
            {
                if (ex.Message == "too many redirects or authentication replays" || ex.Message == "failed to set credentials: The parameter is incorrect.\r\n")
                {
                    if (firstAttempt)
                    {
                        Application.Current.Dispatcher.Invoke(() => { MessageBox.Show("Please type in your git credentials.", "Git credentials", MessageBoxButton.OK, MessageBoxImage.Information); });
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke(() => MessageBox.Show("Incorrect username or password. Please try again.", "Username or password incorrect", MessageBoxButton.OK, MessageBoxImage.Information));
                    }
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        GitCredentialsWindow gitCredentialsWindow = new GitCredentialsWindow();
                        gitCredentialsWindow.ShowDialog();
                    });
                }
                else if (ex.Message == "There is no tracking information for the current branch.")
                {
                    throw;
                }
                else
                {
                    Application.Current.Dispatcher.Invoke(() => MessageBox.Show(ex.Message, "Pull error", MessageBoxButton.OK, MessageBoxImage.Error));
                }
                PullCurrentProfile(false);
            }
            catch (IOException)
            {
                Application.Current.Dispatcher.Invoke(() => MessageBox.Show("Zip file could not be accessed. Please make sure no other processes are using the file and that you have read/write access to it.", "Zip file could not be read/written", MessageBoxButton.OK, MessageBoxImage.Error));
                PullCurrentProfile(false);
            }
        }