private String getObjectIdOfCommit() throws Exception {
   String branch = repository.getFullBranch();
   if (ObjectId.isId(branch)) return branch;
   if (branch.startsWith(Constants.R_REFS)) {
     RevCommit commit = revWalk.parseCommit(repository.resolve(branch));
     return commit.getId().getName();
   }
   if (branch.startsWith(Constants.R_TAGS)) {
     RevTag tag = revWalk.parseTag(repository.resolve(branch));
     return tag.getObject().getId().name();
   }
   throw new IllegalStateException("Can't resolve commit");
 }
  /**
   * Returns the name of the remote repository for the given branch. If there's no current branch or
   * no remote configured to it, the default remote is returned ("origin").
   *
   * @param branch the branch
   * @param repository the repository
   * @return the remote name
   */
  private static String getRemoteName(String branch, Repository repository) {
    String remoteName = null;
    if (ObjectId.isId(branch)) {
      remoteName = Constants.DEFAULT_REMOTE_NAME;
    } else {
      remoteName =
          repository
              .getConfig()
              .getString(
                  ConfigConstants.CONFIG_BRANCH_SECTION,
                  branch,
                  ConfigConstants.CONFIG_REMOTE_SECTION);
      if (remoteName == null) {
        remoteName = Constants.DEFAULT_REMOTE_NAME;
      }
    }

    return remoteName;
  }
    private Ref readRef(final TreeMap<String, Ref> avail, final String rn)
        throws TransportException {
      final String s;
      String ref = ROOT_DIR + rn;
      try {
        final BufferedReader br = openReader(ref);
        try {
          s = br.readLine();
        } finally {
          br.close();
        }
      } catch (FileNotFoundException noRef) {
        return null;
      } catch (IOException err) {
        throw new TransportException(getURI(), "read " + ref, err);
      }

      if (s == null) throw new TransportException(getURI(), "Empty ref: " + rn);

      if (s.startsWith("ref: ")) {
        final String target = s.substring("ref: ".length());
        Ref r = avail.get(target);
        if (r == null) r = readRef(avail, target);
        if (r == null) return null;
        r = new Ref(r.getStorage(), rn, r.getObjectId(), r.getPeeledObjectId(), r.isPeeled());
        avail.put(r.getName(), r);
        return r;
      }

      if (ObjectId.isId(s)) {
        final Ref r = new Ref(loose(avail.get(rn)), rn, ObjectId.fromString(s));
        avail.put(r.getName(), r);
        return r;
      }

      throw new TransportException(getURI(), "Bad ref: " + rn + ": " + s);
    }
Exemple #4
0
  /**
   * @throws RefNotFoundException if the old branch can not be found (branch with provided old name
   *     does not exist or old name resolves to a tag)
   * @throws InvalidRefNameException if the provided new name is <code>null</code> or otherwise
   *     invalid
   * @throws RefAlreadyExistsException if a branch with the new name already exists
   * @throws DetachedHeadException if rename is tried without specifying the old name and HEAD is
   *     detached
   */
  public Ref call()
      throws GitAPIException, RefNotFoundException, InvalidRefNameException,
          RefAlreadyExistsException, DetachedHeadException {
    checkCallable();

    if (newName == null)
      throw new InvalidRefNameException(
          MessageFormat.format(JGitText.get().branchNameInvalid, "<null>")); // $NON-NLS-1$

    try {
      String fullOldName;
      String fullNewName;
      if (repo.findRef(newName) != null)
        throw new RefAlreadyExistsException(
            MessageFormat.format(JGitText.get().refAlreadyExists1, newName));
      if (oldName != null) {
        Ref ref = repo.findRef(oldName);
        if (ref == null)
          throw new RefNotFoundException(
              MessageFormat.format(JGitText.get().refNotResolved, oldName));
        if (ref.getName().startsWith(Constants.R_TAGS))
          throw new RefNotFoundException(
              MessageFormat.format(JGitText.get().renameBranchFailedBecauseTag, oldName));
        fullOldName = ref.getName();
      } else {
        fullOldName = repo.getFullBranch();
        if (fullOldName == null) {
          throw new NoHeadException(JGitText.get().invalidRepositoryStateNoHead);
        }
        if (ObjectId.isId(fullOldName)) throw new DetachedHeadException();
      }

      if (fullOldName.startsWith(Constants.R_REMOTES)) fullNewName = Constants.R_REMOTES + newName;
      else {
        fullNewName = Constants.R_HEADS + newName;
      }

      if (!Repository.isValidRefName(fullNewName))
        throw new InvalidRefNameException(
            MessageFormat.format(JGitText.get().branchNameInvalid, fullNewName));

      RefRename rename = repo.renameRef(fullOldName, fullNewName);
      Result renameResult = rename.rename();

      setCallable(false);

      if (Result.RENAMED != renameResult)
        throw new JGitInternalException(
            MessageFormat.format(JGitText.get().renameBranchUnexpectedResult, renameResult.name()));

      if (fullNewName.startsWith(Constants.R_HEADS)) {
        String shortOldName = fullOldName.substring(Constants.R_HEADS.length());
        final StoredConfig repoConfig = repo.getConfig();
        // Copy all configuration values over to the new branch
        for (String name :
            repoConfig.getNames(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName)) {
          String[] values =
              repoConfig.getStringList(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName, name);
          if (values.length == 0) continue;
          // Keep any existing values already configured for the
          // new branch name
          String[] existing =
              repoConfig.getStringList(ConfigConstants.CONFIG_BRANCH_SECTION, newName, name);
          if (existing.length > 0) {
            String[] newValues = new String[values.length + existing.length];
            System.arraycopy(existing, 0, newValues, 0, existing.length);
            System.arraycopy(values, 0, newValues, existing.length, values.length);
            values = newValues;
          }

          repoConfig.setStringList(
              ConfigConstants.CONFIG_BRANCH_SECTION, newName, name, Arrays.asList(values));
        }
        repoConfig.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName);
        repoConfig.save();
      }

      Ref resultRef = repo.findRef(newName);
      if (resultRef == null)
        throw new JGitInternalException(JGitText.get().renameBranchFailedUnknownReason);
      return resultRef;
    } catch (IOException ioe) {
      throw new JGitInternalException(ioe.getMessage(), ioe);
    }
  }