使用 JGit 或 EGit - directory1\myfile.txt 将文件夹内的文件添加到 Github

Add a file inside a folder to Github using JGit or EGit - directory1\myfile.txt

*在这个问题的底部添加了最终的工作程序*

我能够使用 org.eclipse.egit.github.core java library (and code sample is referred from here: https://gist.github.com/Detelca/2337731 将文件添加到 GitHub)

但我无法提供像 "folder1\myfile.txt" 或 "folder1\folder2\myfile.txt"

这样的路径

我试图找到一个简单的例子来在文件夹下添加一个文件,但我并没有真正找到它。

有什么帮助可以举个例子吗?

下面是我正在使用的代码:(用户名、reponame 等配置详细信息在方法 addFileToGH() 中)

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.eclipse.egit.github.core.Blob;
import org.eclipse.egit.github.core.Commit;
import org.eclipse.egit.github.core.CommitUser;
import org.eclipse.egit.github.core.Reference;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.RepositoryCommit;
import org.eclipse.egit.github.core.Tree;
import org.eclipse.egit.github.core.TreeEntry;
import org.eclipse.egit.github.core.TypedResource;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.CommitService;
import org.eclipse.egit.github.core.service.DataService;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.eclipse.egit.github.core.service.UserService;

public class GHFileWriter {

    public static void main(String[] args) {
        try {
            new GHFileWriter().addFileToGH("myfile.txt", "some file content");
            //new GHFileWriter().addFolderToGH("folder1");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public boolean addFileToGH(String fileName, String fileContent) throws IOException{
        String userName = "myusername";
        String password = "mypassword";
        String repoName = "myrepository"; 
        String ownerName = "myusername";
        String email = "myemail@gmail.com";

        //Basic authentication
        GitHubClient client = new GitHubClient();
            client.setCredentials(userName, password);

        // create needed services
        RepositoryService repositoryService = new RepositoryService();
        CommitService commitService = new CommitService(client);
        DataService dataService = new DataService(client);

     // get some sha's from current state in git
        Repository repository =  repositoryService.getRepository(ownerName, repoName );
        String baseCommitSha = repositoryService.getBranches(repository).get(0).getCommit().getSha();
        RepositoryCommit baseCommit = commitService.getCommit(repository, baseCommitSha);
        String treeSha = baseCommit.getSha();

        // create new blob with data
        Blob blob = new Blob();
            blob.setContent( fileContent ).setEncoding(Blob.ENCODING_UTF8); //"[\"" + System.currentTimeMillis() + "\"]").setEncoding(Blob.ENCODING_UTF8);
        String blob_sha = dataService.createBlob(repository, blob);
        Tree baseTree = dataService.getTree(repository, treeSha);
Collection<TreeEntry> entries = new ArrayList<TreeEntry>();

    // create new tree entry
    TreeEntry treeEntry = new TreeEntry();
        treeEntry.setPath( fileName );
        treeEntry.setMode(TreeEntry.MODE_BLOB);
        treeEntry.setType(TreeEntry.TYPE_BLOB);
        treeEntry.setSha(blob_sha);
        treeEntry.setSize(blob.getContent().length());

   /* //Code for adding folder, not working
TreeEntry folderEntry = new TreeEntry();
        folderEntry.setPath( "folder1" );
        folderEntry.setMode(TreeEntry.MODE_DIRECTORY);
        folderEntry.setType(TreeEntry.TYPE_TREE);
        folderEntry.setSha(blob_sha); //one of the possible issues
        //folderEntry.setSize(blob.getContent().length());

        entries.add(folderEntry);

*/              
entries.add(treeEntry);

        Tree newTree = dataService.createTree(repository, entries, baseTree.getSha()); //Issue is popped-up here, error is thrown here

     // create commit
        Commit commit = new Commit();
            commit.setMessage("Test commit at " + new Date(System.currentTimeMillis()).toLocaleString());
            commit.setTree(newTree);



            //Due to an error with github api we have to to all this
            //TODO: Make this better (another function)
            UserService userService = new UserService(client);
            User user = userService.getUser();

            CommitUser author = new CommitUser();
            Calendar now = Calendar.getInstance();

                author.setName( userName );
                author.setEmail(email);
            //author.setEmail(userService.getEmails().get(0));
                author.setDate(now.getTime());
                commit.setAuthor(author);
                commit.setCommitter(author);


        List<Commit> listOfCommits = new ArrayList<Commit>();
            listOfCommits.add(new Commit().setSha(baseCommitSha));
        // listOfCommits.containsAll(base_commit.getParents());
            commit.setParents(listOfCommits);
        // commit.setSha(base_commit.getSha());
        Commit newCommit = dataService.createCommit(repository, commit);

     // create resource
        TypedResource commitResource = new TypedResource();
        commitResource.setSha(newCommit.getSha());
        commitResource.setType(TypedResource.TYPE_COMMIT);
        commitResource.setUrl(newCommit.getUrl());

        //System.out.println("Committed file URL: "+ newCommit.getUrl());
     // get master reference and update it
        Reference reference = dataService.getReference(repository, "heads/master");
            reference.setObject(commitResource);
            dataService.editReference(repository, reference, true);

            if( newCommit.getUrl() != null || !newCommit.getUrl().equalsIgnoreCase("") ){
                return true;
            }
        //push.setCommits(commits);

        return false;
    }
}

下面是我执行上面代码时的错误。

org.eclipse.egit.github.core.client.RequestException: tree.sha a3b93733a6dfd221c24e94d2ce52c25307910d73 is not a valid tree (422) at org.eclipse.egit.github.core.client.GitHubClient.createException(GitHubClient.java:552) at org.eclipse.egit.github.core.client.GitHubClient.sendJson(GitHubClient.java:643) at org.eclipse.egit.github.core.client.GitHubClient.post(GitHubClient.java:757) at org.eclipse.egit.github.core.service.DataService.createTree(DataService.java:203) at com.apps.ui5.accelerator.writers.GHFileWriter.addFileToGH(GHFileWriter.java:87) at com.apps.ui5.accelerator.writers.GHFileWriter.main(GHFileWriter.java:30)

编辑: 引起错误的代码已被注释。注释代码以“//添加文件夹的代码,不起作用”开头

这行代码可能存在问题:folderEntry.setSha(blob_sha);

此行抛出错误:Tree newTree = dataService.createTree(repository, entries, baseTree.getSha());


编辑(VonC 回复): 您好 VonC,非常感谢您的回复。 JGit 看起来很有前途,但我看到以下问题。

  1. 我运行这是云实例中的一项服务,我不知道我是否可以依赖食谱示例中引用的文件库。如果这在云中有效,我可以考虑将其作为解决方案。
  2. 该示例还添加了一个简单的文件,但不是 folder/directory。
  3. 我需要使用 java 程序而不是 git 命令将更改推送到 GitHub。我该怎么做?

你能参考任何创建文件夹的例子吗?我尝试了以下代码来添加文件夹,但没有成功!

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.dstadler.jgit.helper.CookbookHelper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.lib.Repository;


public class GHFolderWriter {

    public static void main(String[] args) throws IOException, NoFilepatternException, GitAPIException {
        // prepare a new test-repository
        try (Repository repository = CookbookHelper.createNewRepository()) {
            try (Git git = new Git(repository)) {
                //create folder
                File folder = new File(repository.getDirectory().getParent(), "folder1");
                if(!folder.createNewFile()) {
                    throw new IOException("Could not create folder " + folder);
                }

                // create the file
                File myfile = new File(folder, "testfile1.txt");
                if(!myfile.createNewFile()) {
                    throw new IOException("Could not create file " + myfile);
                }

                // Stage all files in the repo including new files
                git.add().addFilepattern(".").call();

                // and then commit the changes.
                git.commit()
                        .setMessage("Commit all changes including additions")
                        .call();

                try(PrintWriter writer = new PrintWriter(myfile)) {
                    writer.append("Hello, world!");
                }

                // Stage all changed files, omitting new files, and commit with one command
                git.commit()
                        .setAll(true)
                        .setMessage("Commit changes to all files")
                        .call();


                System.out.println("Committed all changes to repository at " + repository.getDirectory());
            }
        }

    }

}

它抛出了这个错误:

Exception in thread "main" java.io.IOException: Could not open file. Reason : The system cannot find the path specified (path C:\Users\ramgood\AppData\Local\Temp\TestGitRepository441020326444846897\folder1\testfile1.txt, working dir C:\Users\ramgood\workspace-sapui5-1.32\accelerate) at java.io.WinNTFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:1016) at com.apps.ui5.accelerator.writers.GHFolderWriter.main(GHFolderWriter.java:37)

Edit(3): 我按照你的建议搬到了 jgit 下面是我的新程序。我能够 1. 使用本地存储库克隆远程存储库 2. 将文件夹和文件添加到本地存储库并在本地提交,但 3.无法推送到 github.

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;



/**
 * Note: This snippet is not done and likely does not show anything useful yet
 *
 * @author dominik.stadler at gmx.at
 */
public class PushToRemoteRepository {

    private static final String REMOTE_URL = "https://github.com/debsap/testrepo.git";

    public static void main(String[] args) throws IOException, GitAPIException {
        // prepare a new folder for the cloned repository
        File localPath = File.createTempFile("REStGitRepository", "");
        if(!localPath.delete()) {
            throw new IOException("Could not delete temporary file " + localPath);
        }

        // then clone
        System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
        try (Git git = Git.cloneRepository()
                .setURI(REMOTE_URL)
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider("debsap", "testpasword1"))
                .setDirectory(localPath)
                .call()) {

            Repository repository = git.getRepository();
            // create the folder
            File theDir = new File(repository.getDirectory().getParent(), "dir1");
            theDir.mkdir();

         // create the file
            File myfile = new File(theDir, "testfile2.txt");
            if(!myfile.createNewFile()) {
                throw new IOException("Could not create file " + myfile);
            }

            // Stage all files in the repo including new files
            git.add().addFilepattern(".").call();

            // and then commit the changes.
            git.commit().setMessage("Commit all changes including additions").call();

            try(PrintWriter writer = new PrintWriter(myfile)) {
                writer.append("Hello, world!");
            }
            // Stage all changed files, omitting new files, and commit with one command
            git.commit()
                    .setAll(true)
                    .setMessage("Commit changes to all files")
                    .call();
         // now open the created repository
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            try (Repository repository1 = builder.setGitDir(localPath)
                    .readEnvironment() // scan environment GIT_* variables
                    .findGitDir() // scan up the file system tree
                    .build()) {

                try (Git git1 = new Git(repository1)) {
                    git1.push().call();
                }
                System.out.println("Pushed from repository: " + repository1.getDirectory() + " to remote repository at " + REMOTE_URL);
            }
        }
    }
}

