Pushing files to remote repository using jGit

社会主义新天地 提交于 2020-01-06 07:21:08

问题


I need to programattically push files using jGit from different local repositories to one remote repository hosted on Github. The code below works with no exceptions, but nothing is being changed in Github.

Every user defined 'group' in my program has its own folder under another folder called data. And every group folder contains a folder called repo, which contains an html and css file. When the time's right, I need to push these two files to github.

Here's the structure of the folders:

program (folder)
     [reciter.jar]
     [start.bat]
     data (folder)
          <group 1> (group folder)
               repo (group specific repository folder)
                    [resultsFile.html]
                    [styleFile.css]
          <another group> (group folder)
               ... (omitted)

Here's what I need to do: I want make one repository one Github. (done: https://github.com/Skultrix/reciter.git) When a group pushes their files to Github, I need them to be in this order:

github's root
     <group 1>
          resultsFile.html
          styleFile.css
     <another group>
          resultsFile.html
          styleFile.css

Meaning, if I want to access "another group"'s html file, the path would be another_group/results.html.

Here's what I have tried with jGit:

    public void load() {
        try {
            git = Git.init().setGitDir(group.getDataManager().getRepoDirectory()).call();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //load() is always before sendFile()
    public void sendFile() {
        LoadingModal.showModal(main, () -> {
            try {
                System.out.println("adding");
                git.add()
                        .addFilepattern("resultsFile.html")
                        .call();
                System.out.println("commiting");
                git.commit()
                        .setAll(true)
                        .setMessage("Update results")
                        .call();
                System.out.println("remote adding");
                git.remoteAdd()
                        .setName("origin")
                        .setUri(new URIish("https://github.com/Skultrix/reciter.git"))
                        .call();
                System.out.println("pushing");
                git.push()
                        .setRemote("https://github.com/Skultrix/reciter.git")
                        .setCredentialsProvider(
                                new UsernamePasswordCredentialsProvider("skultrix", <my password>)
                        )
                        .call();
                System.out.println("finish");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "Uploading results to site.", true).run();
    }

When the code above is executed (#sendFile) the console prints:

adding
commiting
remote adding
pushing
finish

But when I check github, nothing is changed, and not even empty commits are made. No exceptions or errors either.

Thanks for any assistance or guidance in advance.


回答1:


You might need to add the path of that file (not just its name), as in this example

adder.addFilepattern(getPath(fileVersion.getFile(), repository));

The path can be relative to the repository, so <group 1> in your case.



来源:https://stackoverflow.com/questions/57349928/pushing-files-to-remote-repository-using-jgit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!