Esempio n. 1
0
 /**
  * adds a new path with the specified stage to the index builder
  *
  * @param path
  * @param p
  * @param stage
  * @param lastMod
  * @param len
  * @return the entry which was added to the index
  */
 private DirCacheEntry add(byte[] path, CanonicalTreeParser p, int stage, long lastMod, long len) {
   if (p != null && !p.getEntryFileMode().equals(FileMode.TREE)) {
     DirCacheEntry e = new DirCacheEntry(path, stage);
     e.setFileMode(p.getEntryFileMode());
     e.setObjectId(p.getEntryObjectId());
     e.setLastModified(lastMod);
     e.setLength(len);
     builder.add(e);
     return e;
   }
   return null;
 }
  /**
   * Returns all tree entries that do not match the ignore paths.
   *
   * @param db
   * @param ignorePaths
   * @param dcBuilder
   * @throws IOException
   */
  private List<DirCacheEntry> getTreeEntries(Repository db, Collection<String> ignorePaths)
      throws IOException {
    List<DirCacheEntry> list = new ArrayList<DirCacheEntry>();
    ObjectId treeId = db.resolve(BRANCH + "^{tree}");
    if (treeId == null) {
      // branch does not exist yet, could be migrating tickets
      return list;
    }
    try (TreeWalk tw = new TreeWalk(db)) {
      int hIdx = tw.addTree(treeId);
      tw.setRecursive(true);

      while (tw.next()) {
        String path = tw.getPathString();
        CanonicalTreeParser hTree = null;
        if (hIdx != -1) {
          hTree = tw.getTree(hIdx, CanonicalTreeParser.class);
        }
        if (!ignorePaths.contains(path)) {
          // add all other tree entries
          if (hTree != null) {
            final DirCacheEntry entry = new DirCacheEntry(path);
            entry.setObjectId(hTree.getEntryObjectId());
            entry.setFileMode(hTree.getEntryFileMode());
            list.add(entry);
          }
        }
      }
    }
    return list;
  }
Esempio n. 3
0
  /**
   * Pop the next most recent object.
   *
   * @return next most recent object; null if traversal is over.
   * @throws MissingObjectException one or or more of the next objects are not available from the
   *     object database, but were thought to be candidates for traversal. This usually indicates a
   *     broken link.
   * @throws IncorrectObjectTypeException one or or more of the objects in a tree do not match the
   *     type indicated.
   * @throws IOException a pack file or loose object could not be read.
   */
  public RevObject nextObject()
      throws MissingObjectException, IncorrectObjectTypeException, IOException {
    if (last != null) treeWalk = last instanceof RevTree ? enter(last) : treeWalk.next();

    while (!treeWalk.eof()) {
      final FileMode mode = treeWalk.getEntryFileMode();
      switch (mode.getObjectType()) {
        case Constants.OBJ_BLOB:
          {
            treeWalk.getEntryObjectId(idBuffer);
            final RevBlob o = lookupBlob(idBuffer);
            if ((o.flags & SEEN) != 0) break;
            o.flags |= SEEN;
            if (shouldSkipObject(o)) break;
            last = o;
            return o;
          }
        case Constants.OBJ_TREE:
          {
            treeWalk.getEntryObjectId(idBuffer);
            final RevTree o = lookupTree(idBuffer);
            if ((o.flags & SEEN) != 0) break;
            o.flags |= SEEN;
            if (shouldSkipObject(o)) break;
            last = o;
            return o;
          }
        default:
          if (FileMode.GITLINK.equals(mode)) break;
          treeWalk.getEntryObjectId(idBuffer);
          throw new CorruptObjectException(
              MessageFormat.format(
                  JGitText.get().corruptObjectInvalidMode3,
                  mode,
                  idBuffer.name(),
                  treeWalk.getEntryPathString(),
                  currentTree.name()));
      }

      treeWalk = treeWalk.next();
    }

    last = null;
    for (; ; ) {
      final RevObject o = pendingObjects.next();
      if (o == null) return null;
      if ((o.flags & SEEN) != 0) continue;
      o.flags |= SEEN;
      if (shouldSkipObject(o)) continue;
      if (o instanceof RevTree) {
        currentTree = (RevTree) o;
        treeWalk = treeWalk.resetRoot(db, currentTree, curs);
      }
      return o;
    }
  }
