Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Clear working directory before checkout in eGit
Clear working directory before checkout in eGit [message #1731808] Tue, 10 May 2016 09:57 Go to next message
David Williams is currently offline David WilliamsFriend
Messages: 4
Registered: May 2016
Junior Member
Hello

What is the easiest way of automatically clearing the working directory from within eGit?

I believe the command line equivalent is "checkout -f" Is there a checkbox / radio button within the eGit interface that I need to set to achieve a clearout?

Thank you in anticipation of your help and advice

kind regards
David
Re: Clear working directory before checkout in eGit [message #1731926 is a reply to message #1731808] Wed, 11 May 2016 06:50 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 263
Registered: July 2009
Senior Member
What do you mean with "clearout"? The functionality of "git clean ..." (remove untracked files". Or do you really want a completely empty working tree where even those files which are under version-control are deleted?

Ciao
Chris
Re: Clear working directory before checkout in eGit [message #1731948 is a reply to message #1731926] Wed, 11 May 2016 08:52 Go to previous messageGo to next message
David Williams is currently offline David WilliamsFriend
Messages: 4
Registered: May 2016
Junior Member
Hi

The latter please - a completely empty working tree where even those files under VC are deleted too.

Re: Clear working directory before checkout in eGit [message #1732051 is a reply to message #1731948] Thu, 12 May 2016 08:48 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 263
Registered: July 2009
Senior Member
Why not just use standard java to delete the files? Make sure not to delete the .git folder. Maybe this code could work as a template:

public class DeleteWorktree {
	public static void main(final String[] args) throws IOException {
		final Repository repo = new RepositoryBuilder().setWorkTree(new File(args[0])).setMustExist(true).build();
		Files.walkFileTree(repo.getWorkTree().toPath(), new SimpleFileVisitor<Path>() {
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
				Files.delete(file);
				return FileVisitResult.CONTINUE;
			}
			public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
				return dir.equals(repo.getDirectory().toPath()) ? FileVisitResult.SKIP_SUBTREE
						: FileVisitResult.CONTINUE;
			}
			public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
				if (!dir.equals(repo.getWorkTree().toPath()))
					Files.delete(dir);
				return FileVisitResult.CONTINUE;
			}
		});
	}
}


Ciao
Chris
Re: Clear working directory before checkout in eGit [message #1732139 is a reply to message #1732051] Thu, 12 May 2016 19:07 Go to previous messageGo to next message
David Williams is currently offline David WilliamsFriend
Messages: 4
Registered: May 2016
Junior Member
Thank you for your suggestion Christian.

That java code is probably the kind of thing that could be invoked by eGit That is what I am asking - is there already something in eGit which we are missing that does that?

Re: Clear working directory before checkout in eGit [message #1732162 is a reply to message #1732139] Fri, 13 May 2016 06:21 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 263
Registered: July 2009
Senior Member
No, EGit offers git functionality and there is no git command which would do what you want. That doesn't mean we can't teach EGit useful stuff which isn't there in native git. But I personally, in may daily workflows don't have a use-case for completely wiping the working tree. But if you have and others also you could consider contributing that functionality to EGit

Ciao
Chris
Re: Clear working directory before checkout in eGit [message #1732261 is a reply to message #1732162] Sat, 14 May 2016 07:07 Go to previous messageGo to next message
David Williams is currently offline David WilliamsFriend
Messages: 4
Registered: May 2016
Junior Member
Thanks Christian.

Looking around, I wonder if the GIT command: "checkout -f" did what I was looking for.

Is there any eGIT capability that invoked "checkout -f" ?

David
Re: Clear working directory before checkout in eGit [message #1732431 is a reply to message #1732261] Tue, 17 May 2016 09:35 Go to previous message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 263
Registered: July 2009
Senior Member
JGits CheckoutCommand has a "setForce(boolean)" method ... but this is not the -f semantic you know from native git. You could first do a "git reset --hard" (with ResetCommand) to make sure all your changes in working tree and index are reset. Then you should be able to checkout. If you additionally want to get rid of all unstaged files you could use the CleanCommand. Something like:

		git.reset().setMode(ResetType.HARD).call();
		git.clean().call();
		Ref call = git.checkout().setName("refs/remotes/origin/x1").call();





Ciao
Chris
Previous Topic:Can't switch branches after last upgrade
Next Topic:Determining which authentication method
Goto Forum:
  


Current Time: Sun May 19 09:31:08 GMT 2019

Powered by FUDForum. Page generated in 0.02706 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top