Example usage for org.eclipse.jgit.api Git add

List of usage examples for org.eclipse.jgit.api Git add

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git add.

Prototype

public AddCommand add() 

Source Link

Document

Return a command object to execute a Add command

Usage

From source file:org.eclipse.orion.server.filesystem.git.GitFileStore.java

License:Open Source License

private void commit(boolean dir) throws CoreException {
    try {/*w  ww . jav  a 2s.co m*/
        Repository local = getLocalRepo();
        Git git = new Git(local);
        String folderPattern = null;
        String filePattern = null;
        if (dir) {
            // add empty dir - http://stackoverflow.com/questions/115983/how-do-i-add-an-empty-directory-to-a-git-repository
            File folder = getLocalFile();
            File gitignoreFile = new File(folder.getPath() + "/" + Constants.DOT_GIT_IGNORE);
            // /<folder>/.gitignore
            gitignoreFile.createNewFile();
            String query = getUrl().getQuery();
            if (query.equals("/")) { // root
                filePattern = Constants.DOT_GIT_IGNORE;
            } else {
                folderPattern = new Path(query).toString().substring(1);
                // <folder>/
                filePattern = folderPattern + "/" + Constants.DOT_GIT_IGNORE;
                // <folder>/.gitignore
            }
        } else {
            // /<folder>/<file>
            IPath f = new Path(getUrl().getQuery()).removeLastSegments(1);
            // /<folder>/
            String s = f.toString().substring(1);
            // <folder>/
            folderPattern = s.equals("") ? null : s;
            // /<folder>/<file>
            s = getUrl().getQuery().substring(1);
            filePattern = s;
        }

        // TODO: folder may already exist, no need to add it again
        AddCommand add = git.add();
        if (folderPattern != null) {
            add.addFilepattern(folderPattern);
        }
        add.addFilepattern(filePattern);
        add.call();

        CommitCommand commit = git.commit();
        commit.setMessage("auto-commit of " + this);
        commit.call();
        LogHelper.log(new Status(IStatus.INFO, Activator.PI_GIT, 1, "Auto-commit of " + this + " done.", null));
    } catch (Exception e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PI_GIT, IStatus.ERROR, e.getMessage(), e));
    }
}

From source file:org.eclipse.orion.server.git.GitFileDecorator.java

License:Open Source License

/**
 * If this  server is configured to use git by default, then each project creation that is not
 * already in a repository needs to have a git -init performed to initialize the repository.
 *//*from  w ww.  j av a 2s. co  m*/
private void initGitRepository(HttpServletRequest request, IPath targetPath, JSONObject representation) {
    //project creation URL is of the form POST /workspace/<id>
    if (!(targetPath.segmentCount() == 2 && "workspace".equals(targetPath.segment(0)) //$NON-NLS-1$
            && Method.POST.equals(Method.fromString(request.getMethod()))))
        return;
    String scm = PreferenceHelper.getString(ServerConstants.CONFIG_FILE_DEFAULT_SCM, "").toLowerCase(); //$NON-NLS-1$
    if (!"git".equals(scm)) //$NON-NLS-1$
        return;
    try {
        IFileStore store = WebProject.fromId(representation.optString(ProtocolConstants.KEY_ID))
                .getProjectStore();
        //create repository in each project if it doesn't already exist
        File localFile = store.toLocalFile(EFS.NONE, null);
        File gitDir = GitUtils.getGitDir(localFile);
        if (gitDir == null) {
            gitDir = new File(localFile, Constants.DOT_GIT);
            FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
            repo.create();
            //we need to perform an initial commit to workaround JGit bug 339610.
            Git git = new Git(repo);
            git.add().addFilepattern(".").call(); //$NON-NLS-1$
            git.commit().setMessage("Initial commit").call();
        }
    } catch (Exception e) {
        //just log it - this is not the purpose of the file decorator
        LogHelper.log(e);
    }
}

From source file:org.eclipse.orion.server.git.servlets.GitIndexHandlerV1.java

License:Open Source License

private boolean handlePut(HttpServletRequest request, HttpServletResponse response, Repository db,
        String pattern) throws ServletException, NoFilepatternException {
    Git git = new Git(db);
    AddCommand add = git.add().addFilepattern(pattern);
    // "git add {pattern}"
    add.call();//from w w  w.  j a v a  2s  .c om
    // TODO: we're calling "add" twice, this is inefficient, see bug 349299
    // "git add -u {pattern}"
    git.add().setUpdate(true).addFilepattern(pattern).call();
    return true;
}

From source file:org.eclipse.orion.server.git.servlets.GitSubmoduleHandlerV1.java

License:Open Source License