Esempio n. 4
0
  private void markTreeUninteresting(final RevTree tree)
      throws MissingObjectException, IncorrectObjectTypeException, IOException {
    if ((tree.flags & UNINTERESTING) != 0) return;
    tree.flags |= UNINTERESTING;

    treeWalk = treeWalk.resetRoot(db, tree, curs);
    while (!treeWalk.eof()) {
      final FileMode mode = treeWalk.getEntryFileMode();
      final int sType = mode.getObjectType();

      switch (sType) {
        case Constants.OBJ_BLOB:
          {
            treeWalk.getEntryObjectId(idBuffer);
            lookupBlob(idBuffer).flags |= UNINTERESTING;
            break;
          }
        case Constants.OBJ_TREE:
          {
            treeWalk.getEntryObjectId(idBuffer);
            final RevTree t = lookupTree(idBuffer);
            if ((t.flags & UNINTERESTING) == 0) {
              t.flags |= UNINTERESTING;
              treeWalk = treeWalk.createSubtreeIterator0(db, t, curs);
              continue;
            }
            break;
          }
        default:
          if (FileMode.GITLINK.equals(mode)) break;
          treeWalk.getEntryObjectId(idBuffer);
          throw new CorruptObjectException(
              MessageFormat.format(
                  JGitText.get().corruptObjectInvalidMode3,
                  mode,
                  idBuffer.name(),
                  treeWalk.getEntryPathString(),
                  tree));
      }

      treeWalk = treeWalk.next();
    }
  }
