Exemplo n.º 1
0
  private boolean isIndexDirty() {
    if (inCore) return false;

    final int modeI = tw.getRawMode(T_INDEX);
    final int modeO = tw.getRawMode(T_OURS);

    // Index entry has to match ours to be considered clean
    final boolean isDirty = nonTree(modeI) && !(modeO == modeI && tw.idEqual(T_INDEX, T_OURS));
    if (isDirty) failingPaths.put(tw.getPathString(), MergeFailureReason.DIRTY_INDEX);
    return isDirty;
  }
Exemplo n.º 2
0
  /**
   * The resolve conflict way of three way merging
   *
   * @param baseTree
   * @param headTree
   * @param mergeTree
   * @param ignoreConflicts Controls what to do in case a content-merge is done and a conflict is
   *     detected. The default setting for this should be <code>false</code>. In this case the
   *     working tree file is filled with new content (containing conflict markers) and the index is
   *     filled with multiple stages containing BASE, OURS and THEIRS content. Having such non-0
   *     stages is the sign to git tools that there are still conflicts for that path.
   *     <p>If <code>true</code> is specified the behavior is different. In case a conflict is
   *     detected the working tree file is again filled with new content (containing conflict
   *     markers). But also stage 0 of the index is filled with that content. No other stages are
   *     filled. Means: there is no conflict on that path but the new content (including conflict
   *     markers) is stored as successful merge result. This is needed in the context of {@link
   *     RecursiveMerger} where when determining merge bases we don't want to deal with
   *     content-merge conflicts.
   * @return whether the trees merged cleanly
   * @throws IOException
   * @since 3.5
   */
  protected boolean mergeTrees(
      AbstractTreeIterator baseTree, RevTree headTree, RevTree mergeTree, boolean ignoreConflicts)
      throws IOException {

    builder = dircache.builder();
    DirCacheBuildIterator buildIt = new DirCacheBuildIterator(builder);

    tw = new NameConflictTreeWalk(reader);
    tw.addTree(baseTree);
    tw.addTree(headTree);
    tw.addTree(mergeTree);
    tw.addTree(buildIt);
    if (workingTreeIterator != null) {
      tw.addTree(workingTreeIterator);
    } else {
      tw.setFilter(TreeFilter.ANY_DIFF);
    }

    if (!mergeTreeWalk(tw, ignoreConflicts)) {
      return false;
    }

    if (!inCore) {
      // No problem found. The only thing left to be done is to
      // checkout all files from "theirs" which have been selected to
      // go into the new index.
      checkout();

      // All content-merges are successfully done. If we can now write the
      // new index we are on quite safe ground. Even if the checkout of
      // files coming from "theirs" fails the user can work around such
      // failures by checking out the index again.
      if (!builder.commit()) {
        cleanUp();
        throw new IndexWriteException();
      }
      builder = null;

    } else {
      builder.finish();
      builder = null;
    }

    if (getUnmergedPaths().isEmpty() && !failed()) {
      resultTree = dircache.writeTree(getObjectInserter());
      return true;
    } else {
      resultTree = null;
      return false;
    }
  }
Exemplo n.º 3
0
  private boolean isWorktreeDirty(WorkingTreeIterator work, DirCacheEntry ourDce)
      throws IOException {
    if (work == null) return false;

    final int modeF = tw.getRawMode(T_FILE);
    final int modeO = tw.getRawMode(T_OURS);

    // Worktree entry has to match ours to be considered clean
    boolean isDirty;
    if (ourDce != null) isDirty = work.isModified(ourDce, true, reader);
    else {
      isDirty = work.isModeDifferent(modeO);
      if (!isDirty && nonTree(modeF)) isDirty = !tw.idEqual(T_FILE, T_OURS);
    }

    // Ignore existing empty directories
    if (isDirty && modeF == FileMode.TYPE_TREE && modeO == FileMode.TYPE_MISSING) isDirty = false;
    if (isDirty) failingPaths.put(tw.getPathString(), MergeFailureReason.DIRTY_WORKTREE);
    return isDirty;
  }
Exemplo n.º 4
0
 /**
  * Writes merged file content to the working tree.
  *
  * @param result the result of the content merge
  * @return the working tree file to which the merged content was written.
  * @throws FileNotFoundException
  * @throws IOException
  */
 private File writeMergedFile(MergeResult<RawText> result)
     throws FileNotFoundException, IOException {
   File workTree = db.getWorkTree();
   FS fs = db.getFS();
   File of = new File(workTree, tw.getPathString());
   File parentFolder = of.getParentFile();
   if (!fs.exists(parentFolder)) parentFolder.mkdirs();
   try (OutputStream os = new BufferedOutputStream(new FileOutputStream(of))) {
     new MergeFormatter().formatMerge(os, result, Arrays.asList(commitNames), CHARACTER_ENCODING);
   }
   return of;
 }
