Exemplo n.º 1
0
  public Repository call() throws JGitInternalException {
    checkCallable();
    if (path == null || path.length() == 0)
      throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
    if (uri == null || uri.length() == 0)
      throw new IllegalArgumentException(JGitText.get().uriNotConfigured);

    try {
      if (submoduleExists())
        throw new JGitInternalException(MessageFormat.format(JGitText.get().submoduleExists, path));
    } catch (IOException e) {
      throw new JGitInternalException(e.getMessage(), e);
    }

    // Clone submodule repository
    File moduleDirectory = SubmoduleWalk.getSubmoduleDirectory(repo, path);
    CloneCommand clone = Git.cloneRepository();
    clone.setDirectory(moduleDirectory);
    clone.setURI(uri);
    if (monitor != null) clone.setProgressMonitor(monitor);
    if (credentialsProvider != null) clone.setCredentialsProvider(credentialsProvider);
    Repository subRepo = clone.call().getRepository();

    // Save submodule URL to parent repository's config
    StoredConfig config = repo.getConfig();
    config.setString(
        ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_URL, uri);
    try {
      config.save();
    } catch (IOException e) {
      throw new JGitInternalException(e.getMessage(), e);
    }

    // Save path and URL to parent repository's .gitmodules file
    FileBasedConfig modulesConfig =
        new FileBasedConfig(new File(repo.getWorkTree(), Constants.DOT_GIT_MODULES), repo.getFS());
    modulesConfig.setString(
        ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_PATH, path);
    modulesConfig.setString(
        ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_URL, uri);
    try {
      modulesConfig.save();
    } catch (IOException e) {
      throw new JGitInternalException(e.getMessage(), e);
    }

    AddCommand add = new AddCommand(repo);
    // Add .gitmodules file to parent repository's index
    add.addFilepattern(Constants.DOT_GIT_MODULES);
    // Add submodule directory to parent repository's index
    add.addFilepattern(path);
    try {
      add.call();
    } catch (NoFilepatternException e) {
      throw new JGitInternalException(e.getMessage(), e);
    }

    return subRepo;
  }
Exemplo n.º 2
0
 /**
  * Adds the given file to the index
  *
  * @param file
  * @throws CoreException
  * @throws IOException
  */
 public void addToIndex(IFile file) throws CoreException, IOException {
   String repoPath = getRepoRelativePath(file.getLocation().toOSString());
   try {
     new Git(repository).add().addFilepattern(repoPath).call();
   } catch (NoFilepatternException e) {
     throw new IOException(e.getMessage());
   }
 }
Exemplo n.º 3
0
 /**
  * Adds file to version control
  *
  * @param file
  * @throws IOException
  */
 public void track(File file) throws IOException {
   String repoPath = getRepoRelativePath(new Path(file.getPath()).toString());
   try {
     new Git(repository).add().addFilepattern(repoPath).call();
   } catch (NoFilepatternException e) {
     throw new IOException(e.getMessage());
   }
 }