public static void removeSubmodule(Repository db, Repository parentRepo, String pathToSubmodule)
        throws Exception {
    pathToSubmodule = pathToSubmodule.replace("\\", "/");
    StoredConfig gitSubmodulesConfig = getGitSubmodulesConfig(parentRepo);
    gitSubmodulesConfig.unsetSection(CONFIG_SUBMODULE_SECTION, pathToSubmodule);
    gitSubmodulesConfig.save();//from  ww  w.  j a  v  a 2 s . c  o  m
    StoredConfig repositoryConfig = parentRepo.getConfig();
    repositoryConfig.unsetSection(CONFIG_SUBMODULE_SECTION, pathToSubmodule);
    repositoryConfig.save();
    Git git = Git.wrap(parentRepo);
    git.add().addFilepattern(DOT_GIT_MODULES).call();
    RmCommand rm = git.rm().addFilepattern(pathToSubmodule);
    if (gitSubmodulesConfig.getSections().size() == 0) {
        rm.addFilepattern(DOT_GIT_MODULES);
    }
    rm.call();
    FileUtils.delete(db.getWorkTree(), FileUtils.RECURSIVE);
    FileUtils.delete(db.getDirectory(), FileUtils.RECURSIVE);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitTest.java

License:Open Source License

protected void createRepository() throws IOException, GitAPIException, CoreException {
    IPath randomLocation = getRandomLocation();
    gitDir = randomLocation.toFile();/*  ww  w . ja  v a 2 s . co m*/
    randomLocation = randomLocation.addTrailingSeparator().append(Constants.DOT_GIT);
    File dotGitDir = randomLocation.toFile().getCanonicalFile();
    db = new FileRepository(dotGitDir);
    assertFalse(dotGitDir.exists());
    db.create(false /* non bare */);

    testFile = new File(gitDir, "test.txt");
    testFile.createNewFile();
    createFile(testFile.toURI(), "test");
    File folder = new File(gitDir, "folder");
    folder.mkdir();
    File folderFile = new File(folder, "folder.txt");
    folderFile.createNewFile();
    createFile(folderFile.toURI(), "folder");

    Git git = new Git(db);
    git.add().addFilepattern(".").call();
    git.commit().setMessage("Initial commit").call();
}

From source file:org.eclipse.recommenders.internal.snipmatch.GitSnippetRepositoryTest.java

License:Open Source License

private void addFileToRemote(String filename, File remote, Git git) throws Exception {
    File file = new File(remote, filename);
    file.createNewFile();//w w w.j a v a 2 s. com
    git.add().addFilepattern(filename).call();
    git.commit().setMessage("commit message").call();
}

From source file:org.eclipse.releng.tests.GitCopyrightAdapterTest.java

License:Open Source License

public void testLastModifiedYear() throws Exception {
    final Git git = new Git(db);
    git.add().addFilepattern(PROJECT_NAME + "/" + FILE1_NAME).call();
    final PersonIdent committer2011 = new PersonIdent(committer, getDateForYear(2011));
    git.commit().setMessage("old commit").setCommitter(committer2011).call();
    git.add().addFilepattern(PROJECT_NAME + "/" + FILE2_NAME).call();
    git.commit().setMessage("new commit").call();

    final GitCopyrightAdapter adapter = new GitCopyrightAdapter(new IResource[] { project });
    adapter.initialize(NULL_MONITOR);//  w  w  w . j  av  a2 s  . c  o  m
    final int lastModifiedYear = adapter.getLastModifiedYear(file1, NULL_MONITOR);

    Assert.assertEquals(2011, lastModifiedYear);
}

From source file:org.eclipse.releng.tests.GitCopyrightAdapterTest.java

License:Open Source License

public void testCopyrightUpdateComment() throws Exception {
    final Git git = new Git(db);
    git.add().addFilepattern(PROJECT_NAME + "/" + FILE1_NAME).call();
    git.commit().setMessage("copyright update").call();

    final GitCopyrightAdapter adapter = new GitCopyrightAdapter(new IResource[] { project });
    adapter.initialize(NULL_MONITOR);//w  ww . j  av a  2s .  c o m
    final int lastModifiedYear = adapter.getLastModifiedYear(file1, NULL_MONITOR);

    Assert.assertEquals(0, lastModifiedYear);
}

From source file:org.exist.git.xquery.Add.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {//w w  w .ja  va 2 s . c  o m
        String localPath = args[0].getStringValue();
        if (!(localPath.endsWith("/")))
            localPath += File.separator;

        Git git = Git.open(new Resource(localPath), FS);

        AddCommand command = git.add();

        for (int i = 0; i < args[1].getItemCount(); i++) {
            command.addFilepattern(args[1].itemAt(i).getStringValue());
        }

        command.call();

        return BooleanValue.TRUE;
    } catch (Throwable e) {
        Throwable cause = e.getCause();
        if (cause != null) {
            throw new XPathException(this, Module.EXGIT001, cause.getMessage());
        }

        throw new XPathException(this, Module.EXGIT001, e);
    }
}

From source file:org.fusesource.fabric.git.internal.GitDataStore.java

License:Apache License

/**
 * Recursively copies the given files from the given directory to the specified directory
 * adding them to the git repo along the way
 *//*from   w  w w  . j  av a  2s.  c o m*/
protected void recursiveCopyAndAdd(Git git, File from, File toDir, String path, boolean useToDirAsDestination)
        throws GitAPIException, IOException {
    assertValid();
    String name = from.getName();
    String pattern = path + (path.length() > 0 ? "/" : "") + name;
    File toFile = new File(toDir, name);

    if (from.isDirectory()) {
        if (useToDirAsDestination) {
            toFile = toDir;
        }
        toFile.mkdirs();
        File[] files = from.listFiles();
        if (files != null) {
            for (File file : files) {
                recursiveCopyAndAdd(git, file, toFile, pattern, false);
            }
        }
    } else {
        Files.copy(from, toFile);
    }
    git.add().addFilepattern(pattern).call();
}