How can I use JGit to add deleted files to the index? - jgit

I want to add to the index all the modifications made in a folder:
files modified
files added
files removed
File gitDir = new File("/home/franck/Repositories/Git/Sandbox/.git");
try (Repository repo = new RepositoryBuilder().setGitDir(gitDir).build()){
try (Git git = new Git(repo)){
final Status status = git.status().addPath("testGit").call();
final AddCommand addCommand = git.add();
Set<String> removed = status.getRemoved();
for (final String s : removed) {
System.out.print("removed: "+s);
addCommand.addFilepattern(s);
}
Set<String> missing = status.getMissing();
for (final String s : missing) {
System.out.print("Missing: "+s);
addCommand.addFilepattern(s);
}
addCommand.call();
}
}
Output of this command:
Missing: testGit/test.txt
And output of git status:
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: testGit/test.txt
no changes added to commit (use "git add" and/or "git commit -a")
Deleted file is not added to the index. How can I achieve this?
I checked testAddRemovedCommittedFile, it seems adding a deletion makes nothing.

To add a deleted file to the index so that it will be deleted when committing you need to use the RmCommand.
Just like you would do with native Git
git rm test.txt
you can do in JGit
git.rm().addFilepattern("test.txt").call();
This will add test.txt to the index and mark it as to be deleted. The next revision that is committed will not contain test.txt any more.
If the file(s) specified by addFilepattern() aren't yet deleted from the work directory, the RmCommand will delete them. This is the default behavior. Calling setCached(true) before executing the command, will leave the work directory unaffected.

Related

with rstudio and github, issue with renamed repo

