Exemple #1
0
  /**
   * Fetch information from a particular remote repository. Attempt to fetch from submodules, if
   * they exist in the local WC
   *
   * @param git
   * @param listener
   * @param remoteRepository
   * @throws
   */
  private void fetchFrom(
      IGitAPI git, File workspace, TaskListener listener, RemoteConfig remoteRepository) {
    try {
      listener.getLogger().println("Fetching from " + remoteRepository.getName());
      git.fetch(remoteRepository);

      List<IndexEntry> submodules = new GitUtils(listener, git).getSubmodules("HEAD");

      for (IndexEntry submodule : submodules) {
        try {
          RemoteConfig submoduleRemoteRepository =
              getSubmoduleRepository(remoteRepository, submodule.getFile());

          File subdir = new File(workspace, submodule.getFile());
          IGitAPI subGit =
              new GitAPI(git.getGitExe(), new FilePath(subdir), listener, git.getEnvironment());

          subGit.fetch(submoduleRemoteRepository);
        } catch (Exception ex) {
          listener.error(
              "Problem fetching from "
                  + remoteRepository.getName()
                  + " - could be unavailable. Continuing anyway");
        }
      }
    } catch (GitException ex) {
      listener.error(
          "Problem fetching from "
              + remoteRepository.getName()
              + " / "
              + remoteRepository.getName()
              + " - could be unavailable. Continuing anyway");
    }
  }
  public int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
    token = normalizeToken(token);

    int start = 0;
    int end = sortedIndexEntries.size();

    final Collator sortCollator = sortLanguage.getCollator();
    while (start < end) {
      final int mid = (start + end) / 2;
      if (interrupted.get()) {
        return -1;
      }
      final IndexEntry midEntry = sortedIndexEntries.get(mid);

      final int comp = sortCollator.compare(token, midEntry.normalizedToken());
      if (comp == 0) {
        final int result = windBackCase(token, mid, interrupted);
        return result;
      } else if (comp < 0) {
        // System.out.println("Upper bound: " + midEntry + ", norm=" +
        // midEntry.normalizedToken() + ", mid=" + mid);
        end = mid;
      } else {
        // System.out.println("Lower bound: " + midEntry + ", norm=" +
        // midEntry.normalizedToken() + ", mid=" + mid);
        start = mid + 1;
      }
    }

    // If we search for a substring of a string that's in there, return
    // that.
    int result = Math.min(start, sortedIndexEntries.size() - 1);
    result = windBackCase(sortedIndexEntries.get(result).normalizedToken(), result, interrupted);
    return result;
  }
  protected void makeCombination(Map<IndexEntry, Revision> settings) {
    // Assume we are checked out
    String name = "combine-" + tid + "-" + (idx++);
    git.branch(name);
    git.checkout(name);

    String commit = "Hudson generated combination of:\n";

    for (IndexEntry submodule : settings.keySet()) {
      Revision branch = settings.get(submodule);
      commit += "  " + submodule.getFile() + " " + branch.toString() + "\n";
    }

    listener.getLogger().print(commit);

    for (IndexEntry submodule : settings.keySet()) {
      Revision branch = settings.get(submodule);
      File subdir = new File(workspace, submodule.getFile());
      IGitAPI subGit =
          new GitAPI(git.getGitExe(), new FilePath(subdir), listener, git.getEnvironment());

      subGit.checkout(branch.sha1.name());
      git.add(submodule.file);
    }

    git.commit(commit);
  }
  protected void makeCombination(Map<IndexEntry, Revision> settings) {
    // Assume we are checked out
    String name = "combine-" + tid + "-" + (idx++);
    git.branch(name);
    git.checkout(name);

    StringBuilder commit = new StringBuilder("Jenkins generated combination of:\n");

    for (Entry<IndexEntry, Revision> setting : settings.entrySet()) {
      commit.append(' ').append(' ');
      commit.append(setting.getKey().getFile());
      commit.append(' ');
      commit.append(setting.getValue());
      commit.append('\n');
    }

    listener.getLogger().print(commit);

    for (Entry<IndexEntry, Revision> setting : settings.entrySet()) {
      IndexEntry submodule = setting.getKey();
      Revision branch = setting.getValue();
      File subdir = new File(workspace, submodule.getFile());
      IGitAPI subGit =
          new GitAPI(
              git.getGitExe(),
              new FilePath(subdir),
              listener,
              git.getEnvironment(),
              git.getReference());

      subGit.checkout(branch.sha1.name());
      git.add(submodule.file);
    }

    try {
      File f = File.createTempFile("gitcommit", ".txt");
      FileOutputStream fos = null;
      try {
        fos = new FileOutputStream(f);
        fos.write(commit.toString().getBytes());
      } finally {
        if (fos != null) fos.close();
      }
      git.commit(f);
      f.delete();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Exemple #5
0
 public static Version resolveVersion(
         final Task task,
         final IndexEntry indexEntry,
         final Group group,
         final String studentId
 ) {
     return resolveVersion(
             task,
             indexEntry.getVerAnchor(),
             indexEntry.getVerStep(),
             group.getStudents().keySet(),
             studentId
     );
 }
  public int difference(Map<IndexEntry, Revision> item, List<IndexEntry> entries) {
    int difference = 0;
    if (entries.size() != item.keySet().size()) return -1;

    for (IndexEntry entry : entries) {
      Revision b = null;
      for (IndexEntry e : item.keySet()) {
        if (e.getFile().equals(entry.getFile())) b = item.get(e);
      }

      if (b == null) return -1;

      if (!entry.object.equals(b.getSha1())) difference++;
    }
    return difference;
  }
  public void findCommonUniqueWords(
      int refStart,
      int refEnd,
      int ocrStart,
      int ocrEnd,
      ArrayList<IndexEntry> anchors1,
      ArrayList<IndexEntry> anchors2) {

    if (anchors1 == null || anchors2 == null) {
      System.out.println("findAnchorWordsSorted: arguments anchors1 or 2 can not be null");
    }

    // count words in designated segments
    HashMap<String, IndexEntry> refVocab = refIndex.indexTerms(refStart, refEnd);
    HashMap<String, IndexEntry> candVocab = ocrIndex.indexTerms(ocrStart, ocrEnd);

    // determine unique terms
    HashMap<String, IndexEntry> refUniqueTerms = TermIndexBuilder.findUniqueTerms(refVocab);
    HashMap<String, IndexEntry> candUniqueTerms = TermIndexBuilder.findUniqueTerms(candVocab);

    // find common unique words
    Collection<IndexEntry> col = refUniqueTerms.values();
    IndexEntry[] uniqueTerms = col.toArray(new IndexEntry[0]);
    for (int i = 0;
        i < uniqueTerms.length && anchors1.size() <= MAX_NUMBER_OF_CANDIDATE_ANCHORS;
        i++) {
      IndexEntry ent1 = uniqueTerms[i];
      IndexEntry ent2 = candUniqueTerms.get(ent1.getTerm());
      if (ent2 != null && isValidAnchor(ent1.getTerm())) {
        anchors1.add(ent1);
        anchors2.add(ent2);
      }
    }
    /*      if ( REPORT ){
    System.out.println(groundTruthFile + "\t" + ocrFile + "\t" + refIndex.getNumOfTokens() + "\t" + ocrIndex.getNumOfTokens() +  "\t" + refUniqueTerms.size() + "\t" + candVocab.size() + "\t" + anchors1.size() + "\n");
    REPORT = false;
    }*/
    // sort anchor words based on their positions in the text
    Comparator<IndexEntry> comparator = new TermPosComparator();
    Collections.sort(anchors1, comparator);
    Collections.sort(anchors2, comparator);
  }
  protected void makeCombination(Map<IndexEntry, Revision> settings) {
    // Assume we are checked out
    String name = "combine-" + tid + "-" + (idx++);
    git.branch(name);
    git.checkout(name);

    String commit = "Hudson generated combination of:\n";

    for (IndexEntry submodule : settings.keySet()) {
      Revision branch = settings.get(submodule);
      commit += "  " + submodule.getFile() + " " + branch.toString() + "\n";
    }

    listener.getLogger().print(commit);

    for (IndexEntry submodule : settings.keySet()) {
      Revision branch = settings.get(submodule);
      File subdir = new File(workspace, submodule.getFile());
      IGitAPI subGit =
          new GitAPI(git.getGitExe(), new FilePath(subdir), listener, git.getEnvironment());

      subGit.checkout(branch.sha1.name());
      git.add(submodule.file);
    }

    try {
      File f = File.createTempFile("gitcommit", ".txt");
      FileOutputStream fos = null;
      try {
        fos = new FileOutputStream(f);
        fos.write(commit.getBytes());
      } finally {
        fos.close();
      }
      git.commit(f);
      f.delete();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public void createSubmoduleCombinations() throws GitException, IOException {
    Map<IndexEntry, Collection<Revision>> moduleBranches =
        new HashMap<IndexEntry, Collection<Revision>>();

    for (IndexEntry submodule : git.getSubmodules("HEAD")) {
      File subdir = new File(workspace, submodule.getFile());
      IGitAPI subGit =
          new GitAPI(git.getGitExe(), new FilePath(subdir), listener, git.getEnvironment());

      GitUtils gu = new GitUtils(listener, subGit);
      Collection<Revision> items = gu.filterTipBranches(gu.getAllBranchRevisions());

      filterRevisions(submodule.getFile(), items);

      moduleBranches.put(submodule, items);
    }

    // Remove any uninteresting branches

    for (IndexEntry entry : moduleBranches.keySet()) {
      listener.getLogger().print("Submodule " + entry.getFile() + " branches");
      for (Revision br : moduleBranches.get(entry)) {
        listener.getLogger().print(" " + br.toString());
      }
      listener.getLogger().print("\n");
    }

    // Make all the possible combinations
    List<Map<IndexEntry, Revision>> combinations = createCombinations(moduleBranches);

    listener
        .getLogger()
        .println("There are " + combinations.size() + " submodule/revision combinations possible");

    // Create a map which is SHA1 -> Submodule IDs that were present
    Map<ObjectId, List<IndexEntry>> entriesMap = new HashMap<ObjectId, List<IndexEntry>>();
    // Knock out already-defined configurations
    for (ObjectId sha1 : git.revListAll()) {
      // What's the submodule configuration
      List<IndexEntry> entries = git.getSubmodules(sha1.name());
      entriesMap.put(sha1, entries);
    }

    for (List<IndexEntry> entries : entriesMap.values()) {
      for (Iterator<Map<IndexEntry, Revision>> it = combinations.iterator(); it.hasNext(); ) {
        Map<IndexEntry, Revision> item = it.next();
        if (matches(item, entries)) {
          it.remove();
          break;
        }
      }
    }

    listener
        .getLogger()
        .println("There are " + combinations.size() + " configurations that could be generated.");

    ObjectId headSha1 = git.revParse("HEAD");

    // Make up the combinations

    for (Map<IndexEntry, Revision> combination : combinations) {
      // By default, use the head sha1
      ObjectId sha1 = headSha1;
      int min = Integer.MAX_VALUE;

      // But let's see if we can find the most appropriate place to create the branch
      for (ObjectId sha : entriesMap.keySet()) {
        List<IndexEntry> entries = entriesMap.get(sha);
        int value = difference(combination, entries);
        if (value > 0 && value < min) {
          min = value;
          sha1 = sha;
        }

        if (min == 1) {
          break; // look no further
        }
      }

      git.checkout(sha1.name());
      makeCombination(combination);
    }
  }
Exemple #10
0
  /**
   * Fixes urls for submodule as stored in .git/config and $SUBMODULE/.git/config for when the
   * remote repo is NOT a bare repository. It is only really possible to detect whether a repository
   * is bare if we have local access to the repository. If the repository is remote, we therefore
   * must default to believing that it is either bare or NON-bare. The defaults are according to the
   * ending of the super-project remote.origin.url: - Ends with "/.git": default is NON-bare -
   * otherwise: default is bare .
   *
   * @param listener The task listener.
   * @throws GitException if executing the git command fails
   */
  public void fixSubmoduleUrls(String remote, TaskListener listener) throws GitException {
    boolean is_bare = true;

    URI origin;
    try {
      String url = getRemoteUrl(remote);

      // ensure that any /.git ending is removed
      String gitEnd = pathJoin("", ".git");
      if (url.endsWith(gitEnd)) {
        url = url.substring(0, url.length() - gitEnd.length());
        // change the default detection value to NON-bare
        is_bare = false;
      }

      origin = new URI(url);
    } catch (URISyntaxException e) {
      // Sometimes the URI is of a form that we can't parse; like
      //   [email protected]:repository
      // In these cases, origin is null and it's best to just exit early.
      return;
    } catch (Exception e) {
      throw new GitException("Could determine remote.origin.url", e);
    }

    if (origin.getScheme() == null
        || ("file".equalsIgnoreCase(origin.getScheme())
            && (origin.getHost() == null || "".equals(origin.getHost())))) {
      // The uri is a local path, so we will test to see if it is a bare
      // repository...
      List<String> paths = new ArrayList<String>();
      paths.add(origin.getPath());
      paths.add(pathJoin(origin.getPath(), ".git"));

      for (String path : paths) {
        try {
          is_bare = isBareRepository(path);
          break; // we can break already if we don't have an exception
        } catch (GitException e) {
        }
      }
    }

    if (!is_bare) {
      try {
        List<IndexEntry> submodules = getSubmodules("HEAD");

        for (IndexEntry submodule : submodules) {
          // First fix the URL to the submodule inside the super-project
          String sUrl = pathJoin(origin.getPath(), submodule.getFile());
          setSubmoduleUrl(submodule.getFile(), sUrl);

          // Second, if the submodule already has been cloned, fix its own
          // url...
          String subGitDir = pathJoin(submodule.getFile(), ".git");

          /* it is possible that the submodule does not exist yet
           * since we wait until after checkout to do 'submodule
           * udpate' */
          if (hasGitRepo(subGitDir)) {
            if (!"".equals(getRemoteUrl("origin", subGitDir))) {
              setRemoteUrl("origin", sUrl, subGitDir);
            }
          }
        }
      } catch (GitException e) {
        // this can fail for example HEAD doesn't exist yet
      }
    } else {
      // we've made a reasonable attempt to detect whether the origin is
      // non-bare, so we'll just assume it is bare from here on out and
      // thus the URLs are correct as given by (which is default behavior)
      //    git config --get submodule.NAME.url
    }
  }
  /**
   * Creates nodes from index entries
   *
   * @param theIndexEntries index entries
   * @param theTargetDocument target document
   * @param theIndexEntryComparator comparator to sort the index entries. if it is null the index
   *     entries will be unsorted
   * @return nodes for the target document
   */
  private Node[] transformToNodes(
      final IndexEntry[] theIndexEntries,
      final Document theTargetDocument,
      final Comparator<IndexEntry> theIndexEntryComparator) {
    if (null != theIndexEntryComparator) {
      Arrays.sort(theIndexEntries, theIndexEntryComparator);
    }

    final List<Element> result = new ArrayList<Element>();
    for (final IndexEntry indexEntry : theIndexEntries) {
      final Element indexEntryNode = createElement(theTargetDocument, "index.entry");

      final Element formattedStringElement = createElement(theTargetDocument, "formatted-value");
      if (indexEntry.getContents() != null) {
        for (final Iterator<Node> i = indexEntry.getContents().iterator(); i.hasNext(); ) {
          final Node child = i.next();
          final Node clone = theTargetDocument.importNode(child, true);
          if (!i.hasNext() && clone.getNodeType() == Node.TEXT_NODE) {
            final Text t = (Text) clone;
            t.setData(t.getData().replaceAll("[\\s\\n]+$", ""));
          }
          formattedStringElement.appendChild(clone);
        }
      } else {
        final Text textNode = theTargetDocument.createTextNode(indexEntry.getFormattedString());
        textNode.normalize();
        formattedStringElement.appendChild(textNode);
      }
      indexEntryNode.appendChild(formattedStringElement);

      final String[] refIDs = indexEntry.getRefIDs();
      for (final String refID : refIDs) {
        final Element referenceIDElement = createElement(theTargetDocument, "refID");
        referenceIDElement.setAttribute("value", refID);
        indexEntryNode.appendChild(referenceIDElement);
      }

      final String val = indexEntry.getValue();
      if (null != val) {
        indexEntryNode.setAttribute("value", val);
      }

      final String sort = indexEntry.getSortString();
      if (null != sort) {
        indexEntryNode.setAttribute("sort-string", sort);
      }

      if (indexEntry.isStartingRange()) {
        indexEntryNode.setAttribute("start-range", "true");
      } else if (indexEntry.isEndingRange()) {
        indexEntryNode.setAttribute("end-range", "true");
      }
      if (indexEntry.isSuppressesThePageNumber()) {
        indexEntryNode.setAttribute("no-page", "true");
      } else if (indexEntry.isRestoresPageNumber()) {
        indexEntryNode.setAttribute("single-page", "true");
      }

      final IndexEntry[] childIndexEntries = indexEntry.getChildIndexEntries();

      final Node[] nodes =
          transformToNodes(childIndexEntries, theTargetDocument, theIndexEntryComparator);

      for (final Node node : nodes) {
        indexEntryNode.appendChild(node);
      }

      final IndexEntry[] seeChildIndexEntries = indexEntry.getSeeChildIndexEntries();
      if (seeChildIndexEntries != null) {
        final Element seeElement = createElement(theTargetDocument, "see-childs");
        final Node[] seeNodes =
            transformToNodes(seeChildIndexEntries, theTargetDocument, theIndexEntryComparator);
        for (final Node node : seeNodes) {
          seeElement.appendChild(node);
        }

        indexEntryNode.appendChild(seeElement);
      }

      final IndexEntry[] seeAlsoChildIndexEntries = indexEntry.getSeeAlsoChildIndexEntries();
      if (seeAlsoChildIndexEntries != null) {
        final Element seeAlsoElement = createElement(theTargetDocument, "see-also-childs");
        final Node[] seeAlsoNodes =
            transformToNodes(seeAlsoChildIndexEntries, theTargetDocument, theIndexEntryComparator);
        for (final Node node : seeAlsoNodes) {
          seeAlsoElement.appendChild(node);
        }

        indexEntryNode.appendChild(seeAlsoElement);
      }

      result.add(indexEntryNode);
    }
    return (Node[]) result.toArray(new Node[result.size()]);
  }
 @Override
 public void write(RandomAccessFile raf, IndexEntry t) throws IOException {
   t.write(raf);
 }