void RemoteRepositoryAssistant::on_assistant_apply ()
{
  // Configurations.
  extern Settings *settings;
  ProjectConfiguration *projectconfig = settings->projectconfig(bible);

  // Whether to use the remote repository.
  bool use_remote_repository = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton_use_repository));
  if (bible_notes_selector_bible ())
    projectconfig->git_use_remote_repository_set(use_remote_repository);
  else
    settings->genconfig.consultation_notes_git_use_remote_repository_set(use_remote_repository);

  // The remote repository URL.
  if (bible_notes_selector_bible ())
    projectconfig->git_remote_repository_url_set(repository_url_get());
  else
    settings->genconfig.consultation_notes_git_remote_repository_url_set(repository_url_get());
  
  // If the repository was cloned, move it into place.
  if (repository_was_cloned()) {
    ustring destination_data_directory;
    if (bible_notes_selector_bible ())
      destination_data_directory = project_data_directory_project(bible);
    else
      destination_data_directory = notes_shared_storage_folder ();
    unix_rmdir(destination_data_directory);
    unix_mv(persistent_clone_directory, destination_data_directory);
    // Switch rename detection off. 
    // This is necessary for the consultation notes, since git has been seen to cause spurious renames.
    GwSpawn spawn ("git");
    spawn.workingdirectory (destination_data_directory);
    spawn.arg ("config");
    spawn.arg ("--global");
    spawn.arg ("diff.renamelimit");
    spawn.arg ("0");
    spawn.run ();
  }

  if (bible_notes_selector_bible ()) {
    // Take a snapshot of the whole project.
    snapshots_shoot_project (bible);
  } else{
    // Create the index for the consultation notes.
    notes_create_index ();
  }

  // Show summary.
  gtk_assistant_set_current_page (GTK_ASSISTANT (assistant), summary_page_number);
}
Esempio n. 2
0
void git_upgrade ()
// Upgrades the git system.
{
  // Go through the projects that have their git repository enabled.
  extern Settings * settings;
  vector <ustring> projects = projects_get_all ();
  for (unsigned int i = 0; i < projects.size(); i++) {
    ProjectConfiguration * projectconfig = settings->projectconfig (projects[i]);
    ustring git_directory = gw_build_filename (project_data_directory_project (projects[i]), ".git");
    if (projectconfig->git_use_remote_repository_get()) {
      // At times there's a stale index.lock file that prevents any collaboration.
      // This is to be removed.
      ustring index_lock = gw_build_filename (git_directory, "index.lock");
      if (g_file_test (index_lock.c_str(), G_FILE_TEST_IS_REGULAR)) {
        gw_message (_("Cleaning out index lock ") + index_lock);
        unix_unlink (index_lock.c_str());
      }
      // Get the data directory for the project
      ustring datadirectory = tiny_project_data_directory_project(projects[i]);
      // On most machines git can determine the user's name from the system services. 
      // On the XO machine, it can't. It is set here manually. 
      // On more recent versions of git, like version 1.8.3 and younger,
      // although git may determine the user's name from the system, 
      // it still requires it to be set manually.
      ustring command;
      command = "git config user.email \"";
      command.append(g_get_user_name());
      command.append("@");
      command.append(g_get_host_name());
      command.append("\"");
      maintenance_register_shell_command (datadirectory, command);
      command = "git config user.name \"";
      command.append(g_get_real_name());
      command.append("\"");
      maintenance_register_shell_command (datadirectory, command);
      // (Re)initialize the repository. This can be done repeatedly without harm.
      // Note that this is done on shutdown.
      maintenance_register_shell_command (datadirectory, "git init");
    } else {
      if (g_file_test (git_directory.c_str(), G_FILE_TEST_IS_DIR)) {
        gw_message (_("Cleaning out folder ") + git_directory);
        ProgressWindow progresswindow (_("Tidying up project ") + projects[i], false);
        progresswindow.set_fraction (0.5);
        unix_rmdir (git_directory);
      }
    }
  }
}
Esempio n. 3
0
void git_resolve_conflicts(const ustring & project, const vector < ustring > &errors)
/*
 This function reads through the "errors" to see if any conflicting merge occurred.
 If that happened, it resolves the conflicts.
 */
{
  /*
     See if the errors indicate a conflict. If not, bail out.

     The first time that a "git pull" is done, and there is a conflict, it says this:

     Auto-merged 3 John/1/data
     CONFLICT (content): Merge conflict in 3 John/1/data
     Automatic merge failed; fix conflicts and then commit the result.  

     The second time that a "git pull" would be done, it would say this:

     You are in the middle of a conflicted merge.

   */
  bool conflict = false;
  for (unsigned int i = 0; i < errors.size(); i++) {
    if (errors[i].find("conflict") != string::npos) {
      conflict = true;
    }
  }
  if (!conflict)
    return;

  // Get the working directory.
  ustring directory = project_data_directory_project(project);

  // Run a "git status" to find the books and chapters that have a merge conflict.
  vector < Reference > conflicted_chapters;
  {
    GwSpawn spawn("git status");
    spawn.workingdirectory(directory);
    spawn.read();
    spawn.run();
    for (unsigned int i = 0; i < spawn.standardout.size(); i++) {
      ustring line = spawn.standardout[i];
      size_t pos = line.find("unmerged:");
      if (pos != string::npos) {
        line.erase(0, pos + 10);
        line = trim(line);
        Parse parse(line, false, G_DIR_SEPARATOR_S);
        if (parse.words.size() >= 2) {
          unsigned int book = books_english_to_id(parse.words[0]);
          if (book) {
            unsigned int chapter = convert_to_int(parse.words[1]);
            Reference reference(book, chapter, "");
            conflicted_chapters.push_back(reference);
          }
        }
      }
    }
  }

  // Resolve all conflicts according to the settings of the user.
  for (unsigned int i = 0; i < conflicted_chapters.size(); i++) {
    git_resolve_conflict_chapter(project, conflicted_chapters[i].book_get(), conflicted_chapters[i].chapter_get());
  }

  // Commit the changes.
  {
    GwSpawn spawn("git commit");
    spawn.workingdirectory(directory);
    spawn.arg("-m");
    spawn.arg("Resolved conflict");
    spawn.arg("-a");
    spawn.run();
  }
}
void RemoteRepositoryAssistant::on_button_push ()
/*
It copies the existing data, without the .git directory, into the persistent clone,
replaces any data that was there, and then pushes this data to the remote repository.
This makes the remote repository to have an exact copy of our data.
*/
{
  // Progress.
  ProgressWindow progresswindow (_("Pushing your data"), false);
  progresswindow.set_fraction (0.2);
  
  // Copy our data into a temporal location.
  ustring my_data_directory = notes_shared_storage_folder ();
  if (bible_notes_selector_bible ())
    my_data_directory = project_data_directory_project(bible);
  ustring temporal_data_directory = git_testing_directory ("mydata");
  unix_cp_r (my_data_directory, temporal_data_directory);

  // In rare cases a .git directory could have been copied along with our data. Remove that.
  unix_rmdir (gw_build_filename (temporal_data_directory, ".git"));

  // Remove all directories and all files from the persistent clone directory, but leave the .git directory
  {
    ReadDirectories rd (persistent_clone_directory, "", "");
    for (unsigned int i = 0; i < rd.directories.size(); i++) {
      if (rd.directories[i] != ".git") {
        unix_rmdir (gw_build_filename (persistent_clone_directory, rd.directories[i]));
      }
    }
    ReadFiles rf (persistent_clone_directory, "", "");
    for (unsigned int i = 0; i < rf.files.size(); i++) {
      unlink (gw_build_filename (persistent_clone_directory, rf.files[i]).c_str());
    }
  }
  
  // Move our data, from its temporal location, into the persistent clone directory.
  progresswindow.set_fraction (0.4);
  {
    ReadDirectories rd (temporal_data_directory, "", "");
    for (unsigned int i = 0; i < rd.directories.size(); i++) {
      unix_mv (gw_build_filename (temporal_data_directory, rd.directories[i]), persistent_clone_directory);
    }
    ReadFiles rf (temporal_data_directory, "", "");
    for (unsigned int i = 0; i < rf.files.size(); i++) {
      unix_mv (gw_build_filename (temporal_data_directory, rf.files[i]), persistent_clone_directory);
    }
  }

  // Commit the new data in the persistent clone directory.
  progresswindow.set_fraction (0.55);
  {
    GwSpawn spawn ("git");
    spawn.workingdirectory (persistent_clone_directory);
    spawn.arg ("add");
    spawn.arg (".");
    spawn.run ();
  }
  progresswindow.set_fraction (0.65);
  {
    GwSpawn spawn ("git");
    spawn.workingdirectory (persistent_clone_directory);
    spawn.arg ("commit");
    spawn.arg ("-a");
    spawn.arg ("-m");
    spawn.arg ("user data into repo");
    spawn.run ();
  }

  // Push our data to the remote repository.
  progresswindow.set_fraction (0.8);
  GwSpawn spawn("git");
  spawn.workingdirectory(persistent_clone_directory);
  spawn.arg ("push");
  spawn.run();

  // Take action depending on the outcome of pushing to the remote repository.
  if (spawn.exitstatus == 0) {
    // Clone okay.
    gtk_label_set_text (GTK_LABEL (label_push), _("Your data has been pushed to the remote repository"));
  } else {
    // Clone failed.
    gtk_label_set_text (GTK_LABEL (label_push), _("Your data could not be pushed to the remote repository,\nplease restart the assistant"));
    repository_unclone();
  }  
}