gt:         try (GitConnection gitConnection = getGitConnection()) {
context: /*******************************************************************************
   * Copyright (c) 2012-2016 Codenvy, S.A.
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the Eclipse Public License v1.0
   * which accompanies this distribution, and is available at
   * http://www.eclipse.org/legal/epl-v10.html
   *
   * Contributors:
   *   Codenvy, S.A. - initial API and implementation
   *******************************************************************************/
  package org.eclipse.che.api.git;
  
  import org.eclipse.che.api.core.ApiException;
  import org.eclipse.che.api.git.shared.AddRequest;
  import org.eclipse.che.api.git.shared.Branch;
  import org.eclipse.che.api.git.shared.BranchCreateRequest;
  import org.eclipse.che.api.git.shared.BranchDeleteRequest;
  import org.eclipse.che.api.git.shared.BranchListRequest;
  import org.eclipse.che.api.git.shared.CheckoutRequest;
  import org.eclipse.che.api.git.shared.CloneRequest;
  import org.eclipse.che.api.git.shared.CommitRequest;
  import org.eclipse.che.api.git.shared.Commiters;
  import org.eclipse.che.api.git.shared.ConfigRequest;
  import org.eclipse.che.api.git.shared.DiffRequest;
  import org.eclipse.che.api.git.shared.FetchRequest;
  import org.eclipse.che.api.git.shared.InitRequest;
  import org.eclipse.che.api.git.shared.LogRequest;
  import org.eclipse.che.api.git.shared.MergeRequest;
  import org.eclipse.che.api.git.shared.MergeResult;
  import org.eclipse.che.api.git.shared.MoveRequest;
  import org.eclipse.che.api.git.shared.PullRequest;
  import org.eclipse.che.api.git.shared.PullResponse;
  import org.eclipse.che.api.git.shared.PushRequest;
  import org.eclipse.che.api.git.shared.PushResponse;
  import org.eclipse.che.api.git.shared.RebaseRequest;
  import org.eclipse.che.api.git.shared.RebaseResponse;
  import org.eclipse.che.api.git.shared.Remote;
  import org.eclipse.che.api.git.shared.RemoteAddRequest;
  import org.eclipse.che.api.git.shared.RemoteListRequest;
  import org.eclipse.che.api.git.shared.RemoteUpdateRequest;
  import org.eclipse.che.api.git.shared.RepoInfo;
  import org.eclipse.che.api.git.shared.ResetRequest;
  import org.eclipse.che.api.git.shared.Revision;
  import org.eclipse.che.api.git.shared.RmRequest;
  import org.eclipse.che.api.git.shared.ShowFileContentRequest;
  import org.eclipse.che.api.git.shared.ShowFileContentResponse;
  import org.eclipse.che.api.git.shared.Status;
  import org.eclipse.che.api.git.shared.StatusFormat;
  import org.eclipse.che.api.git.shared.Tag;
  import org.eclipse.che.api.git.shared.TagCreateRequest;
  import org.eclipse.che.api.git.shared.TagDeleteRequest;
  import org.eclipse.che.api.git.shared.TagListRequest;
  import org.eclipse.che.api.project.server.FolderEntry;
  import org.eclipse.che.api.project.server.ProjectRegistry;
  import org.eclipse.che.api.project.server.RegisteredProject;
  import org.eclipse.che.dto.server.DtoFactory;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import javax.inject.Inject;
  import javax.ws.rs.Consumes;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.QueryParam;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.GenericEntity;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.UriInfo;
  import java.net.URISyntaxException;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  /** @author andrew00x */
  @Path("git")
  public class GitService {
  
      private static final Logger LOG = LoggerFactory.getLogger(GitService.class);
  
      @Inject
      private GitConnectionFactory gitConnectionFactory;
  
      @Inject
      private ProjectRegistry projectRegistry;
  
      @QueryParam("projectPath")
      private String projectPath;
  
      @Path("add")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void add(AddRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              gitConnection.add(request);
          }
      }
  
      @Path("checkout")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void checkout(CheckoutRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              gitConnection.checkout(request);
          }
      }
  
      @Path("branch-create")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public Branch branchCreate(BranchCreateRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return gitConnection.branchCreate(request);
          }
      }
  
      @Path("branch-delete")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void branchDelete(BranchDeleteRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              gitConnection.branchDelete(request);
          }
      }
  
      @Path("branch-rename")
      @POST
      public void branchRename(@QueryParam("oldName") String oldName,
                               @QueryParam("newName") String newName) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              gitConnection.branchRename(oldName, newName);
          }
      }
  
      @Path("branch-list")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
      public GenericEntity<List<Branch>> branchList(BranchListRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return new GenericEntity<List<Branch>>(gitConnection.branchList(request)) {
              };
          }
      }
  
      @Path("clone")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public RepoInfo clone(final CloneRequest request) throws URISyntaxException, ApiException {
          long start = System.currentTimeMillis();
          // On-the-fly resolving of repository's working directory.
          request.setWorkingDir(getAbsoluteProjectPath(request.getWorkingDir()));
          LOG.info("Repository clone from '" + request.getRemoteUri() + "' to '" + request.getWorkingDir() + "' started");
          GitConnection gitConnection = getGitConnection();
          try {
              gitConnection.clone(request);
              return DtoFactory.getInstance().createDto(RepoInfo.class).withRemoteUri(request.getRemoteUri());
          } finally {
              long end = System.currentTimeMillis();
              long seconds = (end - start) / 1000;
              LOG.info("Repository clone from '" + request.getRemoteUri() + "' to '" + request.getWorkingDir()
                       + "' finished. Process took " + seconds + " seconds (" + seconds / 60 + " minutes)");
              gitConnection.close();
          }
      }
  
      @Path("commit")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
      public Revision commit(CommitRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return gitConnection.commit(request);
          }
      }
  
      @Path("diff")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.TEXT_PLAIN)
      public InfoPage diff(DiffRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return gitConnection.diff(request);
          }
      }
  
      /**
       * Show file content from specified revision or branch.
       *
       * @param request
       *         request that contains file name with its full path and revision or branch
       * @return response that contains content of the file
       * @throws ApiException
       *         when some error occurred while retrieving the content of the file
       */
      @Path("show")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
      public ShowFileContentResponse showFileContent(ShowFileContentRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return gitConnection.showFileContent(request);
          }
      }
  
      @Path("fetch")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void fetch(FetchRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              gitConnection.fetch(request);
          }
      }
  
      @Path("init")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void init(final InitRequest request) throws ApiException {
          request.setWorkingDir(getAbsoluteProjectPath(projectPath));
          try (GitConnection gitConnection = getGitConnection()) {
              gitConnection.init(request);
          }
          projectRegistry.setProjectType(projectPath, GitProjectType.TYPE_ID, true);
      }
  
      @Path("log")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
      public LogPage log(LogRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return gitConnection.log(request);
          }
      }
  
      @Path("merge")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
      public MergeResult merge(MergeRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return gitConnection.merge(request);
          }
      }
  
      @Path("rebase")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public RebaseResponse rebase(RebaseRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return gitConnection.rebase(request);
          }
      }
  
      @Path("mv")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public void mv(MoveRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              gitConnection.mv(request);
          }
      }
  
      @Path("pull")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public PullResponse pull(PullRequest request) throws ApiException {
          try (GitConnection gitConnection = getGitConnection()) {
              return gitConnection.pull(request);
          }
      }
  
      @Path("push")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public PushResponse push(PushRequest request) throws ApiException {
  