Esempio n. 5
0
  private DirCache createTemporaryIndex(ObjectId headId, DirCache index, RevWalk rw)
      throws IOException {
    ObjectInserter inserter = null;

    // get DirCacheBuilder for existing index
    DirCacheBuilder existingBuilder = index.builder();

    // get DirCacheBuilder for newly created in-core index to build a
    // temporary index for this commit
    DirCache inCoreIndex = DirCache.newInCore();
    DirCacheBuilder tempBuilder = inCoreIndex.builder();

    onlyProcessed = new boolean[only.size()];
    boolean emptyCommit = true;

    try (TreeWalk treeWalk = new TreeWalk(repo)) {
      treeWalk.setOperationType(OperationType.CHECKIN_OP);
      int dcIdx = treeWalk.addTree(new DirCacheBuildIterator(existingBuilder));
      FileTreeIterator fti = new FileTreeIterator(repo);
      fti.setDirCacheIterator(treeWalk, 0);
      int fIdx = treeWalk.addTree(fti);
      int hIdx = -1;
      if (headId != null) hIdx = treeWalk.addTree(rw.parseTree(headId));
      treeWalk.setRecursive(true);

      String lastAddedFile = null;
      while (treeWalk.next()) {
        String path = treeWalk.getPathString();
        // check if current entry's path matches a specified path
        int pos = lookupOnly(path);

        CanonicalTreeParser hTree = null;
        if (hIdx != -1) hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);

        DirCacheIterator dcTree = treeWalk.getTree(dcIdx, DirCacheIterator.class);

        if (pos >= 0) {
          // include entry in commit

          FileTreeIterator fTree = treeWalk.getTree(fIdx, FileTreeIterator.class);

          // check if entry refers to a tracked file
          boolean tracked = dcTree != null || hTree != null;
          if (!tracked) continue;

          // for an unmerged path, DirCacheBuildIterator will yield 3
          // entries, we only want to add one
          if (path.equals(lastAddedFile)) continue;

          lastAddedFile = path;

          if (fTree != null) {
            // create a new DirCacheEntry with data retrieved from
            // disk
            final DirCacheEntry dcEntry = new DirCacheEntry(path);
            long entryLength = fTree.getEntryLength();
            dcEntry.setLength(entryLength);
            dcEntry.setLastModified(fTree.getEntryLastModified());
            dcEntry.setFileMode(fTree.getIndexFileMode(dcTree));

            boolean objectExists =
                (dcTree != null && fTree.idEqual(dcTree))
                    || (hTree != null && fTree.idEqual(hTree));
            if (objectExists) {
              dcEntry.setObjectId(fTree.getEntryObjectId());
            } else {
              if (FileMode.GITLINK.equals(dcEntry.getFileMode()))
                dcEntry.setObjectId(fTree.getEntryObjectId());
              else {
                // insert object
                if (inserter == null) inserter = repo.newObjectInserter();
                long contentLength = fTree.getEntryContentLength();
                InputStream inputStream = fTree.openEntryStream();
                try {
                  dcEntry.setObjectId(
                      inserter.insert(Constants.OBJ_BLOB, contentLength, inputStream));
                } finally {
                  inputStream.close();
                }
              }
            }

            // add to existing index
            existingBuilder.add(dcEntry);
            // add to temporary in-core index
            tempBuilder.add(dcEntry);

            if (emptyCommit
                && (hTree == null
                    || !hTree.idEqual(fTree)
                    || hTree.getEntryRawMode() != fTree.getEntryRawMode()))
              // this is a change
              emptyCommit = false;
          } else {
            // if no file exists on disk, neither add it to
            // index nor to temporary in-core index

            if (emptyCommit && hTree != null)
              // this is a change
              emptyCommit = false;
          }

          // keep track of processed path
          onlyProcessed[pos] = true;
        } else {
          // add entries from HEAD for all other paths
          if (hTree != null) {
            // create a new DirCacheEntry with data retrieved from
            // HEAD
            final DirCacheEntry dcEntry = new DirCacheEntry(path);
            dcEntry.setObjectId(hTree.getEntryObjectId());
            dcEntry.setFileMode(hTree.getEntryFileMode());

            // add to temporary in-core index
            tempBuilder.add(dcEntry);
          }

          // preserve existing entry in index
          if (dcTree != null) existingBuilder.add(dcTree.getDirCacheEntry());
        }
      }
    }

    // there must be no unprocessed paths left at this point; otherwise an
    // untracked or unknown path has been specified
    for (int i = 0; i < onlyProcessed.length; i++)
      if (!onlyProcessed[i])
        throw new JGitInternalException(
            MessageFormat.format(JGitText.get().entryNotFoundByPath, only.get(i)));

    // there must be at least one change
    if (emptyCommit) throw new JGitInternalException(JGitText.get().emptyCommit);

    // update index
    existingBuilder.commit();
    // finish temporary in-core index used for this commit
    tempBuilder.finish();
    return inCoreIndex;
  }
  /**
   * Deletes a ticket from the repository.
   *
   * @param ticket
   * @return true if successful
   */
  @Override
  protected synchronized boolean deleteTicketImpl(
      RepositoryModel repository, TicketModel ticket, String deletedBy) {
    if (ticket == null) {
      throw new RuntimeException("must specify a ticket!");
    }

    boolean success = false;
    Repository db = repositoryManager.getRepository(ticket.repository);
    try {
      RefModel ticketsBranch = getTicketsBranch(db);

      if (ticketsBranch == null) {
        throw new RuntimeException(BRANCH + " does not exist!");
      }
      String ticketPath = toTicketPath(ticket.number);

      try {
        ObjectId treeId = db.resolve(BRANCH + "^{tree}");

        // Create the in-memory index of the new/updated ticket
        DirCache index = DirCache.newInCore();
        DirCacheBuilder builder = index.builder();

        // Traverse HEAD to add all other paths
        try (TreeWalk treeWalk = new TreeWalk(db)) {
          int hIdx = -1;
          if (treeId != null) {
            hIdx = treeWalk.addTree(treeId);
          }
          treeWalk.setRecursive(true);
          while (treeWalk.next()) {
            String path = treeWalk.getPathString();
            CanonicalTreeParser hTree = null;
            if (hIdx != -1) {
              hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
            }
            if (!path.startsWith(ticketPath)) {
              // add entries from HEAD for all other paths
              if (hTree != null) {
                final DirCacheEntry entry = new DirCacheEntry(path);
                entry.setObjectId(hTree.getEntryObjectId());
                entry.setFileMode(hTree.getEntryFileMode());

                // add to temporary in-core index
                builder.add(entry);
              }
            }
          }
        }

        // finish temporary in-core index used for this commit
        builder.finish();

        success = commitIndex(db, index, deletedBy, "- " + ticket.number);

      } catch (Throwable t) {
        log.error(
            MessageFormat.format(
                "Failed to delete ticket {0,number,0} from {1}", ticket.number, db.getDirectory()),
            t);
      }
    } finally {
      db.close();
    }
    return success;
  }
