Example #1
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);
        }
      }
    }
  }
Example #2
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,
     }
   }
 }