I had a repository named tags, I renamed it to tag.
I then created a new repository named tags (the old name of the first one).
Now when commiting from R Studio both projects try to commit to the same repository (tags).
I initiated my projects with :
shell("git remote add origin https://github.com/moodymudskipper/tag.git", intern = TRUE)
shell("git push -u origin master",intern = TRUE)
and
shell("git remote add origin https://github.com/moodymudskipper/tags.git",intern = TRUE)
shell("git push -u origin master",intern = TRUE)
And after this I only committed through Rstudio's API and usethis functions, I don't know much more than that about git.
Links to the packages :
https://github.com/moodymudskipper/tag
https://github.com/moodymudskipper/tags
How can I sort this out ?
I'm hesitant to throw this out as an answer, but: you can manually edit the ./.git/config file to update the [remote ...] section to change the remote URL. I have done this confidently enough with an empty repo ...
Check for presence of the tag with grep -rli tags.git .git/*; if all you get is .git/config, then you're good to edit and move on. If you find other files, though, I don't know for certain that they will be updated as you continue with your git remote work. In that case, it might be helpful to look at https://help.github.com/en/articles/changing-a-remotes-url in order to formally change the URL.

SBT: Add dependencies to project at runtime

There is sbt project declaration
lazy val myProject = (Project("myProject", file("someRoot"))
enablePlugins ...
settings (...)
It has taskKey that extracts some dependencies to file system.
My problem is that for the moment of loading SBT I can't determine all the dependencies, it could be done only after private Command Alias is executed
addCommandAlias("resolveDependencies", "; resolveDependenciesTask; TODO: update myProject dependencies and reload it")
Is there anyway to do that?
Actually, disregard my comment on your question. You can use a command to modify the state of the build, so after you run it, the changes it made stay.
Something along these lines:
// in your build.sbt
commands += Command.command("yourCustomCommand")(state =>
Project.extract(state).append(
Seq(libraryDependencies += // settings you want to modify
"com.lihaoyi" % "ammonite-repl" % "0.5.7" cross CrossVersion.full),
state))
Then call it with sbt yourCustomCommand.
The state instance you return from the command becomes the new state of the build, i.e. if you've added some dependencies, the build will see them.

Listing files under a specific folder from remote repository with JGit

I'm trying to list files from HEAD from a remote repository (Github). I read examples from the JGit documentation, but most of the time these are referencing to a local repository.
The only piece of code I found about remote repository is:
Collection<Ref> refs = Git.lsRemoteRepository()
.setHeads(true)
.setTags(true)
.setRemote("https://github/example/example.git")
.call();
for (Ref ref : refs) {
System.out.println("Ref: " + ref);
}
But this code is just listing references, like HEAD. Could anyone help listing files from a subfolder inside my remote repository?
LsRemoteCommand is the counterpart of git ls-remote and only lists references of a remote repository. In order to list files contained in a repository, you have to first clone the repository .
For example:
Git git = Git.cloneRepository()
.setURI( "https://github.com/eclipse/jgit.git" )
.setDirectory( "/path/to/repo" )
.call();
See this link for more on cloning repositories with JGit: http://www.codeaffine.com/2015/11/30/jgit-clone-repository/

Update solr config files in solrcloud

Hi fellow SOLR developers,
https://cwiki.apache.org/confluence/display/solr/Using+ZooKeeper+to+Manage+Configuration+Files This link says the below
To update or change your SolrCloud configuration files:
1. Download the latest configuration files from ZooKeeper, using the
source control checkout process.
2. Make your changes. Commit your changed file to source control.
3. Push the changes back to ZooKeeper.
4. Reload the collection so that the changes will be in effect.
But I was wondering if there are some examples or articles somewhere which can help me do this. For instance, how would one download the latest config files from zookeeper? Or should I know Zoopkeeper in-and-out to do this?
Version 5.3.1
I updated the synonym file in the "C:\12345\solrcloud\solr-5.3.1\example\example-DIH\solr\db\conf" folder and I'm using the below command
zkcli -zkhost localhost:2181,localhost:2182,localhost:2183 -cmd upconfig -confname db_config -confdir "C:\12345\solrcloud\solr-5.3.1\example\example-DIH\solr\db\conf"
But the file doesn't seem to change nor the query seem to work. I reloaded the core which did not help too. I even tried restarting my solrcloud.
My Brother, you are using upconfig command....
Same way there is a command called downconfig with same parameters.
So your order of execution will be :
Downconfig
Change the config
Upconfig
Downconfig command :
bin/solr zk downconfig -z 111.222.333.444:2181 -n mynewconfig -d /path/to/configset
Upconfig command :
bin/solr zk upconfig -z 111.222.333.444:2181 -n mynewconfig -d /path/to/configset
I think this short code can help to download the configs of solrcloud from zookeeper.
public void exportSynonyms(...) {
try {
final CloudSolrClient cloudSolrClient = ... //Retrieve the Solr client
final String configName = cloudSolrClient.getDefaultCollection();
final Path configPath = ... //Get or create temporary local file path
cloudSolrClient.downloadConfig(configName, configPath);
// Do something
cloudSolrClient.uploadConfig(configPath, configName);
// Reload the collection
} catch (RestClientException | SolrServiceException | IOException var11){
this.handleException(var11);
}
}
And I met the same problem and asked it on
https://cwiki.apache.org/confluence/display/solr/Using+ZooKeeper+to+Manage+Configuration+Files?focusedCommentId=65147407#comment-65147407
Hope it can help you.
After you update the file in Zookeeper, you need to tell solr to reload the collection, this tells solr to get the new configuration from Zookeeper.
See the Reload Solr Collection REST call:
https://cwiki.apache.org/confluence/display/solr/Collections+API#CollectionsAPI-api2

gradle - copying directory - Issue

I have an issue with copying files in gradle. Here is a snippet of build.gradle:
task mydir() {
new File("build/models").mkdir()
}
task copyTask(dependsOn:mydir, type: Copy){
from 'src/test/groovy/models'
into 'build/models'
from 'src/test/groovy/test.groovy'
into 'build/'
}
The issue here is in the first copy, that is, test.groovy is copied into build. But the first copy, where I am copying all the files in src/../models into build/models is not working. It simply copies the files into build directory only. My build/models remain empty. Can someone please tell me where what I'm doing wrong? I also have a separate task to create build/models directory prior to copyTask execution.
The build script contains the following mistakes:
The directory is created when mydir is configured, not when it is executed. (This means the directory will be created on every build invocation, regardless of which tasks get executed.)
new File('some/path') creates a path relative to the current working directory of the Gradle process, not a path relative to the project directory. Always use project.file() instead.
A Copy task can only have a single top-level into.
Here is a corrected version:
task mydir {
doLast {
mkdir('build/models')
}
}
task copyTask(dependsOn: mydir, type: Copy) {
into 'build'
from 'src/test/groovy/test.groovy'
into('models') {
from 'src/test/groovy/models'
}
}
The Copy task will create build/models automatically, so there is likely no need to have the mydir task. It's odd to use build as the target directory of a Copy task (should use a subdirectory).
In gradle, the copy task could only have single 'into' target with multiple froms.
The second 'into' override the first 'into' value and that's why it's only copied into 'build/' not 'build/models''
You may want to have a separate copy task or use ant.copy instead.

Resources