Esempio n. 7
0
  /**
   * Here the main work is done. This method is called for each existing path in head, index and
   * merge. This method decides what to do with the corresponding index entry: keep it, update it,
   * remove it or mark a conflict.
   *
   * @param h the entry for the head
   * @param m the entry for the merge
   * @param i the entry for the index
   * @param f the file in the working tree
   * @throws IOException
   */
  void processEntry(
      CanonicalTreeParser h, CanonicalTreeParser m, DirCacheBuildIterator i, WorkingTreeIterator f)
      throws IOException {
    DirCacheEntry dce = i != null ? i.getDirCacheEntry() : null;

    String name = walk.getPathString();

    if (m != null && !isValidPath(m)) throw new InvalidPathException(m.getEntryPathString());

    if (i == null && m == null && h == null) {
      // File/Directory conflict case #20
      if (walk.isDirectoryFileConflict())
        // TODO: check whether it is always correct to report a conflict here
        conflict(name, null, null, null);

      // file only exists in working tree -> ignore it
      return;
    }

    ObjectId iId = (i == null ? null : i.getEntryObjectId());
    ObjectId mId = (m == null ? null : m.getEntryObjectId());
    ObjectId hId = (h == null ? null : h.getEntryObjectId());
    FileMode iMode = (i == null ? null : i.getEntryFileMode());
    FileMode mMode = (m == null ? null : m.getEntryFileMode());
    FileMode hMode = (h == null ? null : h.getEntryFileMode());

    /**
     *
     *
     * <pre>
     *  File/Directory conflicts:
     *  the following table from ReadTreeTest tells what to do in case of directory/file
     *  conflicts. I give comments here
     *
     *      H        I       M     Clean     H==M     H==I    I==M         Result
     *      ------------------------------------------------------------------
     * 1    D        D       F       Y         N       Y       N           Update
     * 2    D        D       F       N         N       Y       N           Conflict
     * 3    D        F       D                 Y       N       N           Keep
     * 4    D        F       D                 N       N       N           Conflict
     * 5    D        F       F       Y         N       N       Y           Keep
     * 6    D        F       F       N         N       N       Y           Keep
     * 7    F        D       F       Y         Y       N       N           Update
     * 8    F        D       F       N         Y       N       N           Conflict
     * 9    F        D       F       Y         N       N       N           Update
     * 10   F        D       D                 N       N       Y           Keep
     * 11   F        D       D                 N       N       N           Conflict
     * 12   F        F       D       Y         N       Y       N           Update
     * 13   F        F       D       N         N       Y       N           Conflict
     * 14   F        F       D                 N       N       N           Conflict
     * 15   0        F       D                 N       N       N           Conflict
     * 16   0        D       F       Y         N       N       N           Update
     * 17   0        D       F                 N       N       N           Conflict
     * 18   F        0       D                                             Update
     * 19   D        0       F                                             Update
     * 20   0        0       F       N (worktree=dir)                      Conflict
     * </pre>
     */

    // The information whether head,index,merge iterators are currently
    // pointing to file/folder/non-existing is encoded into this variable.
    //
    // To decode write down ffMask in hexadecimal form. The last digit
    // represents the state for the merge iterator, the second last the
    // state for the index iterator and the third last represents the state
    // for the head iterator. The hexadecimal constant "F" stands for
    // "file",
    // an "D" stands for "directory" (tree), and a "0" stands for
    // non-existing
    //
    // Examples:
    // ffMask == 0xFFD -> Head=File, Index=File, Merge=Tree
    // ffMask == 0xDD0 -> Head=Tree, Index=Tree, Merge=Non-Existing

    int ffMask = 0;
    if (h != null) ffMask = FileMode.TREE.equals(hMode) ? 0xD00 : 0xF00;
    if (i != null) ffMask |= FileMode.TREE.equals(iMode) ? 0x0D0 : 0x0F0;
    if (m != null) ffMask |= FileMode.TREE.equals(mMode) ? 0x00D : 0x00F;

    // Check whether we have a possible file/folder conflict. Therefore we
    // need a least one file and one folder.
    if (((ffMask & 0x222) != 0x000)
        && (((ffMask & 0x00F) == 0x00D)
            || ((ffMask & 0x0F0) == 0x0D0)
            || ((ffMask & 0xF00) == 0xD00))) {

      // There are 3*3*3=27 possible combinations of file/folder
      // conflicts. Some of them are not-relevant because
      // they represent no conflict, e.g. 0xFFF, 0xDDD, ... The following
      // switch processes all relevant cases.
      switch (ffMask) {
        case 0xDDF: // 1 2
          if (isModified(name)) {
            conflict(name, dce, h, m); // 1
          } else {
            update(name, mId, mMode); // 2
          }

          break;
        case 0xDFD: // 3 4
          keep(dce);
          break;
        case 0xF0D: // 18
          remove(name);
          break;
        case 0xDFF: // 5 6
        case 0xFDD: // 10 11
          // TODO: make use of tree extension as soon as available in jgit
          // we would like to do something like
          // if (!equalIdAndMode(iId, iMode, mId, mMode)
          //   conflict(name, i.getDirCacheEntry(), h, m);
          // But since we don't know the id of a tree in the index we do
          // nothing here and wait that conflicts between index and merge
          // are found later
          break;
        case 0xD0F: // 19
          update(name, mId, mMode);
          break;
        case 0xDF0: // conflict without a rule
        case 0x0FD: // 15
          conflict(name, dce, h, m);
          break;
        case 0xFDF: // 7 8 9
          if (equalIdAndMode(hId, hMode, mId, mMode)) {
            if (isModified(name)) conflict(name, dce, h, m); // 8
            else update(name, mId, mMode); // 7
          } else if (!isModified(name)) update(name, mId, mMode); // 9
          else
            // To be confirmed - this case is not in the table.
            conflict(name, dce, h, m);
          break;
        case 0xFD0: // keep without a rule
          keep(dce);
          break;
        case 0xFFD: // 12 13 14
          if (equalIdAndMode(hId, hMode, iId, iMode))
            if (f == null || f.isModified(dce, true)) conflict(name, dce, h, m);
            else remove(name);
          else conflict(name, dce, h, m);
          break;
        case 0x0DF: // 16 17
          if (!isModified(name)) update(name, mId, mMode);
          else conflict(name, dce, h, m);
          break;
        default:
          keep(dce);
      }
      return;
    }

    // if we have no file at all then there is nothing to do
    if ((ffMask & 0x222) == 0) return;

    if ((ffMask == 0x00F) && f != null && FileMode.TREE.equals(f.getEntryFileMode())) {
      // File/Directory conflict case #20
      conflict(name, null, h, m);
    }

    if (i == null) {
      // Nothing in Index
      // At least one of Head, Index, Merge is not empty
      // make sure not to overwrite untracked files
      if (f != null) {
        // A submodule is not a file. We should ignore it
        if (!FileMode.GITLINK.equals(mMode)) {
          // a dirty worktree: the index is empty but we have a
          // workingtree-file
          if (mId == null
              || !equalIdAndMode(mId, mMode, f.getEntryObjectId(), f.getEntryFileMode())) {
            conflict(name, null, h, m);
            return;
          }
        }
      }

      /**
       *
       *
       * <pre>
       * 	          I (index)     H        M     H==M  Result
       * 	        -------------------------------------------
       * 	        0 nothing    nothing  nothing        (does not happen)
       * 	        1 nothing    nothing  exists         use M
       * 	        2 nothing    exists   nothing        remove path from index
       * 	        3 nothing    exists   exists   yes   keep index
       * 	          nothing    exists   exists   no    fail
       * </pre>
       */
      if (h == null)
        // Nothing in Head
        // Nothing in Index
        // At least one of Head, Index, Merge is not empty
        // -> only Merge contains something for this path. Use it!
        // Potentially update the file
        update(name, mId, mMode); // 1
      else if (m == null)
        // Nothing in Merge
        // Something in Head
        // Nothing in Index
        // -> only Head contains something for this path and it should
        // be deleted. Potentially removes the file!
        remove(name); // 2
      else { // 3
        // Something in Merge
        // Something in Head
        // Nothing in Index
        // -> Head and Merge contain something (maybe not the same) and
        // in the index there is nothing (e.g. 'git rm ...' was
        // called before). Ignore the cached deletion and use what we
        // find in Merge. Potentially updates the file.
        if (equalIdAndMode(hId, hMode, mId, mMode)) keep(dce);
        else conflict(name, dce, h, m);
      }
    } else {
      // Something in Index
      if (h == null) {
        // Nothing in Head
        // Something in Index
        /**
         *
         *
         * <pre>
         * 	          clean I==H  I==M       H        M        Result
         * 	         -----------------------------------------------------
         * 	        4 yes   N/A   N/A     nothing  nothing  keep index
         * 	        5 no    N/A   N/A     nothing  nothing  keep index
         *
         * 	        6 yes   N/A   yes     nothing  exists   keep index
         * 	        7 no    N/A   yes     nothing  exists   keep index
         * 	        8 yes   N/A   no      nothing  exists   fail
         * 	        9 no    N/A   no      nothing  exists   fail
         * </pre>
         */
        if (m == null || equalIdAndMode(mId, mMode, iId, iMode)) {
          // Merge contains nothing or the same as Index
          // Nothing in Head
          // Something in Index
          if (m == null && walk.isDirectoryFileConflict()) {
            // Nothing in Merge and current path is part of
            // File/Folder conflict
            // Nothing in Head
            // Something in Index
            if (dce != null && (f == null || f.isModified(dce, true)))
              // No file or file is dirty
              // Nothing in Merge and current path is part of
              // File/Folder conflict
              // Nothing in Head
              // Something in Index
              // -> File folder conflict and Merge wants this
              // path to be removed. Since the file is dirty
              // report a conflict
              conflict(name, dce, h, m);
            else
              // A file is present and file is not dirty
              // Nothing in Merge and current path is part of
              // File/Folder conflict
              // Nothing in Head
              // Something in Index
              // -> File folder conflict and Merge wants this path
              // to be removed. Since the file is not dirty remove
              // file and index entry
              remove(name);
          } else
            // Something in Merge or current path is not part of
            // File/Folder conflict
            // Merge contains nothing or the same as Index
            // Nothing in Head
            // Something in Index
            // -> Merge contains nothing new. Keep the index.
            keep(dce);
        } else
          // Merge contains something and it is not the same as Index
          // Nothing in Head
          // Something in Index
          // -> Index contains something new (different from Head)
          // and Merge is different from Index. Report a conflict
          conflict(name, dce, h, m);
      } else if (m == null) {
        // Nothing in Merge
        // Something in Head
        // Something in Index

        /**
         *
         *
         * <pre>
         * 	           clean I==H  I==M       H        M        Result
         * 	         -----------------------------------------------------
         * 	        10 yes   yes   N/A     exists   nothing  remove path from index
         * 	        11 no    yes   N/A     exists   nothing  fail
         * 	        12 yes   no    N/A     exists   nothing  fail
         * 	        13 no    no    N/A     exists   nothing  fail
         * </pre>
         */
        if (iMode == FileMode.GITLINK) {
          // A submodule in Index
          // Nothing in Merge
          // Something in Head
          // Submodules that disappear from the checkout must
          // be removed from the index, but not deleted from disk.
          remove(name);
        } else {
          // Something different from a submodule in Index
          // Nothing in Merge
          // Something in Head
          if (equalIdAndMode(hId, hMode, iId, iMode)) {
            // Index contains the same as Head
            // Something different from a submodule in Index
            // Nothing in Merge
            // Something in Head
            if (f == null || f.isModified(dce, true))
              // file is dirty
              // Index contains the same as Head
              // Something different from a submodule in Index
              // Nothing in Merge
              // Something in Head
              // -> file is dirty but is should be removed. That's
              // a conflict
              conflict(name, dce, h, m);
            else
              // file doesn't exist or is clean
              // Index contains the same as Head
              // Something different from a submodule in Index
              // Nothing in Merge
              // Something in Head
              // -> Remove from index and delete the file
              remove(name);
          } else
            // Index contains something different from Head
            // Something different from a submodule in Index
            // Nothing in Merge
            // Something in Head
            // -> Something new is in index (and maybe even on the
            // filesystem). But Merge wants the path to be removed.
            // Report a conflict
            conflict(name, dce, h, m);
        }
      } else {
        // Something in Merge
        // Something in Head
        // Something in Index
        if (!equalIdAndMode(hId, hMode, mId, mMode)
            && !equalIdAndMode(hId, hMode, iId, iMode)
            && !equalIdAndMode(mId, mMode, iId, iMode))
          // All three contents in Head, Merge, Index differ from each
          // other
          // -> All contents differ. Report a conflict.
          conflict(name, dce, h, m);
        else
        // At least two of the contents of Head, Index, Merge
        // are the same
        // Something in Merge
        // Something in Head
        // Something in Index

        if (equalIdAndMode(hId, hMode, iId, iMode) && !equalIdAndMode(mId, mMode, iId, iMode)) {
          // Head contains the same as Index. Merge differs
          // Something in Merge

          // For submodules just update the index with the new SHA-1
          if (dce != null && FileMode.GITLINK.equals(dce.getFileMode())) {
            // Index and Head contain the same submodule. Merge
            // differs
            // Something in Merge
            // -> Nothing new in index. Move to merge.
            // Potentially updates the file

            // TODO check that we don't overwrite some unsaved
            // file content
            update(name, mId, mMode);
          } else if (dce != null && (f == null || f.isModified(dce, true))) {
            // File doesn't exist or is dirty
            // Head and Index don't contain a submodule
            // Head contains the same as Index. Merge differs
            // Something in Merge
            // -> Merge wants the index and file to be updated
            // but the file is dirty. Report a conflict
            conflict(name, dce, h, m);
          } else {
            // File exists and is clean
            // Head and Index don't contain a submodule
            // Head contains the same as Index. Merge differs
            // Something in Merge
            // -> Standard case when switching between branches:
            // Nothing new in index but something different in
            // Merge. Update index and file
            update(name, mId, mMode);
          }
        } else {
          // Head differs from index or merge is same as index
          // At least two of the contents of Head, Index, Merge
          // are the same
          // Something in Merge
          // Something in Head
          // Something in Index

          // Can be formulated as: Either all three states are
          // equal or Merge is equal to Head or Index and differs
          // to the other one.
          // -> In all three cases we don't touch index and file.

          keep(dce);
        }
      }
    }
  }
