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

时间:2017-09-17 14:38:14

标签: java github egit jgit

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

我能够使用 org.eclipse.egit.github.core java库将文件添加到GitHub(代码示例来自此处:https://gist.github.com/Detelca/2337731

但是我无法提供类似" 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不是有效树(422)at   org.eclipse.egit.github.core.client.GitHubClient.createException(GitHubClient.java:552)     在   org.eclipse.egit.github.core.client.GitHubClient.sendJson(GitHubClient.java:643)     在   org.eclipse.egit.github.core.client.GitHubClient.post(GitHubClient.java:757)     在   org.eclipse.egit.github.core.service.DataService.createTree(DataService.java:203)     在   com.apps.ui5.accelerator.writers.GHFileWriter.addFileToGH(GHFileWriter.java:87)     在   com.apps.ui5.accelerator.writers.GHFileWriter.main(GHFileWriter.java:30)

编辑:注释错误导致代码。注释的代码以&#34; //代码开头添加文件夹,不工作&#34;

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

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

编辑(VonC响应): 您好VonC,非常感谢您的回复。 JGit看起来很有希望,但我看到下面的问题。

  1. 我在云实例中将其作为服务运行,我不知道我是否可以依赖于在cookbook示例中引用的filerepo。如果这在云中有效,我可以将其视为解决方案。
  2. 该示例还添加了一个简单文件,但没有添加文件夹/目录。
  3. 我需要使用java程序而不是git命令将更改推送到GitHub。我怎么能这样做?
  4. 您能参考创建文件夹的任何示例吗?我尝试下面的代码添加文件夹,但没有用!

    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());
                }
            }
    
        }
    
    }
    

    抛出这个错误:

      

    线程中的异常&#34; main&#34; java.io.IOException:无法打开文件。   原因:系统找不到指定的路径(路径   C:\ Users \用户ramgood \应用程序数据\本地的\ Temp \ TestGitRepository441020326444846897 \文件夹1 \ testfile1.txt,   工作目录C:\ Users \ ramgood \ workspace-sapui5-1.32 \ accelerated)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)

    编辑(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时遇到错误。

      

    https://github.com/debsap/testrepo.git克隆到   C:\ Users \用户ramgood \应用程序数据\本地的\ Temp \ REStGitRepository8321744366017013430   线程&#34; main&#34;中的例外情况   org.eclipse.jgit.api.errors.TransportException:origin:not found。在   org.eclipse.jgit.api.PushCommand.call(PushCommand.java:183)at   org.dstadler.jgit.unfinished.PushToRemoteRepository.main(PushToRemoteRepository.java:88)   引起:org.eclipse.jgit.errors.NoRemoteRepositoryException:   来源:未找到。在   org.eclipse.jgit.transport.TransportLocal $ 1.open(TransportLocal.java:131)     在   org.eclipse.jgit.transport.TransportBundleFile $ 1.open(TransportBundleFile.java:106)     在org.eclipse.jgit.transport.Transport.open(Transport.java:565)at   org.eclipse.jgit.transport.Transport.openAll(Transport.java:383)at at   org.eclipse.jgit.api.PushCommand.call(PushCommand.java:147)... 1   更

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

      

    [core] symlinks = false repositoryformatversion = 0 filemode =   false logallrefupdates = true [remote&#34; origin&#34;] url =   https://github.com/debsap/testrepo.git fetch =   + refs / heads / :refs / remotes / origin / [branch&#34; master&#34;] 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的回复。

1 个答案:

答案 0 :(得分:3)

对于操作Git仓库中的文件,最好使用JGit directly,而不是Egit-GitHub(尽管它基于JGit)。

这样,您可以从centic9/jgit-cookbook中的示例中受益,包括使用addFilepattern的{​​{3}}示例:

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

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

  

我在云实例中将其作为服务运行,我不知道我是否可以依赖于在cookbook示例中引用的filerepo。如果这在云中有效,我可以将其视为一种解决方案。

您将依赖于您现在使用的相同文件仓库。

  

该示例还添加了一个简单文件,但没有添加文件夹/目录。

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

  

我需要使用java程序而不是git命令将更改推送到GitHub。我怎么能这样做?

只需jgit/porcelain/CommitAll.javaadding a remote to the GitHub repo 两者都可以在JGit中使用(所以在Java中)。