Exemplo n.º 5
0
 private boolean isModified(String path) throws CorruptObjectException, IOException {
   NameConflictTreeWalk tw = new NameConflictTreeWalk(repo);
   tw.addTree(new DirCacheIterator(dc));
   tw.addTree(new FileTreeIterator(repo));
   tw.setRecursive(true);
   tw.setFilter(PathFilter.create(path));
   DirCacheIterator dcIt;
   WorkingTreeIterator wtIt;
   while (tw.next()) {
     dcIt = tw.getTree(0, DirCacheIterator.class);
     wtIt = tw.getTree(1, WorkingTreeIterator.class);
     if (dcIt == null || wtIt == null) return true;
     if (wtIt.isModified(dcIt.getDirCacheEntry(), true)) {
       return true;
     }
   }
   return false;
 }
Exemplo n.º 6
0
 /**
  * Process the given TreeWalk's entries.
  *
  * @param treeWalk The walk to iterate over.
  * @param ignoreConflicts see {@link ResolveMerger#mergeTrees(AbstractTreeIterator, RevTree,
  *     RevTree, boolean)}
  * @return Whether the trees merged cleanly.
  * @throws IOException
  * @since 3.5
  */
 protected boolean mergeTreeWalk(TreeWalk treeWalk, boolean ignoreConflicts) throws IOException {
   boolean hasWorkingTreeIterator = tw.getTreeCount() > T_FILE;
   while (treeWalk.next()) {
     if (!processEntry(
         treeWalk.getTree(T_BASE, CanonicalTreeParser.class),
         treeWalk.getTree(T_OURS, CanonicalTreeParser.class),
         treeWalk.getTree(T_THEIRS, CanonicalTreeParser.class),
         treeWalk.getTree(T_INDEX, DirCacheBuildIterator.class),
         hasWorkingTreeIterator ? treeWalk.getTree(T_FILE, WorkingTreeIterator.class) : null,
         ignoreConflicts)) {
       cleanUp();
       return false;
     }
     if (treeWalk.isSubtree() && enterSubtree) treeWalk.enterSubtree();
   }
   return true;
 }
Exemplo n.º 7
0
  /**
   * Scan index and merge tree (no HEAD). Used e.g. for initial checkout when there is no head yet.
   *
   * @throws MissingObjectException
   * @throws IncorrectObjectTypeException
   * @throws CorruptObjectException
   * @throws IOException
   */
  public void prescanOneTree()
      throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException,
          IOException {
    removed.clear();
    updated.clear();
    conflicts.clear();

    builder = dc.builder();

    walk = new NameConflictTreeWalk(repo);
    walk.addTree(mergeCommitTree);
    walk.addTree(new DirCacheBuildIterator(builder));
    walk.addTree(workingTree);

    while (walk.next()) {
      processEntry(
          walk.getTree(0, CanonicalTreeParser.class),
          walk.getTree(1, DirCacheBuildIterator.class),
          walk.getTree(2, WorkingTreeIterator.class));
      if (walk.isSubtree()) walk.enterSubtree();
    }
    conflicts.removeAll(removed);
  }
Exemplo n.º 8
0
  /**
   * Scan head, index and merge tree. Used during normal checkout or merge operations.
   *
   * @throws CorruptObjectException
   * @throws IOException
   */
  public void preScanTwoTrees() throws CorruptObjectException, IOException {
    removed.clear();
    updated.clear();
    conflicts.clear();
    walk = new NameConflictTreeWalk(repo);
    builder = dc.builder();

    addTree(walk, headCommitTree);
    addTree(walk, mergeCommitTree);
    walk.addTree(new DirCacheBuildIterator(builder));
    walk.addTree(workingTree);

    while (walk.next()) {
      processEntry(
          walk.getTree(0, CanonicalTreeParser.class),
          walk.getTree(1, CanonicalTreeParser.class),
          walk.getTree(2, DirCacheBuildIterator.class),
          walk.getTree(3, WorkingTreeIterator.class));
      if (walk.isSubtree()) walk.enterSubtree();
    }
  }
