3

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Kyrix
  • 61
  • 2
  • 9

1 Answers1

2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Are the rest of the commands alright? Am I initializing the git repository in the right way? Is there supposed to be a .git file somewhere? Also how do I check where the root of my local repository is? – Kyrix Aug 05 '19 at 06:14
  • @Kyrix from different local repositories to one remote repository hosted on GitHub. I missed that part: only one local repo can push to one remote repo. In your case, you would need to use the absolute path for the file you want to add. – VonC Aug 05 '19 at 06:25
  • Update: I copied the getPath method from the example and I used it like this https://pastebin.com/ZmV5kj0G thanks for your help im so frustrated – Kyrix Aug 05 '19 at 07:09
  • I have a question, Is every local repository asscoiated with some ID or something? Because when I make a local repository it commits to github (and I see the commit on github) but with no files. And when I delete the local repository and make another one with the same name no commits are made.. – Kyrix Aug 05 '19 at 07:15
  • @Kyrix A local repo is only linked to a remote one through its `git remote -v` URL: you need to have onnly one local repo, because the history of commits you will be making there will have to match and complete the same set of commit of the remote repo. – VonC Aug 05 '19 at 07:28