Esempio n. 8
0
 /**
  * Processing an entry in the context of {@link #prescanOneTree()} when only one tree is given
  *
  * @param m the tree to merge
  * @param i the index
  * @param f the working tree
  * @throws IOException
  */
 void processEntry(CanonicalTreeParser m, DirCacheBuildIterator i, WorkingTreeIterator f)
     throws IOException {
   if (m != null) {
     if (!isValidPath(m)) throw new InvalidPathException(m.getEntryPathString());
     // There is an entry in the merge commit. Means: we want to update
     // what's currently in the index and working-tree to that one
     if (i == null) {
       // The index entry is missing
       if (f != null && !FileMode.TREE.equals(f.getEntryFileMode()) && !f.isEntryIgnored()) {
         // don't overwrite an untracked and not ignored file
         conflicts.add(walk.getPathString());
       } else update(m.getEntryPathString(), m.getEntryObjectId(), m.getEntryFileMode());
     } else if (f == null || !m.idEqual(i)) {
       // The working tree file is missing or the merge content differs
       // from index content
       update(m.getEntryPathString(), m.getEntryObjectId(), m.getEntryFileMode());
     } else if (i.getDirCacheEntry() != null) {
       // The index contains a file (and not a folder)
       if (f.isModified(i.getDirCacheEntry(), true) || i.getDirCacheEntry().getStage() != 0)
         // The working tree file is dirty or the index contains a
         // conflict
         update(m.getEntryPathString(), m.getEntryObjectId(), m.getEntryFileMode());
       else {
         // update the timestamp of the index with the one from the
         // file if not set, as we are sure to be in sync here.
         DirCacheEntry entry = i.getDirCacheEntry();
         if (entry.getLastModified() == 0) entry.setLastModified(f.getEntryLastModified());
         keep(entry);
       }
     } else
       // The index contains a folder
       keep(i.getDirCacheEntry());
   } else {
     // There is no entry in the merge commit. Means: we want to delete
     // what's currently in the index and working tree
     if (f != null) {
       // There is a file/folder for that path in the working tree
       if (walk.isDirectoryFileConflict()) {
         conflicts.add(walk.getPathString());
       } else {
         // No file/folder conflict exists. All entries are files or
         // all entries are folders
         if (i != null) {
           // ... and the working tree contained a file or folder
           // -> add it to the removed set and remove it from
           // conflicts set
           remove(i.getEntryPathString());
           conflicts.remove(i.getEntryPathString());
         } else {
           // untracked file, neither contained in tree to merge
           // nor in index
         }
       }
     } else {
       // There is no file/folder for that path in the working tree,
       // nor in the merge head.
       // The only entry we have is the index entry. Like the case
       // where there is a file with the same name, remove it,
     }
   }
 }
