Hi,
I'm not sure if this is intended by the design of the vcs api, so i'll make my use case clear:
Currently for importing commits from a git repo, we parse the branch list to get a list of all branches. We just use VersioncontrolLabel::ensureLabel to ensure that a label is in the db. We have no idea when a branch was added, who did it or why, so we don't create any VersioncontrolOperation for branch creation, ever. This could change with a update-hook, but i'd still like to support the log parser anyways. This means we currently display a branch just as an attribute of a commit in the commit log. That's fine, but with git you'll add and remove a lot of branches, and we don't want already deleted branches to show up in the commit log. this means, when a branch is deleted in a git repo, we currently just delete the label from the database and remove it from all commits which are in a given branch (potentially many!).
The way this is done is extremly hacky, and it should at least get in the API. Maybe we should also think of a way of optimizing the deletion of branches, maybe through a boolean column deleted in the labels table, and modifying every query for versioncontrol_labels to not include labels with a status of deleted = 1. The cleanup of the table (remove deleted branches from the VCOperations and from the labels table) then could go into cron or could just get a seperate button, so it doesn't block the whole vcs_api.

The code i currently use for that is the following:

/**
 * This function removes the given branch from all commits.
 * @param VersioncontrolRepository $repository
 * @param string $branch
 * @return none
 */
function _versioncontrol_git_log_remove_branch_from_commits($repository, $branch) {
  $constraints = array(
    'vcs' => array('git'),
    'repo_ids' => array($repository->repo_id),
    'types' => array(VERSIONCONTROL_OPERATION_COMMIT),
    'branches' => array($branch),
  );
  $commits_as_op = VersioncontrolOperationCache::getInstance()->getOperations($constraints);
  foreach($commits_as_op as $commit_op) {
    $new_labels = array();
    foreach($commit_op->labels as $label) {
      if($label->type == VERSIONCONTROL_LABEL_BRANCH && $label->name != $branch) {
        $new_labels[] = $label;
      }
    }
    $commit_op->updateLabels($new_labels);
  }
}



  // Deleted branches are removed, commits in them are not!
  // TODO: the db_query in here is not nice, that should be part of vcapi!
  foreach($branches_deleted as $branch_name) {
    _versioncontrol_git_log_remove_branch_from_commits($repository, $branch_name);
    db_query('DELETE FROM {versioncontrol_labels}
              WHERE repo_id = %d AND type = %d AND label_id = %d',
              $repository->repo_id, VERSIONCONTROL_LABEL_BRANCH,
              $branch_label_list[$branch_name]->label_id);
  }

Comments

marvil07’s picture

Title: provide a way to delete a label from the database » provide a way to delete a entities from the database

This still needs work, but some start is committed to the cvs repo with the entities commit, so now, to remove a branch you can you can VersioncontrolBranch->delete()

The general intention is about be able to provide a delete method on each entity.

Talking about this with sdboyer, he is planning to add an activity table that store all the things that happen, so in the entities table we can remove data to be consistent with the repository.

marvil07’s picture

Issue tags: +git phase 2

tagging

webchick’s picture

Priority: Normal » Critical

Marking as critical, to indicate this is a migration blocker.

chrisstrahl’s picture

Issue tags: +git sprint 2

tagging for sprint 2

chrisstrahl’s picture

Assigned: Unassigned » sdboyer
sdboyer’s picture

Status: Active » Needs work

Nearly got this one done locally, gonna try to muscle it out this weekend.

neclimdul’s picture

Title: provide a way to delete a entities from the database » provide a way to delete an entities from the database

title nit-pick

sdboyer’s picture

I'm gonna collapse this with #879858: Unify entity C(R)UD in favor of making it just a big unified C(R)UD issue.

sdboyer’s picture

Status: Needs work » Closed (duplicate)