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

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

我能够使用 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 不是 org.eclipse.egit.github.core.client.GitHubClient.createException(GitHubClient.java:552) 的有效树 (422)在 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)

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

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

在这一行抛出错误:Tree newTree u003d dataService.createTree(repository, entries, baseTree.getSha());


Edit(VonC 回复): 您好 VonC,非常感谢您的回复。 JGit 看起来很有希望,但我看到了以下问题。

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

  2. 该示例还添加了一个简单的文件,但不是文件夹/目录。

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

    }

}

它抛出这个错误:

线程“主”java.io.IOException 中的异常:无法打开文件。原因:系统找不到指定的路径(路径 C:\Users\ramgood\AppData\Local\Temp\TestGitRepository441020326444846897\folder1\testfile1.txt,工作目录 C:\Users\ramgood\workspace-sapui5-1.32\accelerate)在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 时遇到错误。

从https://github.com/debsap/testrepo.git克隆到 C:\Users\ramgood\AppData\Local\Temp\REStGitRepository8321744366017013430 线程“主”org.eclipse.jgit.api.errors 中的异常。 TransportException:来源:未找到。在 org.eclipse.jgit.api.PushCommand.call(PushCommand.java:183) 在 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) 在 org.eclipse.jgit.transport.Transport.openAll(Transport.java:383) 在 org.eclipse.jgit.api.PushCommand.call(PushCommand.java:147) ...还有 1 个

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

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

Edit(Solution): 下面是工作程序。

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,而不是Egit-GitHub(尽管它基于 JGit)。

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

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

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

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

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

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

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

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

只需将遥控器添加到 GitHub 存储库,并推送。

两者都在 JGit 中可用(在 Java 中也是如此)。

Logo

ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!

更多推荐