Add support for boolean config values "yes", "no"
PATCH 1/2 Add support for boolean config values "yes", "no"
In 8f8c6fafd92f (shipped in 1.6.3) Linus taught C Git how to read boolean configuration values set to "yes" as true and "no" as false. Add support for these values, and some test cases.
---
This little 2 patch series is to adjust some cases in JGit to match Git 1.6.3 semantics.
.../org/spearce/jgit/lib/RepositoryConfigTest.java | 70 ++++++++++++++++++-- .../src/org/spearce/jgit/lib/RepositoryConfig.java | 13 +++- 2 files changed, 74 insertions(+), 9 deletions(-)
org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java +63 -7
@@ -57,9 +57,7 @@ * @throws IOException */ public void test001_ReadBareKey() throws IOException { - final File path = writeTrashFile("config_001", "[foo]\nbar\n"); - RepositoryConfig repositoryConfig = new RepositoryConfig(null, path); - System.out.println(repositoryConfig.getString("foo", null, "bar")); + final RepositoryConfig repositoryConfig = read("[foo]\nbar\n"); assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false)); assertEquals("", repositoryConfig.getString("foo", null, "bar")); }
@@ -70,8 +68,7 @@ public void test001_ReadBareKey() throws IOException { * @throws IOException */ public void test002_ReadWithSubsection() throws IOException { - final File path = writeTrashFile("config_002", "[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n"); - RepositoryConfig repositoryConfig = new RepositoryConfig(null, path); + final RepositoryConfig repositoryConfig = read("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n"); assertEquals(true, repositoryConfig.getBoolean("foo", "zip", "bar", false)); assertEquals("", repositoryConfig.getString("foo","zip", "bar")); assertEquals(false, repositoryConfig.getBoolean("foo", "zap", "bar", true));
@@ -113,8 +110,7 @@ assertTrue(Arrays.equals(values.toArray(), repositoryConfig } public void test006_readCaseInsensitive() throws IOException { - final File path = writeTrashFile("config_001", "[Foo]\nBar\n"); - RepositoryConfig repositoryConfig = new RepositoryConfig(null, path); + final RepositoryConfig repositoryConfig = read("[Foo]\nBar\n"); assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false)); assertEquals("", repositoryConfig.getString("foo", null, "bar")); }
@@ -182,4 +178,64 @@ public void test007_readUserInfos() throws IOException { assertEquals("local username", authorName); assertEquals("author@localemail", authorEmail); } + + public void testReadBoolean_TrueFalse1() throws IOException { + final RepositoryConfig c = read("[s]\na = true\nb = false\n"); + assertEquals("true", c.getString("s", null, "a")); + assertEquals("false", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_TrueFalse2() throws IOException { + final RepositoryConfig c = read("[s]\na = TrUe\nb = fAlSe\n"); + assertEquals("TrUe", c.getString("s", null, "a")); + assertEquals("fAlSe", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_YesNo1() throws IOException { + final RepositoryConfig c = read("[s]\na = yes\nb = no\n"); + assertEquals("yes", c.getString("s", null, "a")); + assertEquals("no", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_YesNo2() throws IOException { + final RepositoryConfig c = read("[s]\na = yEs\nb = NO\n"); + assertEquals("yEs", c.getString("s", null, "a")); + assertEquals("NO", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_OnOff1() throws IOException { + final RepositoryConfig c = read("[s]\na = on\nb = off\n"); + assertEquals("on", c.getString("s", null, "a")); + assertEquals("off", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_OnOff2() throws IOException { + final RepositoryConfig c = read("[s]\na = ON\nb = OFF\n"); + assertEquals("ON", c.getString("s", null, "a")); + assertEquals("OFF", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + private RepositoryConfig read(final String content) throws IOException { + final File p = writeTrashFile(getName() + ".config", content); + final RepositoryConfig c = new RepositoryConfig(null, p); + return c; + } }
org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java +11 -2
@@ -254,10 +254,19 @@ public boolean getBoolean(final String section, String subsection, return defaultValue; n = n.toLowerCase(); - if (MAGIC_EMPTY_VALUE.equals(n) || "yes".equalsIgnoreCase(n) || "true".equalsIgnoreCase(n) || "1".equals(n)) { + if (MAGIC_EMPTY_VALUE.equals(n) + || "yes".equalsIgnoreCase(n) + || "true".equalsIgnoreCase(n) + || "1".equals(n) + || "on".equals(n)) { return true; - } else if ("no".equalsIgnoreCase(n) || "false".equalsIgnoreCase(n) || "0".equalsIgnoreCase(n)) { + + } else if ("no".equalsIgnoreCase(n) + || "false".equalsIgnoreCase(n) + || "0".equalsIgnoreCase(n) + || "off".equalsIgnoreCase(n)) { return false; + } else { throw new IllegalArgumentException("Invalid boolean value: " + section + "." + name + "=" + n);
Signature · 1 line
1.6.3.195.gad81
PATCH 2/2 Make Repository.isValidRefName compatible with Git 1.6.3
In 3e262b95c509 I taught C Git to disallow refs whose names end in ".lock". This suffix is used by the atomic update mechanism as a signal that the ref is being modified. When reading a loose ref directory both JGit and C Git skip over any files whose names end with this suffix, as the file is assumed to be one of these magic locking files from another concurrent process. Consequently, any ref that ends with this name will become invisible once created.
We also add a suite of tests for the isValidRefName function, fix its formatting to better conform to current style conventions, and correct the result for "master"; this is not a valid ref name as it has only 1 path component. At least 2 path components is required.
--- .../tst/org/spearce/jgit/lib/ValidRefNameTest.java | 168 ++++++++++++++++++++ .../src/org/spearce/jgit/lib/Repository.java | 28 ++-- 2 files changed, 181 insertions(+), 15 deletions(-) create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/ValidRefNameTest.java
org.spearce.jgit.test/tst/org/spearce/jgit/lib/ValidRefNameTest.java new file +168 -0
@@ -0,0 +1,168 @@ +/* + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.lib; + +import junit.framework.TestCase; + +public class ValidRefNameTest extends TestCase { + private static void assertValid(final boolean exp, final String name) { + assertEquals("\"" + name + "\"", exp, Repository.isValidRefName(name)); + } + + public void testEmptyString() { + assertValid(false, ""); + assertValid(false, "/"); + } + + public void testMustHaveTwoComponents() { + assertValid(false, "master"); + assertValid(true, "heads/master"); + } + + public void testValidHead() { + assertValid(true, "refs/heads/master"); + assertValid(true, "refs/heads/pu"); + assertValid(true, "refs/heads/z"); + assertValid(true, "refs/heads/FoO"); + } + + public void testValidTag() { + assertValid(true, "refs/tags/v1.0"); + } + + public void testNoLockSuffix() { + assertValid(false, "refs/heads/master.lock"); + } + + public void testNoDirectorySuffix() { + assertValid(false, "refs/heads/master/"); + } + + public void testNoSpace() { + assertValid(false, "refs/heads/i haz space"); + } + + public void testNoAsciiControlCharacters() { + for (char c = '\0'; c < ' '; c++) + assertValid(false, "refs/heads/mast" + c + "er"); + } + + public void testNoBareDot() { + assertValid(false, "refs/heads/."); + assertValid(false, "refs/heads/.."); + assertValid(false, "refs/heads/./master"); + assertValid(false, "refs/heads/../master"); + } + + public void testNoLeadingDot() { + assertValid(false, "."); + assertValid(false, "refs/heads/.bar"); + assertValid(false, "refs/heads/..bar"); + } + + public void testContainsDot() { + assertValid(true, "refs/heads/m.a.s.t.e.r"); + assertValid(false, "refs/heads/master..pu"); + } + + public void testNoMagicRefCharacters() { + assertValid(false, "refs/heads/master^"); + assertValid(false, "refs/heads/^master"); + assertValid(false, "^refs/heads/master"); + + assertValid(false, "refs/heads/master~"); + assertValid(false, "refs/heads/~master"); + assertValid(false, "~refs/heads/master"); + + assertValid(false, "refs/heads/master:"); + assertValid(false, "refs/heads/:master"); + assertValid(false, ":refs/heads/master"); + } + + public void testShellGlob() { + assertValid(false, "refs/heads/master?"); + assertValid(false, "refs/heads/?master"); + assertValid(false, "?refs/heads/master"); + + assertValid(false, "refs/heads/master["); + assertValid(false, "refs/heads/[master"); + assertValid(false, "[refs/heads/master"); + + assertValid(false, "refs/heads/master*"); + assertValid(false, "refs/heads/*master"); + assertValid(false, "*refs/heads/master"); + } + + public void testValidSpecialCharacters() { + assertValid(true, "refs/heads/!"); + assertValid(true, "refs/heads/\""); + assertValid(true, "refs/heads/#"); + assertValid(true, "refs/heads/$"); + assertValid(true, "refs/heads/%"); + assertValid(true, "refs/heads/&"); + assertValid(true, "refs/heads/'"); + assertValid(true, "refs/heads/("); + assertValid(true, "refs/heads/)"); + assertValid(true, "refs/heads/+"); + assertValid(true, "refs/heads/,"); + assertValid(true, "refs/heads/-"); + assertValid(true, "refs/heads/;"); + assertValid(true, "refs/heads/<"); + assertValid(true, "refs/heads/="); + assertValid(true, "refs/heads/>"); + assertValid(true, "refs/heads/@"); + assertValid(true, "refs/heads/]"); + assertValid(true, "refs/heads/_"); + assertValid(true, "refs/heads/`"); + assertValid(true, "refs/heads/{"); + assertValid(true, "refs/heads/|"); + assertValid(true, "refs/heads/}"); + + // This is valid on UNIX, but not on Windows. + // + assertValid(true, "refs/heads/\\"); + } + + public void testRefLogQueryIsValidRef() { + // Yes, these are actually ambiguous. You can create a ref + // that also looks like a reflog query. + // + assertValid(true, "refs/heads/master@{1}"); + assertValid(true, "refs/heads/master@{1.hour.ago}"); + } +}
org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +13 -15
@@ -939,36 +939,34 @@ public static boolean isValidRefName(final String refName) { final int len = refName.length(); if (len == 0) return false; + if (refName.endsWith(".lock")) + return false; + int components = 1; char p = '\0'; - for (int i=0; i<len; ++i) { - char c = refName.charAt(i); + for (int i = 0; i < len; i++) { + final char c = refName.charAt(i); if (c <= ' ') return false; - switch(c) { + switch (c) { case '.': - if (i == 0) - return false; - if (p == '/') - return false; - if (p == '.') + switch (p) { + case '\0': case '/': case '.': return false; + } break; case '/': - if (i == 0) - return false; - if (i == len -1) + if (i == 0 || i == len - 1) return false; + components++; break; case '~': case '^': case ':': - case '?': case '[': - return false; - case '*': + case '?': case '[': case '*': return false; } p = c; } - return true; + return components > 1; } /**
Signature · 1 line
1.6.3.195.gad81
Robin Rosenberg
NAKRe: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
NAK, ammended patch follows.
Disallows @{, names that end with '.', '\' (that's the "almost" part) plus explicitly allows unicode names.
-- robin
Quoted 1 line
From
0854d96e2774bd084cc673ab16cf61c489f0562eMon Sep 17 00:00:00 2001
From: Shawn O. Pearce <[email protected]>
Date: Thu, 7 May 2009 08:05:14 -0700
Subject: [EGIT PATCH] Make Repository.isValidRefName almost compatible with Git 1.6.3
In 3e262b95c509 I taught C Git to disallow refs whose names end in ".lock". This suffix is used by the atomic update mechanism as a signal that the ref is being modified. When reading a loose ref directory both JGit and C Git skip over any files whose names end with this suffix, as the file is assumed to be one of these magic locking files from another concurrent process. Consequently, any ref that ends with this name will become invisible once created.
In cbdffe4093be Junio disallows names that looks like reflog queries as well as names that ends with periods.
We also add a suite of tests for the isValidRefName function, fix its formatting to better conform to current style conventions, and correct the result for "master"; this is not a valid ref name as it has only 1 path component. At least 2 path components is required.
In addition to 1.6.3 we disallow names with a '\' (backslash) since that is a directory separator in Windows. Allowing JGit users to create such names is just asking for trouble. This does not necessarily prevent JGit from working with such refs in other situations.
--- .../tst/org/spearce/jgit/lib/ValidRefNameTest.java | 171 ++++++++++++++++++++ .../src/org/spearce/jgit/lib/Repository.java | 33 +++-- 2 files changed, 191 insertions(+), 13 deletions(-) create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/ValidRefNameTest.java
org.spearce.jgit.test/tst/org/spearce/jgit/lib/ValidRefNameTest.java new file +171 -0
@@ -0,0 +1,171 @@ +/* + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.lib; + +import junit.framework.TestCase; + +public class ValidRefNameTest extends TestCase { + private static void assertValid(final boolean exp, final String name) { + assertEquals("\"" + name + "\"", exp, Repository.isValidRefName(name)); + } + + public void testEmptyString() { + assertValid(false, ""); + assertValid(false, "/"); + } + + public void testMustHaveTwoComponents() { + assertValid(false, "master"); + assertValid(true, "heads/master"); + } + + public void testValidHead() { + assertValid(true, "refs/heads/master"); + assertValid(true, "refs/heads/pu"); + assertValid(true, "refs/heads/z"); + assertValid(true, "refs/heads/FoO"); + } + + public void testValidTag() { + assertValid(true, "refs/tags/v1.0"); + } + + public void testNoLockSuffix() { + assertValid(false, "refs/heads/master.lock"); + } + + public void testNoDirectorySuffix() { + assertValid(false, "refs/heads/master/"); + } + + public void testNoSpace() { + assertValid(false, "refs/heads/i haz space"); + } + + public void testNoAsciiControlCharacters() { + for (char c = '\0'; c < ' '; c++) + assertValid(false, "refs/heads/mast" + c + "er"); + } + + public void testNoBareDot() { + assertValid(false, "refs/heads/."); + assertValid(false, "refs/heads/.."); + assertValid(false, "refs/heads/./master"); + assertValid(false, "refs/heads/../master"); + } + + public void testNoLeadingOrTrailingDot() { + assertValid(false, "."); + assertValid(false, "refs/heads/.bar"); + assertValid(false, "refs/heads/..bar"); + assertValid(false, "refs/heads/bar."); + } + + public void testContainsDot() { + assertValid(true, "refs/heads/m.a.s.t.e.r"); + assertValid(false, "refs/heads/master..pu"); + } + + public void testNoMagicRefCharacters() { + assertValid(false, "refs/heads/master^"); + assertValid(false, "refs/heads/^master"); + assertValid(false, "^refs/heads/master"); + + assertValid(false, "refs/heads/master~"); + assertValid(false, "refs/heads/~master"); + assertValid(false, "~refs/heads/master"); + + assertValid(false, "refs/heads/master:"); + assertValid(false, "refs/heads/:master"); + assertValid(false, ":refs/heads/master"); + } + + public void testShellGlob() { + assertValid(false, "refs/heads/master?"); + assertValid(false, "refs/heads/?master"); + assertValid(false, "?refs/heads/master"); + + assertValid(false, "refs/heads/master["); + assertValid(false, "refs/heads/[master"); + assertValid(false, "[refs/heads/master"); + + assertValid(false, "refs/heads/master*"); + assertValid(false, "refs/heads/*master"); + assertValid(false, "*refs/heads/master"); + } + + public void testValidSpecialCharacters() { + assertValid(true, "refs/heads/!"); + assertValid(true, "refs/heads/\""); + assertValid(true, "refs/heads/#"); + assertValid(true, "refs/heads/$"); + assertValid(true, "refs/heads/%"); + assertValid(true, "refs/heads/&"); + assertValid(true, "refs/heads/'"); + assertValid(true, "refs/heads/("); + assertValid(true, "refs/heads/)"); + assertValid(true, "refs/heads/+"); + assertValid(true, "refs/heads/,"); + assertValid(true, "refs/heads/-"); + assertValid(true, "refs/heads/;"); + assertValid(true, "refs/heads/<"); + assertValid(true, "refs/heads/="); + assertValid(true, "refs/heads/>"); + assertValid(true, "refs/heads/@"); + assertValid(true, "refs/heads/]"); + assertValid(true, "refs/heads/_"); + assertValid(true, "refs/heads/`"); + assertValid(true, "refs/heads/{"); + assertValid(true, "refs/heads/|"); + assertValid(true, "refs/heads/}"); + + // This is valid on UNIX, but not on Windows + // hence we make in invalid due to non-portability + // + assertValid(false, "refs/heads/\\"); + } + + public void testUnicodeNames() { + assertValid(true, "refs/heads/\u00e5ngstr\u00f6m"); + } + + public void testRefLogQueryIsValidRef() { + assertValid(false, "refs/heads/master@{1}"); + assertValid(false, "refs/heads/master@{1.hour.ago}"); + } +}
org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +20 -13
@@ -931,6 +931,8 @@ public RepositoryState getRepositoryState() { * a special meaning in a Git object reference expression. Some other * dangerous characters are also excluded. * + * For portability reasons '\' is excluded + * * @param refName * * @return true if refName is a valid ref name
@@ -939,36 +941,41 @@ public static boolean isValidRefName(final String refName) { final int len = refName.length(); if (len == 0) return false; + if (refName.endsWith(".lock")) + return false; + int components = 1; char p = '\0'; - for (int i=0; i<len; ++i) { - char c = refName.charAt(i); + for (int i = 0; i < len; i++) { + final char c = refName.charAt(i); if (c <= ' ') return false; - switch(c) { + switch (c) { case '.': - if (i == 0) - return false; - if (p == '/') + switch (p) { + case '\0': case '/': case '.': return false; - if (p == '.') + } + if (i == len -1) return false; break; case '/': - if (i == 0) + if (i == 0 || i == len - 1) return false; - if (i == len -1) + components++; + break; + case '@': + if (p == '{') return false; break; case '~': case '^': case ':': - case '?': case '[': - return false; - case '*': + case '?': case '[': case '*': + case '\\': return false; } p = c; } - return true; + return components > 1; } /**
Signature · 1 line
1.6.3
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
Quoted 3 lines · Robin Rosenberg
In
3e262b95c509I taught C Git to disallow refs whose names end in ".lock".
Btw, I think we should revert that, and instead change our naming for lock-files.
It's a silly limitation, and for a stupid reason, I think. I can well imagine people using a branch naming policy of using '.' instead of spaces, and then It makes perfect sense to call a branch "fix.vm.lock" when you have a VM locking issue you want to fix.
So I think we should remove that stupid "*.lock" rule from the C version (and then obviously from the JGIT one too).
Now, that leaves what we _should_ call the lock-files, and the only sane thing to do is to pick a sequence that is already invalid for other reasons. We could make the lockfile end in "..lock", for example (".." is not legal in names due to the confusion with the "a..b" range notation), or just end or begin with a "." (same issue, except "..." now).
Or just make it end in '~' which is also invalid for similar reasons.
Linus
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
Quoted 16 lines · Linus Torvalds
Quoted 3 lines · Robin Rosenberg
In
3e262b95c509I taught C Git to disallow refs whose names end in ".lock".Btw, I think we should revert that, and instead change our naming for lock-files.
It's a silly limitation, and for a stupid reason, I think. I can well imagine people using a branch naming policy of using '.' instead of spaces, and then It makes perfect sense to call a branch "fix.vm.lock" when you have a VM locking issue you want to fix.
So I think we should remove that stupid "*.lock" rule from the C version (and then obviously from the JGIT one too).
Quoted 7 lines
Now, that leaves what we _should_ call the lock-files, and the only sane thing to do is to pick a sequence that is already invalid for other reasons. We could make the lockfile end in "..lock", for example (".." is not legal in names due to the confusion with the "a..b" range notation), or just end or begin with a "." (same issue, except "..." now).
Or just make it end in '~' which is also invalid for similar reasons.
I was tempted to go for the tilde, but then realized that
.git/config~
is a common thing to have in a repository owned by an Emacs user, and we do create with O_CREAT, so it is not such a good idea.
'..lck' may be a good name to use here.
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Linus Torvalds
Quoted 3 lines
In
3e262b95c509I taught C Git to disallow refs whose names end in ".lock".Btw, I think we should revert that, and instead change our naming for lock-files.
'..lck' may be a good name to use here.
I agree. So much so that I wrote a patch for you.
--8<--
Change .lock suffix to ..lck
In 3e262b95c509 I taught Git to deny creation of refs whose name ends in ".lock", as the loose ref scanner skips over these under the assumption that they are refs being modified by a concurrent process operating on the same repository.
Linus pointed out that the name "fix.vm.lock" is an otherwise perfectly valid branch name, except the ".lock" suffix conflicts with the internal implementation detail of how Git manages doing an atomic update.
Instead, change the name to "..lck", as suggested by Junio.
This uses the same storage space in memory as ".lock", so we can do a fairly dumb search-replace to make the change, but the ".." prefix has been forbidden in a ref name for ages, to prevent "a..b" from being ambiguous as a single ref name, or as the pair "^a b".
--- Documentation/git-check-ref-format.txt | 2 -- Documentation/technical/api-lockfile.txt | 4 ++-- builtin-reflog.c | 2 +- config.c | 2 +- lockfile.c | 10 +++++----- refs.c | 11 ++++------- t/t3505-cherry-pick-empty.sh | 2 +- t/t3600-rm.sh | 4 ++-- t/t4123-apply-shrink.sh | 2 +- t/t7001-mv.sh | 4 ++-- t/t7502-commit.sh | 2 +- t/test-lib.sh | 6 +++--- 12 files changed, 23 insertions(+), 28 deletions(-)
Documentation/git-check-ref-format.txt +0 -2
@@ -34,8 +34,6 @@ imposes the following rules on how references are named: . They cannot end with a slash `/` nor a dot `.`. -. They cannot end with the sequence `.lock`. - . They cannot contain a sequence `@{`. These rules make it easy for shell script based tools to parse
Documentation/technical/api-lockfile.txt +2 -2
@@ -4,9 +4,9 @@ lockfile API The lockfile API serves two purposes: * Mutual exclusion. When we write out a new index file, first - we create a new file `$GIT_DIR/index.lock`, write the new + we create a new file `$GIT_DIR/index..lck`, write the new contents into it, and rename it to the final destination - `$GIT_DIR/index`. We try to create the `$GIT_DIR/index.lock` + `$GIT_DIR/index`. We try to create the `$GIT_DIR/index..lck` file with O_EXCL so that we can notice and fail when somebody else is already trying to update the index file.
builtin-reflog.c +1 -1
@@ -341,7 +341,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,if (!file_exists (log_file)) goto finish;if (!cmd->dry_run) { - newlog_path =git_pathdup ("logs/%s.lock" , ref); + newlog_path =git_pathdup ("logs/%s..lck" , ref); cb.newlog =fopen (newlog_path,"w" ); }
config.c +1 -1
@@ -925,7 +925,7 @@ int git_config_set(const char *key, const char *value)* * This function does this: * -* - it locks the config file by creating ".git/config.lock" +* - it locks the config file by creating ".git/config..lck" * * - it then parses the config using store_aux() as validator to find * the position on the key/value pair to replace. If it is to be unset,
lockfile.c +5 -5
@@ -129,11 +129,11 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)strcpy (lk->filename, path);/* * subtract 5 from size to make sure there's room for adding -* ".lock" for the lock file name +* "..lck" for the lock file name */ if (!(flags &LOCK_NODEREF ))resolve_symlink (lk->filename,sizeof (lk->filename)-5 ); -strcat (lk->filename,".lock" ); +strcat (lk->filename,"..lck" ); lk->fd =open (lk->filename,O_RDWR |O_CREAT |O_EXCL ,0666 );if (0 <= lk->fd) {if (!lock_file_list) {
@@ -159,13 +159,13 @@ static int lock_file(struct lock_file *lk, const char *path, int flags) NORETURN void unable_to_lock_index_die(const char *path, int err) {if (err ==EEXIST ) { -die ("Unable to create '%s.lock': %s.\n\n" +die ("Unable to create '%s..lck': %s.\n\n" "If no other git process is currently running, this probably means a\n" "git process crashed in this repository earlier. Make sure no other git\n" "process is running and remove the file manually to continue." , path,strerror (err)); }else { -die ("Unable to create '%s.lock': %s" , path,strerror (err)); +die ("Unable to create '%s..lck': %s" , path,strerror (err)); } }
@@ -219,7 +219,7 @@ int commit_lock_file(struct lock_file *lk)if (lk->fd >=0 &&close_lock_file (lk))return -1 ;strcpy (result_file, lk->filename); - i =strlen (result_file) -5 ;/* .lock */ + i =strlen (result_file) -5 ;/* ..lck */ result_file[i] =0 ;if (rename (lk->filename, result_file))return -1 ;
refs.c +4 -7
@@ -266,7 +266,7 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list) namelen =strlen (de->d_name);if (namelen >255 )continue ; -if (has_extension (de->d_name,".lock" )) +if (has_extension (de->d_name,"..lck" ))continue ;memcpy (ref + baselen, de->d_name, namelen+1 );if (stat (git_path ("%s" , ref), &st) <0 )
@@ -681,7 +681,6 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)* - it has double dots "..", or * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or * - it ends with a "/". -* - it ends with ".lock" */ static inline int bad_ref_char (int ch)
@@ -743,8 +742,6 @@ int check_ref_format(const char *ref)return CHECK_REF_FORMAT_ERROR ;if (level <2 )return CHECK_REF_FORMAT_ONELEVEL ; -if (has_extension (ref,".lock" )) -return CHECK_REF_FORMAT_ERROR ;return ret; } }
@@ -996,7 +993,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)const char *path;if (!(delopt &REF_NODEREF )) { - i =strlen (lock->lk->filename) -5 ;/* .lock */ + i =strlen (lock->lk->filename) -5 ;/* ..lck */ lock->lk->filename[i] =0 ; path = lock->lk->filename; }else {
@@ -1363,7 +1360,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,error ("refname too long: %s" , refs_heads_master); goto error_free_return; } - lockpath =mkpath ("%s.lock" , git_HEAD); + lockpath =mkpath ("%s..lck" , git_HEAD); fd =open (lockpath,O_CREAT |O_EXCL |O_WRONLY ,0666 );if (fd <0 ) {error ("Unable to open %s for writing" , lockpath);
@@ -1593,7 +1590,7 @@ static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data) namelen =strlen (de->d_name);if (namelen >255 )continue ; -if (has_extension (de->d_name,".lock" )) +if (has_extension (de->d_name,"..lck" ))continue ;memcpy (log + baselen, de->d_name, namelen+1 );if (stat (git_path ("logs/%s" , log), &st) <0 )
t/t3505-cherry-pick-empty.sh +1 -1
@@ -26,7 +26,7 @@ test_expect_code 1 'cherry-pick an empty commit' ' test_expect_success 'index lockfile was removed' ' - test ! -f .git/index.lock + test ! -f .git/index..lck '
t/t3600-rm.sh +2 -2
@@ -264,9 +264,9 @@ test_expect_success 'choking "git rm" should not let it die with cruft' ' i=$(( $i + 1 )) done | git update-index --index-info && git rm -n "some-file-*" | :; - test -f .git/index.lock + test -f .git/index..lck status=$? - rm -f .git/index.lock + rm -f .git/index..lck git reset -q --hard test "$status" != 0 '
t/t4123-apply-shrink.sh +1 -1
@@ -47,7 +47,7 @@ test_expect_success 'apply should fail gracefully' ' else status=$? echo "Status was $status" - if test -f .git/index.lock + if test -f .git/index..lck then echo Oops, should not have crashed false
t/t7001-mv.sh +2 -2
@@ -62,14 +62,14 @@ test_expect_success \ 'checking -f on untracked file with existing target' \ 'touch path0/untracked1 && git mv -f untracked1 path0 - test ! -f .git/index.lock && + test ! -f .git/index..lck && test -f untracked1 && test -f path0/untracked1' # clean up the mess in case bad things happen rm -f idontexist untracked1 untracked2 \ path0/idontexist path0/untracked1 path0/untracked2 \ - .git/index.lock + .git/index..lck test_expect_success \ 'adding another file' \
t/t7502-commit.sh +1 -1
@@ -241,7 +241,7 @@ test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' ' GIT_EDITOR=.git/FAKE_EDITOR export GIT_EDITOR exec git commit -a'\'' && - test ! -f .git/index.lock + test ! -f .git/index..lck ' rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
t/test-lib.sh +3 -3
@@ -558,13 +558,13 @@ else test -h "$2" && test "$1" = "$(readlink "$2")" || { # be super paranoid - if mkdir "$2".lock + if mkdir "$2"..lck then rm -f "$2" && ln -s "$1" "$2" && - rm -r "$2".lock + rm -r "$2"..lck else - while test -d "$2".lock + while test -d "$2"..lck do say "Waiting for lock on $2." sleep 1
Signature · 5 lines
1.6.3.195.gad81 -- Shawn.
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
2009/5/8 Shawn O. Pearce <[email protected]>:
Quoted 4 lines
lockfile.c +0
-0@@ -129,11 +129,11 @@ static int lock_file(struct lock_file *lk, const char *path, int flags) strcpy(lk->filename, path);
lockfile.c static int lock_file(struct lock_file *lk, const char *path, int flags) · line 137
Quoted 10 lines
lockfile.c +2
-2/* * subtract 5 from size to make sure there's room for adding - * ".lock" for the lock file name + * "..lck" for the lock file name */ if (!(flags & LOCK_NODEREF)) resolve_symlink(lk->filename, sizeof(lk->filename)-5); - strcat(lk->filename, ".lock"); + strcat(lk->filename, "..lck"); lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
There must be at least some deprecation period during which also the old .lock lockfiles are considered and created. Just imagine someone modifying the ref with an older version, which knows nothing about ..lck.
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
Hi,
Quoted 12 lines · Alex Riesen
2009/5/8 Shawn O. Pearce <[email protected]>:
Quoted 11 lines
lockfile.c +1
-1@@ -129,11 +129,11 @@ static int lock_file(struct lock_file *lk, const char *path, int flags) strcpy(lk->filename, path); /* * subtract 5 from size to make sure there's room for adding - * ".lock" for the lock file name + * "..lck" for the lock file name */ if (!(flags & LOCK_NODEREF)) resolve_symlink(lk->filename, sizeof(lk->filename)-5);
Quoted 8 lines
lockfile.c static int lock_file(struct lock_file *lk, const char *path, int flags) · line 137
Quoted 3 lines
lockfile.c +1
-1- strcat(lk->filename, ".lock"); + strcat(lk->filename, "..lck"); lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);There must be at least some deprecation period during which also the old .lock lockfiles are considered and created. Just imagine someone modifying the ref with an older version, which knows nothing about ..lck.
As lock files are only supposed to be created by Git itself, and have a maximum lifetime until the end of the process, I think we do not need a grace period at all.
Ciao,
Dscho
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
Quoted 8 lines · Johannes Schindelin
Quoted 4 lines
There must be at least some deprecation period during which also the old .lock lockfiles are considered and created. Just imagine someone modifying the ref with an older version, which knows nothing about ..lck.
As lock files are only supposed to be created by Git itself, and have a maximum lifetime until the end of the process, I think we do not need a grace period at all.
Could there be people with slightly older git and shiny new jgit (or the other combination) working on the same repository?
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
Hi,
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Johannes Schindelin
Quoted 4 lines
There must be at least some deprecation period during which also the old .lock lockfiles are considered and created. Just imagine someone modifying the ref with an older version, which knows nothing about ..lck.
Quoted 6 lines
Quoted 3 lines
As lock files are only supposed to be created by Git itself, and have a maximum lifetime until the end of the process, I think we do not need a grace period at all.
Could there be people with slightly older git and shiny new jgit (or the other combination) working on the same repository?
You mean concurrently? Sure, but do we have to care? People doing this certainly know what they are doing, and live happily even with a 0.5" hole in their foot.
Ciao,
Dscho
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
2009/5/8 Johannes Schindelin <[email protected]>:
Quoted 12 lines · Junio C Hamano
Quoted 11 lines
Quoted 8 lines · Johannes Schindelin
Quoted 4 lines
There must be at least some deprecation period during which also the old .lock lockfiles are considered and created. Just imagine someone modifying the ref with an older version, which knows nothing about ..lck.
As lock files are only supposed to be created by Git itself, and have a maximum lifetime until the end of the process, I think we do not need a grace period at all.
Quoted 6 lines
Quoted 2 lines
Could there be people with slightly older git and shiny new jgit (or the other combination) working on the same repository?
You mean concurrently? Sure, but do we have to care? People doing this certainly know what they are doing, and live happily even with a 0.5" hole in their foot.
I certainly hope that'll be you, one day.
Of course people run git concurrently on the same repo. Even from different machines.
That's _why_ we have the locking in the first place.
And of course there will be different versions of git installed on these machines.
And most certainly the people will not know what they were doing, blame the hole on Git (and maybe even shoot you in return, after they'll find that stupid message of yours in archives).
Re: [JGIT PATCH 2/2] Make Repository.isValidRefName compatible with Git 1.6.3
Quoted 5 lines · Alex Riesen
2009/5/8 Johannes Schindelin <[email protected]>:
Quoted 4 lines · Junio C Hamano
Quoted 3 lines
Could there be people with slightly older git and shiny new jgit (or the other combination) working on the same repository?
Yes, Gerrit Code Review runs a JGit daemon 24/7. I have a lot of folks running Gerrit servers inside their company networks, and two running them on the public internet. Gerrit runs bleeding edge JGit, and would most certainly pick up a "..lck" patch there before they upgrade their git-core package.
This is one of those cases where the locking is really important, because you should be able to concurrently access the same git repository from the command line with git-core utilities while Gerrit's daemon process is servicing network based user requests.
Quoted 3 lines
Quoted 3 lines
You mean concurrently? ??Sure, but do we have to care? ??People doing this certainly know what they are doing, and live happily even with a 0.5" hole in their foot.
Yes, dammit, we should care.
Quoted 2 lines
Of course people run git concurrently on the same repo. Even from different machines.
Even ignoring JGit and Gerrit's daemon process entirely as "useless stupid projects that never should have happened", _this_ is enough of a reason to care. People use Git on NFS all of the time. With different machines. Accessing the same repo. Those different machines most likely have different versions of Git installed.
Quoted 1 line
That's _why_ we have the locking in the first place.
Yes.
I've actually thought about this "fix.vm.lock" being invalid for quite a while now, and wanted to change lockfile.c to use another pattern that was already an invalid ref name, like Junio's "..lck" proposal does. But I've never been able to come up with a sane way to deal with the transition. So I never brought the topic up before.
I still don't have a sane way to deal with the transition.
The only thing I can think of is:
- grab ".lock"
- grab "..lck"
- do stuff
- commit by link("..lck", orig)
- ulink ".lock"
And maybe in a year we stop writing ".lock".
But, here's news for you, *#@!#@@!$@!*&@! distributions are still shipping 1.5.0-1.5.0.3. In particular I've spent the better part of this past two weeks telling new Git users to upgrade off of these versions due to the pread() bug introduced in [1] and fixed in [2].
I've seen WAAAAY too many "fatal: cannot pread pack file:" errors. Enough for a lifetime.
[1] http://repo.or.cz/w/git.git?a=commit;h=6d2fa7f1b489c65e677c18eda5c144dbc5d614ab
[2] http://repo.or.cz/w/git.git?a=commit;h=a91d49cd369ac5fc8e1a17357a975d09cf6c8cb3
Signature · 1 line
Shawn.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH] Allow branch names that end with ".lock"
Quoted 4 lines · Junio C Hamano
A project policy for naming branches could be to use a dot as a word separator (instead of '-' which is often done by existing projects), and "fix.vm.lock" could be a very valid name for a branch to address a VM locking issues.
I think we sent the same patch... except...
Quoted 1 line
git-gui/lib/index.tcl | 2 +-
Don't do that. I'll patch git-gui and send you a pull request.
Signature · 1 line
Shawn.
Re: [PATCH] Allow branch names that end with ".lock"
Quoted 11 lines · Shawn O. Pearce
Quoted 4 lines · Junio C Hamano
A project policy for naming branches could be to use a dot as a word separator (instead of '-' which is often done by existing projects), and "fix.vm.lock" could be a very valid name for a branch to address a VM locking issues.
I think we sent the same patch... except...
Quoted 1 line
git-gui/lib/index.tcl | 2 +-Don't do that. I'll patch git-gui and send you a pull request.
Why not? I never commit my "how about this" weatherbaloon patches directly ;-)
If you found that ours match identically except for that one line, that is a very good indication. I didn't check.
Shawn O. Pearce
NAKRe: [PATCH] Allow branch names that end with ".lock"
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Shawn O. Pearce
Quoted 4 lines · Junio C Hamano
A project policy for naming branches could be to use a dot as a word separator (instead of '-' which is often done by existing projects), and "fix.vm.lock" could be a very valid name for a branch to address a VM locking issues.
Quoted 8 lines
Quoted 5 lines
I think we sent the same patch... except...
Quoted 1 line
git-gui/lib/index.tcl | 2 +-Don't do that. I'll patch git-gui and send you a pull request.
Why not? I never commit my "how about this" weatherbaloon patches directly ;-)
I know. I was saying, "don't patch git-gui in the same commit as git.git". I would have spun that as two different commits.
Quoted 2 lines
If you found that ours match identically except for that one line, that is a very good indication. I didn't check.
Here's git diff yours mine, we were really close:
Documentation/technical/api-lockfile.txt +1 -1
@@ -4,7 +4,7 @@ lockfile API The lockfile API serves two purposes: * Mutual exclusion. When we write out a new index file, first - we create a new "lock" file `$GIT_DIR/index..lck`, write the new + we create a new file `$GIT_DIR/index..lck`, write the new contents into it, and rename it to the final destination `$GIT_DIR/index`. We try to create the `$GIT_DIR/index..lck` file with O_EXCL so that we can notice and fail when somebody
ACK, I like your version better than mine.
refs.c +0 -2
@@ -742,8 +742,6 @@ int check_ref_format(const char *ref)return CHECK_REF_FORMAT_ERROR ;if (level <2 )return CHECK_REF_FORMAT_ONELEVEL ; -if (has_extension (ref,"..lck" )) -return CHECK_REF_FORMAT_ERROR ;return ret; } }
NAK, I like how I removed this block. It can't happen anymore, the no ".." in name earlier should have caught the condition.
Signature · 1 line
Shawn.
Re: [PATCH] Allow branch names that end with ".lock"
Quoted 14 lines · Shawn O. Pearce
refs.c +0
-2@@ -742,8 +742,6 @@ int check_ref_format(const char *ref)return CHECK_REF_FORMAT_ERROR ;if (level <2 )return CHECK_REF_FORMAT_ONELEVEL ; -if (has_extension (ref,"..lck" )) -return CHECK_REF_FORMAT_ERROR ;return ret; } }
Quoted 2 lines
NAK, I like how I removed this block. It can't happen anymore, the no ".." in name earlier should have caught the condition.
Very true. Thanks.
Quoted 3 lines · Shawn O. Pearce
In
8f8c6fafd92f(shipped in 1.6.3) Linus taught C Git how to read boolean configuration values set to "yes" as true and "no" as false. Add support for these values, and some test cases.
Linus added support for "on" and "off" in that commit as it appears your commit is also doing. :)
I was about to point out that you are not ignoring case for "on", but using equalsIgnoreCase() appears to be unnecessary since above that you are already doing n = n.toLowerCase().
-brandon
Quoted 26 lines
org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java +11
-2@@ -254,10 +254,19 @@ public boolean getBoolean(final String section, String subsection, return defaultValue; n = n.toLowerCase(); - if (MAGIC_EMPTY_VALUE.equals(n) || "yes".equalsIgnoreCase(n) || "true".equalsIgnoreCase(n) || "1".equals(n)) { + if (MAGIC_EMPTY_VALUE.equals(n) + || "yes".equalsIgnoreCase(n) + || "true".equalsIgnoreCase(n) + || "1".equals(n) + || "on".equals(n)) { return true; - } else if ("no".equalsIgnoreCase(n) || "false".equalsIgnoreCase(n) || "0".equalsIgnoreCase(n)) { + + } else if ("no".equalsIgnoreCase(n) + || "false".equalsIgnoreCase(n) + || "0".equalsIgnoreCase(n) + || "off".equalsIgnoreCase(n)) { return false; + } else { throw new IllegalArgumentException("Invalid boolean value: " + section + "." + name + "=" + n);
PATCH v2 1/2 Add support for boolean config values "on", "off"
In 8f8c6fafd92f (shipped in 1.6.3) Linus taught C Git how to read boolean configuration values set to "on" as true and "off" as false. Add support for these values, and some test cases.
---
Quoted 7 lines · Brandon Casey
Quoted 3 lines · Shawn O. Pearce
In
8f8c6fafd92f(shipped in 1.6.3) Linus taught C Git how to read boolean configuration values set to "yes" as true and "no" as false. Add support for these values, and some test cases.Linus added support for "on" and "off" in that commit as it appears your commit is also doing. :)
Whoops, good catch, thanks.
Quoted 3 lines
I was about to point out that you are not ignoring case for "on", but using
equalsIgnoreCase()appears to be unnecessary since above that you are already doing n =n.toLowerCase().
I dropped the toLowerCase and used equalsIgnoreCase instead. There is no reason to be allocating a copy of the string when equalsIgnorCase is sufficient.
.../org/spearce/jgit/lib/RepositoryConfigTest.java | 70 ++++++++++++++++++-- .../src/org/spearce/jgit/lib/RepositoryConfig.java | 14 +++- 2 files changed, 74 insertions(+), 10 deletions(-)
org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java +63 -7
@@ -57,9 +57,7 @@ * @throws IOException */ public void test001_ReadBareKey() throws IOException { - final File path = writeTrashFile("config_001", "[foo]\nbar\n"); - RepositoryConfig repositoryConfig = new RepositoryConfig(null, path); - System.out.println(repositoryConfig.getString("foo", null, "bar")); + final RepositoryConfig repositoryConfig = read("[foo]\nbar\n"); assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false)); assertEquals("", repositoryConfig.getString("foo", null, "bar")); }
@@ -70,8 +68,7 @@ public void test001_ReadBareKey() throws IOException { * @throws IOException */ public void test002_ReadWithSubsection() throws IOException { - final File path = writeTrashFile("config_002", "[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n"); - RepositoryConfig repositoryConfig = new RepositoryConfig(null, path); + final RepositoryConfig repositoryConfig = read("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n"); assertEquals(true, repositoryConfig.getBoolean("foo", "zip", "bar", false)); assertEquals("", repositoryConfig.getString("foo","zip", "bar")); assertEquals(false, repositoryConfig.getBoolean("foo", "zap", "bar", true));
@@ -113,8 +110,7 @@ assertTrue(Arrays.equals(values.toArray(), repositoryConfig } public void test006_readCaseInsensitive() throws IOException { - final File path = writeTrashFile("config_001", "[Foo]\nBar\n"); - RepositoryConfig repositoryConfig = new RepositoryConfig(null, path); + final RepositoryConfig repositoryConfig = read("[Foo]\nBar\n"); assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false)); assertEquals("", repositoryConfig.getString("foo", null, "bar")); }
@@ -182,4 +178,64 @@ public void test007_readUserInfos() throws IOException { assertEquals("local username", authorName); assertEquals("author@localemail", authorEmail); } + + public void testReadBoolean_TrueFalse1() throws IOException { + final RepositoryConfig c = read("[s]\na = true\nb = false\n"); + assertEquals("true", c.getString("s", null, "a")); + assertEquals("false", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_TrueFalse2() throws IOException { + final RepositoryConfig c = read("[s]\na = TrUe\nb = fAlSe\n"); + assertEquals("TrUe", c.getString("s", null, "a")); + assertEquals("fAlSe", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_YesNo1() throws IOException { + final RepositoryConfig c = read("[s]\na = yes\nb = no\n"); + assertEquals("yes", c.getString("s", null, "a")); + assertEquals("no", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_YesNo2() throws IOException { + final RepositoryConfig c = read("[s]\na = yEs\nb = NO\n"); + assertEquals("yEs", c.getString("s", null, "a")); + assertEquals("NO", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_OnOff1() throws IOException { + final RepositoryConfig c = read("[s]\na = on\nb = off\n"); + assertEquals("on", c.getString("s", null, "a")); + assertEquals("off", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + public void testReadBoolean_OnOff2() throws IOException { + final RepositoryConfig c = read("[s]\na = ON\nb = OFF\n"); + assertEquals("ON", c.getString("s", null, "a")); + assertEquals("OFF", c.getString("s", null, "b")); + + assertTrue(c.getBoolean("s", "a", false)); + assertFalse(c.getBoolean("s", "b", true)); + } + + private RepositoryConfig read(final String content) throws IOException { + final File p = writeTrashFile(getName() + ".config", content); + final RepositoryConfig c = new RepositoryConfig(null, p); + return c; + } }
org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java +11 -3
@@ -253,11 +253,19 @@ public boolean getBoolean(final String section, String subsection, if (n == null) return defaultValue; - n = n.toLowerCase(); - if (MAGIC_EMPTY_VALUE.equals(n) || "yes".equalsIgnoreCase(n) || "true".equalsIgnoreCase(n) || "1".equals(n)) { + if (MAGIC_EMPTY_VALUE.equals(n) + || "yes".equalsIgnoreCase(n) + || "true".equalsIgnoreCase(n) + || "1".equals(n) + || "on".equalsIgnoreCase(n)) { return true; - } else if ("no".equalsIgnoreCase(n) || "false".equalsIgnoreCase(n) || "0".equalsIgnoreCase(n)) { + + } else if ("no".equalsIgnoreCase(n) + || "false".equalsIgnoreCase(n) + || "0".equals(n) + || "off".equalsIgnoreCase(n)) { return false; + } else { throw new IllegalArgumentException("Invalid boolean value: " + section + "." + name + "=" + n);
Signature · 1 line
1.6.3.195.gad81