Skip to content

Commit

Permalink
Merge pull request #192 from loomchild/master
Browse files Browse the repository at this point in the history
JENKINS-31108 Disable push from shallow clone using CGit<1.9.0
  • Loading branch information
MarkEWaite committed Nov 7, 2015
2 parents 53f81db + f348aa5 commit 0e0a310
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Expand Up @@ -1075,6 +1075,10 @@ public boolean isBareRepository(String GIT_DIR) throws GitException, Interrupted
return !"false".equals(line.trim());
}

public boolean isShallowRepository() {
return new File(workspace, pathJoin(".git", "shallow")).exists();
}

private String pathJoin( String a, String b ) {
return new File(a, b).toString();
}
Expand Down Expand Up @@ -1728,6 +1732,10 @@ public void execute() throws GitException, InterruptedException {
args.add("--tags");
}

if (!isAtLeastVersion(1,9,0,0) && isShallowRepository()) {
throw new GitException("Can't push from shallow repository using git client older than 1.9.0");
}

StandardCredentials cred = credentials.get(remote.toPrivateString());
if (cred == null) cred = defaultCredentials;
launchCommandWithCredentials(args, workspace, cred, remote, timeout);
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -1601,6 +1601,37 @@ public void test_push_deprecated_signature() throws Exception {
assertEquals("Working SHA1 != bare SHA1", w.git.getHeadRev(w.repoPath(), "master"), bare.git.getHeadRev(bare.repoPath(), "master"));
}

@NotImplementedInJGit
public void test_push_from_shallow_clone() throws Exception {
WorkingArea r = new WorkingArea();
r.init();
r.commitEmpty("init");
r.touch("file1");
r.git.add("file1");
r.git.commit("commit1");
r.cmd("git checkout -b other");

w.init();
w.cmd("git remote add origin " + r.repoPath());
w.cmd("git pull --depth=1 origin master");

w.touch("file2");
w.git.add("file2");
w.git.commit("commit2");
ObjectId sha1 = w.head();

try {
w.git.push("origin", "master");
assertTrue("git < 1.9.0 can push from shallow repository", w.cgit().isAtLeastVersion(1, 9, 0, 0));
String remoteSha1 = r.cmd("git rev-parse master").substring(0, 40);
assertEquals(sha1.name(), remoteSha1);
} catch (GitException e) {
// expected for git cli < 1.9.0
assertTrue("Wrong exception message: " + e, e.getMessage().contains("push from shallow repository"));
assertFalse("git >= 1.9.0 can't push from shallow repository", w.cgit().isAtLeastVersion(1, 9, 0, 0));
}
}

public void test_notes_add() throws Exception {
w.init();
w.touch("file1");
Expand Down

0 comments on commit 0e0a310

Please sign in to comment.