我在尝试推送到远程仓库或 github 时遇到错误。

Cloning from https://github.com/debsap/testrepo.git to C:\Users\ramgood\AppData\Local\Temp\REStGitRepository8321744366017013430 Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: origin: not found. at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:183) at org.dstadler.jgit.unfinished.PushToRemoteRepository.main(PushToRemoteRepository.java:88) Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException: origin: not found. at org.eclipse.jgit.transport.TransportLocal.open(TransportLocal.java:131) at org.eclipse.jgit.transport.TransportBundleFile.open(TransportBundleFile.java:106) at org.eclipse.jgit.transport.Transport.open(Transport.java:565) at org.eclipse.jgit.transport.Transport.openAll(Transport.java:383) at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:147) ... 1 more

下面是 .git 文件夹中的配置文件。请注意,我不能手动编辑它,只能通过 java 程序更新。

[core] symlinks = false repositoryformatversion = 0 filemode = false logallrefupdates = true [remote "origin"] url = https://github.com/debsap/testrepo.git fetch = +refs/heads/:refs/remotes/origin/ [branch "master"] remote = origin merge = refs/heads/master

编辑(解决方案): 下面是工作程序。

public void pushToRemote(String xmlViewContent) throws IOException, InvalidRemoteException, TransportException, GitAPIException{
        // prepare a new folder for the cloned repository
        File localPath = File.createTempFile("GitRepository", "");
        if(!localPath.delete()) {
            throw new IOException("Could not delete temporary file " + localPath);
        }

        // then clone
        System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
        try (Git git = Git.cloneRepository()
                .setURI(REMOTE_URL)
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
                .setDirectory(localPath)
                .call()) {

            Repository repository = git.getRepository();
            // create the folder
            File theDir = new File(repository.getDirectory().getParent(), "webapp");
                theDir.mkdir();

         // create the file
            File myfile = new File(theDir, "InputView.view.xml");
                myfile.createNewFile();

            // Stage all files in the repo including new files
            git.add().addFilepattern(".").call();

            // and then commit the changes.
            git.commit().setMessage("Commit all changes including additions").call();

            try(PrintWriter writer = new PrintWriter(myfile)) {
                writer.append( xmlViewContent );
            }
            // Stage all changed files, omitting new files, and commit with one command
            git.commit()
                    .setAll(true)
                    .setMessage("Commit changes to all files")
                    .call();
            git.add().addFilepattern("*").call();
            RevCommit result = git.commit().setMessage("initial commit").call();
            git.push()
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
                .call();
            System.out.println("Pushed with commit: "+ result);

        }
    }

感谢 VonC 的回复。

要在 Git 存储库中操作文件,您最好使用 JGit directly, not Egit-GitHub(尽管它基于 JGit)。

这样,您可以从 centic9/jgit-cookbook, including a jgit/porcelain/CommitAll.java 示例中的示例中受益,使用 addFilepattern:

// Stage all files in the repo including new files
git.add().addFilepattern(".").call();

这样,您可以添加任何您需要的文件夹(内容)。

I am running this as a service in a cloud instance where I dont know if I can rely on filerepo which is refererred in the cookbook example. If this works in cloud, i can consider that as a solution.

您将依赖于您现在正在使用的同一个文件存储库。

The example is also adding a simple file but not a folder/directory.

只需用文件夹替换文件:它会起作用并添加文件夹 content.

And I need to push changes to GitHub using a java program, not git commands. How can I do that?

只需 adding a remote to the GitHub repo, and pushing.
两者都在 JGit 中可用(因此在 Java 中)。