login
Header Space

 
 

Re: [JGIT PATCH v2 07/24] Added findWorkTree method to Repository class.

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Florian Koeberle <florianskarten@...>
Cc: <git@...>
Date: Monday, May 12, 2008 - 8:24 pm

Florian Koeberle <florianskarten@web.de> wrote:

Isn't `final File workingDirectoryFile = new File(".")` easier to write?

And if we are going to throw a checked exception because we cannot
find a repository in ".", maybe that should be a new checked
exception, like NoGitRepositoryFoundException?


References to C Git aren't common in jgit source code.  We aren't
a reference implementation, but that hasn't stopped people from
finding out code a sane description of "how things work".  I'd rather
not describe our code in terms of C Git's code; especially what
happens if C Git refactors one day, this reference might no longer
make sense.  Anyone coming back here to improve jgit or use this
method will be confused about what we are doing.


We usually omit { and } on simple conditions like this.  Its a coding
pattern we stole from C Git, which stole it from the Linux kernel.


Would this perhaps be a shorter, easier to follow variant of the
same algorithm?

	directory = directory.getAbsoluteFile();
	File workdir = directory;
	while (workdir != null) {
		final File gitdir = new File(workdir, REPOSITORY_DIRECTORY_NAME);
		if (isRepository(gitdir))
			return new WorkTree(workdir, new Repository(gitdir));
		if (isRepository(workdir))
			return new WorkTree(null, new Repository(workdir));
		workdir = workdir.getParentFile();
	}
	throw new NoGitRepositoryFoundException("No repository for " + directory);

I often do write infinite loops with "for (;;) {", but usually its
because the loop condition is based on an iterator-style method
returning null at the end and I want to store the result in a local
variable scoped to only the body of the loop:

	for (;;) {
		final RevCommit next = walk.next();
		if (next == null)
			break;

		// use next somehow
	}

Otherwise I try to avoid infinite loops in my final version
of something.  Though on a recent job interview the interviewer
noticed I tend to start writing code with an infinite loop, then
go back and correct the loop condition before saying "done".  ;-)

-- 
Shawn.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [JGIT PATCH v2 04/24] Added path related constats to Con..., Florian Köberle, (Fri May 23, 11:46 am)
[JGIT PATCH v2 02/24] Formatted Repository class., Florian Koeberle, (Mon May 12, 4:13 pm)
[JGIT PATCH v2 09/24] Added the class Rule., Florian Koeberle, (Mon May 12, 4:13 pm)
Re: [JGIT PATCH v2 07/24] Added findWorkTree method to Repos..., Shawn O. Pearce, (Mon May 12, 8:24 pm)
[JGIT PATCH v2 08/24] Added the interface FilePattern., Florian Koeberle, (Mon May 12, 4:13 pm)
[JGIT PATCH v2 10/24] Added the iterface Rules., Florian Koeberle, (Mon May 12, 4:13 pm)
[JGIT PATCH v2 12/24] Added the class GlobalFilePattern, Florian Koeberle, (Mon May 12, 4:13 pm)
[JGIT PATCH v2 11/24] Added the class FNMatchPattern., Florian Koeberle, (Mon May 12, 4:13 pm)
Re: [JGIT PATCH v2 11/24] Added the class FNMatchPattern., Shawn O. Pearce, (Mon May 12, 8:38 pm)
[JGIT PATCH v2 19/24] Added the class AddRuleListFactory., Florian Koeberle, (Mon May 12, 4:13 pm)
Re: [JGIT PATCH v2 19/24] Added the class AddRuleListFactory., Florian Köberle, (Tue May 13, 7:24 am)
[JGIT PATCH v2 17/24] Added the class TreeFilePattern., Florian Koeberle, (Mon May 12, 4:13 pm)
Re: [JGIT PATCH v2 17/24] Added the class TreeFilePattern., Shawn O. Pearce, (Mon May 12, 9:22 pm)
[JGIT PATCH v2 20/24] Added class AddRulesFactory., Florian Koeberle, (Mon May 12, 4:13 pm)
[JGIT PATCH v2 22/24] Added class LightFileTreeIterable., Florian Koeberle, (Mon May 12, 4:13 pm)
[JGIT PATCH v2 03/24] Formatted Constats class., Florian Koeberle, (Mon May 12, 4:13 pm)
[JGIT PATCH v2 14/24] Added the class IgnoreRuleListFactory., Florian Koeberle, (Mon May 12, 4:13 pm)
[JGIT PATCH v2 13/24] Added the class ComplexFilePattern., Florian Koeberle, (Mon May 12, 4:13 pm)
speck-geostationary