Exemple #1
0
 public void branchesCheckout() {
   String branchName = "";
   Set<Entry<String, Ref>> refs = repository.getAllRefs().entrySet();
   for (Entry<String, Ref> ref : refs) {
     if (ref.getKey().startsWith(Constants.R_REMOTES) && !ref.getKey().contains(Constants.HEAD)) {
       branchName = ref.getValue().getName().split(Constants.R_REMOTES)[1];
       // TODO: replace with logging
       System.out.println(
           "Trying to checkout branch: "
               + branchName
               + " ("
               + branchName.split("origin/")[1]
               + ")");
       CheckoutCommand checkoutCommand = this.git.checkout();
       checkoutCommand.setForce(true);
       checkoutCommand.setCreateBranch(true);
       checkoutCommand.setUpstreamMode(SetupUpstreamMode.TRACK);
       checkoutCommand.setName(branchName.split("origin/")[1]);
       checkoutCommand.setStartPoint(branchName);
       try {
         checkoutCommand.call();
         // TODO: replace with logging
         // println "Successfully checked out branch " + branchName;
       } catch (RefAlreadyExistsException e) {
         // TODO: replace with logging
         // println "Skipping branch (already exists): " + branchName;
       } catch (RefNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       } catch (InvalidRefNameException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       } catch (GitAPIException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
  @Override
  @Port(name = "files", method = "saveFile")
  public boolean saveFile(String relativePath, byte[] data) {
    boolean result = true; // super.saveFile(relativePath, data);
    File f = new File(baseFolder.getAbsolutePath() + relativePath);
    if (f.exists()) {
      result = save(relativePath, data);

      /*String relativePathClean = relativePath;
      if (relativePath.startsWith("/")) {
      	relativePathClean = relativePath.substring(relativePath.indexOf("/") + 1);
      }*/
      commitRepository(
          " File " + relativePath + " saved ",
          " from site ",
          " [email protected] "); // TODO fix name and email

      try {

        // addFileToRepository(f);
        git.pull().call();

        /*} catch (Exception e) {
        	logger.error("error while unlock and commit git ", e);
        }*/
      } catch (DetachedHeadException e) {
        try {

          git.revert().call();
          save(relativePath + ".bak_" + new Date(), data);
          commitRepository(
              " File " + relativePath + " saved with conflict ",
              " name ",
              " email "); // TODO fix name and email
          UsernamePasswordCredentialsProvider user =
              new UsernamePasswordCredentialsProvider(
                  (String) this.getDictionary().get("login"),
                  (String) this.getDictionary().get("pass"));
          git.push().setCredentialsProvider(user).call();
          return false;

        } catch (GitAPIException e1) {
          e1.printStackTrace(); // To change body of catch statement use File | Settings | File
          // Templates.
        }

        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      } catch (NoHeadException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      } catch (TransportException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      } catch (InvalidConfigurationException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      } catch (InvalidRemoteException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      } catch (CanceledException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      } catch (WrongRepositoryStateException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      } catch (RefNotFoundException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      } catch (GitAPIException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      }
      try {
        UsernamePasswordCredentialsProvider user =
            new UsernamePasswordCredentialsProvider(
                (String) this.getDictionary().get("login"),
                (String) this.getDictionary().get("pass"));
        git.push().setCredentialsProvider(user).call();
        return true;
      } catch (GitAPIException e) {
        e.printStackTrace(); // To change body of catch statement use File | Settings | File
        // Templates.
      }
    }

    return true;
  }