Exemplo n.º 9
0
  /**
   * Updates the index after a content merge has happened. If no conflict has occurred this includes
   * persisting the merged content to the object database. In case of conflicts this method takes
   * care to write the correct stages to the index.
   *
   * @param base
   * @param ours
   * @param theirs
   * @param result
   * @throws FileNotFoundException
   * @throws IOException
   */
  private void updateIndex(
      CanonicalTreeParser base,
      CanonicalTreeParser ours,
      CanonicalTreeParser theirs,
      MergeResult<RawText> result)
      throws FileNotFoundException, IOException {
    File mergedFile = !inCore ? writeMergedFile(result) : null;
    if (result.containsConflicts()) {
      // A conflict occurred, the file will contain conflict markers
      // the index will be populated with the three stages and the
      // workdir (if used) contains the halfway merged content.
      add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
      add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
      add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
      mergeResults.put(tw.getPathString(), result);
      return;
    }

    // No conflict occurred, the file will contain fully merged content.
    // The index will be populated with the new merged version.
    DirCacheEntry dce = new DirCacheEntry(tw.getPathString());

    // Set the mode for the new content. Fall back to REGULAR_FILE if
    // we can't merge modes of OURS and THEIRS.
    int newMode = mergeFileModes(tw.getRawMode(0), tw.getRawMode(1), tw.getRawMode(2));
    dce.setFileMode(
        newMode == FileMode.MISSING.getBits() ? FileMode.REGULAR_FILE : FileMode.fromBits(newMode));
    if (mergedFile != null) {
      long len = mergedFile.length();
      dce.setLastModified(mergedFile.lastModified());
      dce.setLength((int) len);
      InputStream is = new FileInputStream(mergedFile);
      try {
        dce.setObjectId(getObjectInserter().insert(OBJ_BLOB, len, is));
      } finally {
        is.close();
      }
    } else dce.setObjectId(insertMergeResult(result));
    builder.add(dce);
  }