Esempio n. 9
0
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    handleAuth(req);
    resp.setCharacterEncoding("UTF-8");
    final PrintWriter out = resp.getWriter();
    try {
      String pathInfo = req.getPathInfo();
      Pattern pattern = Pattern.compile("/([^/]*)(?:/([^/]*)(?:/(.*))?)?");
      Matcher matcher = pattern.matcher(pathInfo);
      matcher.matches();
      String projectName = null;
      String refName = null;
      String filePath = null;
      if (matcher.groupCount() > 0) {
        projectName = matcher.group(1);
        refName = matcher.group(2);
        filePath = matcher.group(3);
        if (projectName == null || projectName.equals("")) {
          projectName = null;
        } else {
          projectName = java.net.URLDecoder.decode(projectName, "UTF-8");
        }
        if (refName == null || refName.equals("")) {
          refName = null;
        } else {
          refName = java.net.URLDecoder.decode(refName, "UTF-8");
        }
        if (filePath == null || filePath.equals("")) {
          filePath = null;
        } else {
          filePath = java.net.URLDecoder.decode(filePath, "UTF-8");
        }
      }
      if (projectName != null) {
        if (filePath == null) filePath = "";
        NameKey projName = NameKey.parse(projectName);

        ProjectControl control;
        try {
          control = projControlFactory.controlFor(projName);
          if (!control.isVisible()) {
            log.debug("Project not visible!");
            resp.sendError(
                HttpServletResponse.SC_UNAUTHORIZED,
                "You need to be logged in to see private projects");
            return;
          }
        } catch (NoSuchProjectException e1) {
        }
        Repository repo = repoManager.openRepository(projName);
        if (refName == null) {
          JSONArray contents = new JSONArray();
          List<Ref> call;
          try {
            call = new Git(repo).branchList().call();
            Git git = new Git(repo);
            for (Ref ref : call) {
              JSONObject jsonObject = new JSONObject();
              try {
                jsonObject.put("name", ref.getName());
                jsonObject.put("type", "ref");
                jsonObject.put("size", "0");
                jsonObject.put("path", "");
                jsonObject.put("project", projectName);
                jsonObject.put("ref", ref.getName());
                lastCommit(git, null, ref.getObjectId(), jsonObject);
              } catch (JSONException e) {
              }
              contents.put(jsonObject);
            }
            String response = contents.toString();
            resp.setContentType("application/json");
            resp.setHeader("Cache-Control", "no-cache");
            resp.setHeader("ETag", "W/\"" + response.length() + "-" + response.hashCode() + "\"");
            log.debug(response);
            out.write(response);
          } catch (GitAPIException e) {
          }
        } else {
          Ref head = repo.getRef(refName);
          if (head == null) {
            JSONArray contents = new JSONArray();
            String response = contents.toString();
            resp.setContentType("application/json");
            resp.setHeader("Cache-Control", "no-cache");
            resp.setHeader("ETag", "W/\"" + response.length() + "-" + response.hashCode() + "\"");
            log.debug(response);
            out.write(response);
            return;
          }
          RevWalk walk = new RevWalk(repo);
          // add try catch to catch failures
          Git git = new Git(repo);
          RevCommit commit = walk.parseCommit(head.getObjectId());
          RevTree tree = commit.getTree();
          TreeWalk treeWalk = new TreeWalk(repo);
          treeWalk.addTree(tree);
          treeWalk.setRecursive(false);
          if (!filePath.equals("")) {
            PathFilter pathFilter = PathFilter.create(filePath);
            treeWalk.setFilter(pathFilter);
          }
          if (!treeWalk.next()) {
            CanonicalTreeParser canonicalTreeParser =
                treeWalk.getTree(0, CanonicalTreeParser.class);
            JSONArray contents = new JSONArray();
            if (canonicalTreeParser != null) {
              while (!canonicalTreeParser.eof()) {
                String path = canonicalTreeParser.getEntryPathString();
                FileMode mode = canonicalTreeParser.getEntryFileMode();
                listEntry(
                    path,
                    mode.equals(FileMode.TREE) ? "dir" : "file",
                    "0",
                    path,
                    projectName,
                    head.getName(),
                    git,
                    contents);
                canonicalTreeParser.next();
              }
            }
            String response = contents.toString();
            resp.setContentType("application/json");
            resp.setHeader("Cache-Control", "no-cache");
            resp.setHeader("ETag", "\"" + tree.getId().getName() + "\"");
            log.debug(response);
            out.write(response);
          } else {
            // if (treeWalk.isSubtree()) {
            // treeWalk.enterSubtree();
            // }

            JSONArray contents = getListEntries(treeWalk, repo, git, head, filePath, projectName);
            String response = contents.toString();

            resp.setContentType("application/json");
            resp.setHeader("Cache-Control", "no-cache");
            resp.setHeader("ETag", "\"" + tree.getId().getName() + "\"");
            log.debug(response);
            out.write(response);
          }
          walk.release();
          treeWalk.release();
        }
      }
    } catch (RepositoryNotFoundException e) {
      handleException(resp, e, 400);
    } catch (MissingObjectException e) {
      // example "Missing unknown 7035305927ca125757ecd8407e608f6dcf0bd8a5"
      // usually indicative of being unable to locate a commit from a submodule
      log.error(e.getMessage(), e);
      String msg =
          e.getMessage()
              + ".  This exception could have been caused by the use of a git submodule, "
              + "which is currently not supported by the repository browser.";
      handleException(resp, new Exception(msg), 501);
    } catch (IOException e) {
      handleException(resp, e, 500);
    } finally {
      out.close();
    }
  }