String[] paths = getPaths(outputDirectory);
String prefix = getPrefix(destinationDirectory);
GitHubClient client = createClient(host, userName, password, oauth2Token);
DataService service = new DataService(client);
boolean createNoJekyll = noJekyll;
if(createNoJekyll){
for(String path: paths){
if (NO_JEKYLL_FILE.equals(path)){
createNoJekyll = false;
break;
}
}
}
// Write blobs and build tree entries
List<TreeEntry> entries = new ArrayList<TreeEntry>(paths.length);
if(numThreads <= 1){
createEntries(entries, prefix, paths, service, repository, outputDirectory);
}else{
createEntriesInThreads(entries, prefix, paths, service, repository, outputDirectory, numThreads);
}
if (createNoJekyll) {
if (log.isDebugEnabled()){
log.debug("Creating empty '.nojekyll' blob at root of tree");
}
TreeEntry entry = createEntry("", NO_JEKYLL_FILE, service, repository, outputDirectory);
entries.add(entry);
}
Reference ref = getReference(service, repository);
if(dryRun){
log.debug("Dry run mode, skip deploy.");
return;
}
// Write tree
Tree tree = createTree(service, repository, ref, entries);
// Build commit
Commit commit = new Commit();
commit.setMessage(message != null ? message : "GitHubWagon: Deploying OpooPress to GitHub Pages.");
commit.setTree(tree);
// Set parent commit SHA-1 if reference exists
if (ref != null){
commit.setParents(Collections.singletonList(new Commit().setSha(ref.getObject().getSha())));
}
Commit created;
try {
created = service.createCommit(repository, commit);
log.info(MessageFormat.format("Creating commit with SHA-1: {0}", created.getSha()));
} catch (IOException e) {
throw new GitHubException("Error creating commit: " + e.getMessage(), e);
}
TypedResource object = new TypedResource();
object.setType(TYPE_COMMIT).setSha(created.getSha());
if (ref != null) {
// Update existing reference
ref.setObject(object);
try {
log.info(String.format("Updating reference %s from %s to %s", branch, commit.getParents().get(0).getSha(), created.getSha()));
service.editReference(repository, ref, force);
} catch (IOException e) {
throw new GitHubException("Error editing reference: " + e.getMessage(), e);
}
} else {
// Create new reference
ref = new Reference().setObject(object).setRef(branch);
try {
log.info(MessageFormat.format("Creating reference {0} starting at commit {1}", branch, created.getSha()));
service.createReference(repository, ref);
} catch (IOException e) {
throw new GitHubException("Error creating reference: " + e.getMessage(), e);
}
}
}