Exemplo n.º 10
0
  /**
   * Processes one path and tries to merge. This method will do all do all trivial (not content)
   * merges and will also detect if a merge will fail. The merge will fail when one of the following
   * is true
   *
   * <ul>
   *   <li>the index entry does not match the entry in ours. When merging one branch into the
   *       current HEAD, ours will point to HEAD and theirs will point to the other branch. It is
   *       assumed that the index matches the HEAD because it will only not match HEAD if it was
   *       populated before the merge operation. But the merge commit should not accidentally
   *       contain modifications done before the merge. Check the <a href=
   *       "http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html#_3_way_merge" >git
   *       read-tree</a> documentation for further explanations.
   *   <li>A conflict was detected and the working-tree file is dirty. When a conflict is detected
   *       the content-merge algorithm will try to write a merged version into the working-tree. If
   *       the file is dirty we would override unsaved data.
   * </ul>
   *
   * @param base the common base for ours and theirs
   * @param ours the ours side of the merge. When merging a branch into the HEAD ours will point to
   *     HEAD
   * @param theirs the theirs side of the merge. When merging a branch into the current HEAD theirs
   *     will point to the branch which is merged into HEAD.
   * @param index the index entry
   * @param work the file in the working tree
   * @param ignoreConflicts see {@link ResolveMerger#mergeTrees(AbstractTreeIterator, RevTree,
   *     RevTree, boolean)}
   * @return <code>false</code> if the merge will fail because the index entry didn't match ours or
   *     the working-dir file was dirty and a conflict occurred
   * @throws MissingObjectException
   * @throws IncorrectObjectTypeException
   * @throws CorruptObjectException
   * @throws IOException
   * @since 3.5
   */
  protected boolean processEntry(
      CanonicalTreeParser base,
      CanonicalTreeParser ours,
      CanonicalTreeParser theirs,
      DirCacheBuildIterator index,
      WorkingTreeIterator work,
      boolean ignoreConflicts)
      throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException,
          IOException {
    enterSubtree = true;
    final int modeO = tw.getRawMode(T_OURS);
    final int modeT = tw.getRawMode(T_THEIRS);
    final int modeB = tw.getRawMode(T_BASE);

    if (modeO == 0 && modeT == 0 && modeB == 0)
      // File is either untracked or new, staged but uncommitted
      return true;

    if (isIndexDirty()) return false;

    DirCacheEntry ourDce = null;

    if (index == null || index.getDirCacheEntry() == null) {
      // create a fake DCE, but only if ours is valid. ours is kept only
      // in case it is valid, so a null ourDce is ok in all other cases.
      if (nonTree(modeO)) {
        ourDce = new DirCacheEntry(tw.getRawPath());
        ourDce.setObjectId(tw.getObjectId(T_OURS));
        ourDce.setFileMode(tw.getFileMode(T_OURS));
      }
    } else {
      ourDce = index.getDirCacheEntry();
    }

    if (nonTree(modeO) && nonTree(modeT) && tw.idEqual(T_OURS, T_THEIRS)) {
      // OURS and THEIRS have equal content. Check the file mode
      if (modeO == modeT) {
        // content and mode of OURS and THEIRS are equal: it doesn't
        // matter which one we choose. OURS is chosen. Since the index
        // is clean (the index matches already OURS) we can keep the existing one
        keep(ourDce);
        // no checkout needed!
        return true;
      } else {
        // same content but different mode on OURS and THEIRS.
        // Try to merge the mode and report an error if this is
        // not possible.
        int newMode = mergeFileModes(modeB, modeO, modeT);
        if (newMode != FileMode.MISSING.getBits()) {
          if (newMode == modeO)
            // ours version is preferred
            keep(ourDce);
          else {
            // the preferred version THEIRS has a different mode
            // than ours. Check it out!
            if (isWorktreeDirty(work, ourDce)) return false;
            // we know about length and lastMod only after we have written the new content.
            // This will happen later. Set these values to 0 for know.
            DirCacheEntry e = add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_0, 0, 0);
            toBeCheckedOut.put(tw.getPathString(), e);
          }
          return true;
        } else {
          // FileModes are not mergeable. We found a conflict on modes.
          // For conflicting entries we don't know lastModified and length.
          add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
          add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
          add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
          unmergedPaths.add(tw.getPathString());
          mergeResults.put(
              tw.getPathString(), new MergeResult<RawText>(Collections.<RawText>emptyList()));
        }
        return true;
      }
    }

    if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS)) {
      // THEIRS was not changed compared to BASE. All changes must be in
      // OURS. OURS is chosen. We can keep the existing entry.
      if (ourDce != null) keep(ourDce);
      // no checkout needed!
      return true;
    }

    if (modeB == modeO && tw.idEqual(T_BASE, T_OURS)) {
      // OURS was not changed compared to BASE. All changes must be in
      // THEIRS. THEIRS is chosen.

      // Check worktree before checking out THEIRS
      if (isWorktreeDirty(work, ourDce)) return false;
      if (nonTree(modeT)) {
        // we know about length and lastMod only after we have written
        // the new content.
        // This will happen later. Set these values to 0 for know.
        DirCacheEntry e = add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_0, 0, 0);
        if (e != null) toBeCheckedOut.put(tw.getPathString(), e);
        return true;
      } else {
        // we want THEIRS ... but THEIRS contains a folder or the
        // deletion of the path. Delete what's in the workingtree (the
        // workingtree is clean) but do not complain if the file is
        // already deleted locally. This complements the test in
        // isWorktreeDirty() for the same case.
        if (tw.getTreeCount() > T_FILE && tw.getRawMode(T_FILE) == 0) return true;
        toBeDeleted.add(tw.getPathString());
        return true;
      }
    }

    if (tw.isSubtree()) {
      // file/folder conflicts: here I want to detect only file/folder
      // conflict between ours and theirs. file/folder conflicts between
      // base/index/workingTree and something else are not relevant or
      // detected later
      if (nonTree(modeO) && !nonTree(modeT)) {
        if (nonTree(modeB)) add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
        add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
        unmergedPaths.add(tw.getPathString());
        enterSubtree = false;
        return true;
      }
      if (nonTree(modeT) && !nonTree(modeO)) {
        if (nonTree(modeB)) add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
        add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
        unmergedPaths.add(tw.getPathString());
        enterSubtree = false;
        return true;
      }

      // ours and theirs are both folders or both files (and treewalk
      // tells us we are in a subtree because of index or working-dir).
      // If they are both folders no content-merge is required - we can
      // return here.
      if (!nonTree(modeO)) return true;

      // ours and theirs are both files, just fall out of the if block
      // and do the content merge
    }

    if (nonTree(modeO) && nonTree(modeT)) {
      // Check worktree before modifying files
      if (isWorktreeDirty(work, ourDce)) return false;

      // Don't attempt to resolve submodule link conflicts
      if (isGitLink(modeO) || isGitLink(modeT)) {
        add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
        add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
        add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
        unmergedPaths.add(tw.getPathString());
        return true;
      }

      MergeResult<RawText> result = contentMerge(base, ours, theirs);
      if (ignoreConflicts) result.setContainsConflicts(false);
      updateIndex(base, ours, theirs, result);
      if (result.containsConflicts() && !ignoreConflicts) unmergedPaths.add(tw.getPathString());
      modifiedFiles.add(tw.getPathString());
    } else if (modeO != modeT) {
      // OURS or THEIRS has been deleted
      if (((modeO != 0 && !tw.idEqual(T_BASE, T_OURS))
          || (modeT != 0 && !tw.idEqual(T_BASE, T_THEIRS)))) {

        add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
        add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
        DirCacheEntry e = add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);

        // OURS was deleted checkout THEIRS
        if (modeO == 0) {
          // Check worktree before checking out THEIRS
          if (isWorktreeDirty(work, ourDce)) return false;
          if (nonTree(modeT)) {
            if (e != null) toBeCheckedOut.put(tw.getPathString(), e);
          }
        }

        unmergedPaths.add(tw.getPathString());

        // generate a MergeResult for the deleted file
        mergeResults.put(tw.getPathString(), contentMerge(base, ours, theirs));
      }
    }
    return true;
  }
Exemplo n.º 11
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);
        }
      }
    }
  }
Exemplo n.º 12
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,
     }
   }
 }