Exemplo n.º 4
0
  /**
   * Adds the all.
   *
   * @param g the g
   */
  public void addAll(Git g) {

    try {
      g.add().addFilepattern(".").call();

      System.out.println("Added all to repository at " + g.getRepository().getDirectory());

    } catch (NoFilepatternException e) {
      e.printStackTrace();
    } catch (GitAPIException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 5
0
  // This will probs end up being static
  public static void addFile(Git g, String fileName) {
    File myFile = new File(g.getRepository().getDirectory().getParent(), fileName);

    try {
      myFile.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }

    try {
      g.add().addFilepattern(fileName).call();

      System.out.println(
          "Added file " + myFile + " to repository at " + g.getRepository().getDirectory());

    } catch (NoFilepatternException e) {
      e.printStackTrace();
    } catch (GitAPIException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 6
0
  /**
   * Executes the {@code commit} command with all the options and parameters collected by the setter
   * methods of this class. Each instance of this class should only be used for one invocation of
   * the command (means: one call to {@link #call()})
   *
   * @return a {@link RevCommit} object representing the successful commit.
   * @throws NoHeadException when called on a git repo without a HEAD reference
   * @throws NoMessageException when called without specifying a commit message
   * @throws UnmergedPathsException when the current index contained unmerged paths (conflicts)
   * @throws ConcurrentRefUpdateException when HEAD or branch ref is updated concurrently by someone
   *     else
   * @throws WrongRepositoryStateException when repository is not in the right state for committing
   * @throws AbortedByHookException if there are either pre-commit or commit-msg hooks present in
   *     the repository and one of them rejects the commit.
   */
  public RevCommit call()
      throws GitAPIException, NoHeadException, NoMessageException, UnmergedPathsException,
          ConcurrentRefUpdateException, WrongRepositoryStateException, AbortedByHookException {
    checkCallable();
    Collections.sort(only);

    try (RevWalk rw = new RevWalk(repo)) {
      RepositoryState state = repo.getRepositoryState();
      if (!state.canCommit())
        throw new WrongRepositoryStateException(
            MessageFormat.format(JGitText.get().cannotCommitOnARepoWithState, state.name()));

      if (!noVerify) {
        Hooks.preCommit(repo, hookOutRedirect).call();
      }

      processOptions(state, rw);

      if (all && !repo.isBare() && repo.getWorkTree() != null) {
        try (Git git = new Git(repo)) {
          git.add()
              .addFilepattern(".") // $NON-NLS-1$
              .setUpdate(true)
              .call();
        } catch (NoFilepatternException e) {
          // should really not happen
          throw new JGitInternalException(e.getMessage(), e);
        }
      }

      Ref head = repo.getRef(Constants.HEAD);
      if (head == null)
        throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);

      // determine the current HEAD and the commit it is referring to
      ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}"); // $NON-NLS-1$
      if (headId == null && amend)
        throw new WrongRepositoryStateException(JGitText.get().commitAmendOnInitialNotPossible);

      if (headId != null)
        if (amend) {
          RevCommit previousCommit = rw.parseCommit(headId);
          for (RevCommit p : previousCommit.getParents()) parents.add(p.getId());
          if (author == null) author = previousCommit.getAuthorIdent();
        } else {
          parents.add(0, headId);
        }

      if (!noVerify) {
        message = Hooks.commitMsg(repo, hookOutRedirect).setCommitMessage(message).call();
      }

      // lock the index
      DirCache index = repo.lockDirCache();
      try (ObjectInserter odi = repo.newObjectInserter()) {
        if (!only.isEmpty()) index = createTemporaryIndex(headId, index, rw);

        // Write the index as tree to the object database. This may
        // fail for example when the index contains unmerged paths
        // (unresolved conflicts)
        ObjectId indexTreeId = index.writeTree(odi);

        if (insertChangeId) insertChangeId(indexTreeId);

        // Create a Commit object, populate it and write it
        CommitBuilder commit = new CommitBuilder();
        commit.setCommitter(committer);
        commit.setAuthor(author);
        commit.setMessage(message);

        commit.setParentIds(parents);
        commit.setTreeId(indexTreeId);
        ObjectId commitId = odi.insert(commit);
        odi.flush();

        RevCommit revCommit = rw.parseCommit(commitId);
        RefUpdate ru = repo.updateRef(Constants.HEAD);
        ru.setNewObjectId(commitId);
        if (reflogComment != null) {
          ru.setRefLogMessage(reflogComment, false);
        } else {
          String prefix =
              amend
                  ? "commit (amend): " //$NON-NLS-1$
                  : parents.size() == 0
                      ? "commit (initial): " //$NON-NLS-1$
                      : "commit: "; //$NON-NLS-1$
          ru.setRefLogMessage(prefix + revCommit.getShortMessage(), false);
        }
        if (headId != null) ru.setExpectedOldObjectId(headId);
        else ru.setExpectedOldObjectId(ObjectId.zeroId());
        Result rc = ru.forceUpdate();
        switch (rc) {
          case NEW:
          case FORCED:
          case FAST_FORWARD:
            {
              setCallable(false);
              if (state == RepositoryState.MERGING_RESOLVED || isMergeDuringRebase(state)) {
                // Commit was successful. Now delete the files
                // used for merge commits
                repo.writeMergeCommitMsg(null);
                repo.writeMergeHeads(null);
              } else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) {
                repo.writeMergeCommitMsg(null);
                repo.writeCherryPickHead(null);
              } else if (state == RepositoryState.REVERTING_RESOLVED) {
                repo.writeMergeCommitMsg(null);
                repo.writeRevertHead(null);
              }
              return revCommit;
            }
          case REJECTED:
          case LOCK_FAILURE:
            throw new ConcurrentRefUpdateException(
                JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
          default:
            throw new JGitInternalException(
                MessageFormat.format(
                    JGitText.get().updatingRefFailed, Constants.HEAD, commitId.toString(), rc));
        }
      } finally {
        index.unlock();
      }
    } catch (UnmergedPathException e) {
      throw new UnmergedPathsException(e);
    } catch (IOException e) {
      throw new JGitInternalException(
          JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
    }
  }