Esempio n. 1
0
  private void assertEntry(FileMode type, boolean entryIgnored, String pathName)
      throws IOException {
    assertTrue("walk has entry", walk.next());
    assertEquals(pathName, walk.getPathString());
    assertEquals(type, walk.getFileMode(0));

    WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
    assertNotNull("has tree", itr);
    assertEquals("is ignored", entryIgnored, itr.isEntryIgnored());
    if (D.equals(type)) walk.enterSubtree();
  }
  private void assertIteration(FileMode type, String pathName, List<Attribute> nodeAttrs)
      throws IOException {
    assertTrue("walk has entry", walk.next());
    assertEquals(pathName, walk.getPathString());
    assertEquals(type, walk.getFileMode(0));
    DirCacheIterator itr = walk.getTree(0, DirCacheIterator.class);
    assertNotNull("has tree", itr);

    AttributesNode attributesNode = itr.getEntryAttributesNode(db.newObjectReader());
    assertAttributesNode(pathName, attributesNode, nodeAttrs);

    if (D.equals(type)) walk.enterSubtree();
  }
Esempio n. 3
0
  private void formatHeader(ByteArrayOutputStream o, DiffEntry ent) throws IOException {
    final ChangeType type = ent.getChangeType();
    final String oldp = ent.getOldPath();
    final String newp = ent.getNewPath();
    final FileMode oldMode = ent.getOldMode();
    final FileMode newMode = ent.getNewMode();

    formatGitDiffFirstHeaderLine(o, type, oldp, newp);

    switch (type) {
      case ADD:
        o.write(encodeASCII("new file mode ")); // $NON-NLS-1$
        newMode.copyTo(o);
        o.write('\n');
        break;

      case DELETE:
        o.write(encodeASCII("deleted file mode ")); // $NON-NLS-1$
        oldMode.copyTo(o);
        o.write('\n');
        break;

      case RENAME:
        o.write(
            encodeASCII("similarity index " + ent.getScore() + "%")); // $NON-NLS-1$ //$NON-NLS-2$
        o.write('\n');

        o.write(encode("rename from " + quotePath(oldp))); // $NON-NLS-1$
        o.write('\n');

        o.write(encode("rename to " + quotePath(newp))); // $NON-NLS-1$
        o.write('\n');
        break;

      case COPY:
        o.write(
            encodeASCII("similarity index " + ent.getScore() + "%")); // $NON-NLS-1$ //$NON-NLS-2$
        o.write('\n');

        o.write(encode("copy from " + quotePath(oldp))); // $NON-NLS-1$
        o.write('\n');

        o.write(encode("copy to " + quotePath(newp))); // $NON-NLS-1$
        o.write('\n');

        if (!oldMode.equals(newMode)) {
          o.write(encodeASCII("new file mode ")); // $NON-NLS-1$
          newMode.copyTo(o);
          o.write('\n');
        }
        break;

      case MODIFY:
        if (0 < ent.getScore()) {
          o.write(
              encodeASCII(
                  "dissimilarity index " //$NON-NLS-1$
                      + (100 - ent.getScore())
                      + "%")); //$NON-NLS-1$
          o.write('\n');
        }
        break;
    }

    if ((type == MODIFY || type == RENAME) && !oldMode.equals(newMode)) {
      o.write(encodeASCII("old mode ")); // $NON-NLS-1$
      oldMode.copyTo(o);
      o.write('\n');

      o.write(encodeASCII("new mode ")); // $NON-NLS-1$
      newMode.copyTo(o);
      o.write('\n');
    }

    if (!ent.getOldId().equals(ent.getNewId())) {
      formatIndexLine(o, ent);
    }
  }
Esempio n. 4
0
 /**
  * Compares whether two pairs of ObjectId and FileMode are equal.
  *
  * @param id1
  * @param mode1
  * @param id2
  * @param mode2
  * @return <code>true</code> if FileModes and ObjectIds are equal. <code>false</code> otherwise
  */
 private boolean equalIdAndMode(ObjectId id1, FileMode mode1, ObjectId id2, FileMode mode2) {
   if (!mode1.equals(mode2)) return false;
   return id1 != null ? id1.equals(id2) : id2 == null;
 }
Esempio n. 5
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();
    }
  }