0

I'm trying to perform a rename of a file from say "XXX.txt" to "YYY.txt" residing in a remote GIT repository.

I used the following code snippets using JGit API:

File file = new File( repository.getWorkTree(), "XXX.txt" );
File renameFile = new File( repository.getWorkTree(), "YYY.txt" );
file.renameTo(renameFile);

git.add().addFilepattern("YYY.txt").call();
git.rm().addFilepattern("XXX.txt").call();

CommitCommand commit = git.commit();
commit.setCommitter("Raguram", "raguramm@amius.co.in").setMessage("push command");
commit.call();

// Code for Pushing the commit to a remote repo

The above code doesn't work and still my index file is not getting updated with the new name [YYY.txt] rather it still contains the old name [XXX.txt].

I referred the link How do I rename a file in JGit which states a similar thing, but coudn't able to rename a file.

Anything I'm missing? Suggestions will be appreciated.

Community
  • 1
  • 1
Ragu
  • 187
  • 1
  • 11
  • Are you sure that the file is actually renamed? Does `renameTo()` return true? Instead of new `File( "XXX.txt" )` you probably need to specify `new FIle( repository.getWorkTree(), "XXX.txt" )` and the same for `YYY.txt` – Rüdiger Herrmann Feb 02 '16 at 09:06
  • @RüdigerHerrmann : Yes, rename returns true and I am able to see the file getting renamed when I try to debug my application. but AddCommand and RmCommand is not getting effect. What you said is correct `new FIle( repository.getWorkTree(), "XXX.txt" )` and I mean the same. I updated my question to reflect your point. – Ragu Feb 02 '16 at 09:33
  • How do you know that _my index file is not getting updated_? – Rüdiger Herrmann Feb 02 '16 at 10:21
  • _index_ file present under **.git folder** still contains the old file name [XXX.txt] and not the new one after executing the AddCommand and RmCommand commands. – Ragu Feb 02 '16 at 10:55
  • Try to use the `StatusCommand`, `getAdded()` should contain the new file name, `getMissing()` should contain the old name – Rüdiger Herrmann Feb 02 '16 at 11:08
  • To clarify the above noted: I meant, _in order to diagnose the problem_, use the `StatusCommand`... – Rüdiger Herrmann Feb 03 '16 at 07:27
  • Can you illustrate some example with `StatusCommand` ?? – Ragu Feb 03 '16 at 10:19
  • Inspect or print out what `git.status().getAdded()` and git.status().getMissing()` returns and amend your question with the results. – Rüdiger Herrmann Feb 03 '16 at 10:59
  • @RüdigerHerrmann : I found the root cause behind the issue, The file seperator is actually causing the problem. In my case it was like '\' and in reality it should be '/'. Am I correct ? . This is the reason why the git index file was not getting updated. – Ragu Feb 03 '16 at 13:51
  • Right, paths given to JGit like in `addFilepattern()` must always use forward-slashes (`/`) regardless of what platform you are running. – Rüdiger Herrmann Feb 03 '16 at 14:32
  • I've updated the answer to question http://stackoverflow.com/questions/20502431/how-do-i-rename-a-file-in-jgit accordingly and suggest to close this as duplicate. – Rüdiger Herrmann Feb 03 '16 at 14:35
  • @RüdigerHerrmann : Thanks a lot for your help... ! This should be marked as a important note, when trying to do any operations in JGit. As developers of windows tend to use the File seperator as `'\'`and they might not be aware of this path segment [ `'/'` ] convention that is to be followed . – Ragu Feb 04 '16 at 07:00
  • @RüdigerHerrmann : I'm closing this issue, Really Thanks to you for helping me...! – Ragu Feb 04 '16 at 07:02
  • BTW, the [JavaDoc for the respective commands](http://download.eclipse.org/jgit/site/3.5.0.201409260305-r/apidocs/org/eclipse/jgit/api/AddCommand.html#addFilepattern(java.lang.String)) clearly states that only slashes may be used. – Rüdiger Herrmann Feb 04 '16 at 08:41
  • @RüdigerHerrmann, Oh ok.. I'm noticing this now only :) Coming to the `AddCommand`, Is it possible to create a empty directory (with no files under it) in GIT repository ? I referred the link (http://stackoverflow.com/questions/115983/how-can-i-add-an-empty-directory-to-a-git-repository) which states that it is not possible – Ragu Feb 04 '16 at 09:05

0 Answers0