View Javadoc
1   /*
2    * Copyright (C) 2014, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.gitrepo;
44  
45  import static org.eclipse.jgit.lib.Constants.DEFAULT_REMOTE_NAME;
46  import static org.eclipse.jgit.lib.Constants.R_REMOTES;
47  
48  import java.io.File;
49  import java.io.FileInputStream;
50  import java.io.IOException;
51  import java.io.InputStream;
52  import java.net.URI;
53  import java.text.MessageFormat;
54  import java.util.ArrayList;
55  import java.util.List;
56  import java.util.Map;
57  import java.util.Set;
58  import java.util.StringJoiner;
59  
60  import org.eclipse.jgit.annotations.Nullable;
61  import org.eclipse.jgit.api.Git;
62  import org.eclipse.jgit.api.GitCommand;
63  import org.eclipse.jgit.api.SubmoduleAddCommand;
64  import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
65  import org.eclipse.jgit.api.errors.GitAPIException;
66  import org.eclipse.jgit.api.errors.JGitInternalException;
67  import org.eclipse.jgit.dircache.DirCache;
68  import org.eclipse.jgit.dircache.DirCacheBuilder;
69  import org.eclipse.jgit.dircache.DirCacheEntry;
70  import org.eclipse.jgit.gitrepo.ManifestParser.IncludedFileReader;
71  import org.eclipse.jgit.gitrepo.RepoProject.CopyFile;
72  import org.eclipse.jgit.gitrepo.RepoProject.LinkFile;
73  import org.eclipse.jgit.gitrepo.internal.RepoText;
74  import org.eclipse.jgit.internal.JGitText;
75  import org.eclipse.jgit.lib.CommitBuilder;
76  import org.eclipse.jgit.lib.Config;
77  import org.eclipse.jgit.lib.Constants;
78  import org.eclipse.jgit.lib.FileMode;
79  import org.eclipse.jgit.lib.ObjectId;
80  import org.eclipse.jgit.lib.ObjectInserter;
81  import org.eclipse.jgit.lib.ObjectReader;
82  import org.eclipse.jgit.lib.PersonIdent;
83  import org.eclipse.jgit.lib.ProgressMonitor;
84  import org.eclipse.jgit.lib.Ref;
85  import org.eclipse.jgit.lib.RefDatabase;
86  import org.eclipse.jgit.lib.RefUpdate;
87  import org.eclipse.jgit.lib.RefUpdate.Result;
88  import org.eclipse.jgit.lib.Repository;
89  import org.eclipse.jgit.revwalk.RevCommit;
90  import org.eclipse.jgit.revwalk.RevWalk;
91  import org.eclipse.jgit.util.FileUtils;
92  
93  /**
94   * A class used to execute a repo command.
95   *
96   * This will parse a repo XML manifest, convert it into .gitmodules file and the
97   * repository config file.
98   *
99   * If called against a bare repository, it will replace all the existing content
100  * of the repository with the contents populated from the manifest.
101  *
102  * repo manifest allows projects overlapping, e.g. one project's manifestPath is
103  * "foo" and another project's manifestPath is "foo/bar". This won't
104  * work in git submodule, so we'll skip all the sub projects
105  * ("foo/bar" in the example) while converting.
106  *
107  * @see <a href="https://code.google.com/p/git-repo/">git-repo project page</a>
108  * @since 3.4
109  */
110 public class RepoCommand extends GitCommand<RevCommit> {
111 	private String manifestPath;
112 	private String baseUri;
113 	private URI targetUri;
114 	private String groupsParam;
115 	private String branch;
116 	private String targetBranch = Constants.HEAD;
117 	private boolean recordRemoteBranch = false;
118 	private boolean recordSubmoduleLabels = false;
119 	private boolean recordShallowSubmodules = false;
120 	private PersonIdent author;
121 	private RemoteReader callback;
122 	private InputStream inputStream;
123 	private IncludedFileReader includedReader;
124 	private boolean ignoreRemoteFailures = false;
125 
126 	private List<RepoProject> bareProjects;
127 	private Git git;
128 	private ProgressMonitor monitor;
129 
130 	/**
131 	 * A callback to get ref sha1 of a repository from its uri.
132 	 *
133 	 * We provided a default implementation {@link DefaultRemoteReader} to
134 	 * use ls-remote command to read the sha1 from the repository and clone the
135 	 * repository to read the file. Callers may have their own quicker
136 	 * implementation.
137 	 *
138 	 * @since 3.4
139 	 */
140 	public interface RemoteReader {
141 		/**
142 		 * Read a remote ref sha1.
143 		 *
144 		 * @param uri
145 		 *            The URI of the remote repository
146 		 * @param ref
147 		 *            The ref (branch/tag/etc.) to read
148 		 * @return the sha1 of the remote repository, or null if the ref does
149 		 *         not exist.
150 		 * @throws GitAPIException
151 		 */
152 		@Nullable
153 		public ObjectId sha1(String uri, String ref) throws GitAPIException;
154 
155 		/**
156 		 * Read a file from a remote repository.
157 		 *
158 		 * @param uri
159 		 *            The URI of the remote repository
160 		 * @param ref
161 		 *            The ref (branch/tag/etc.) to read
162 		 * @param path
163 		 *            The relative path (inside the repo) to the file to read
164 		 * @return the file content.
165 		 * @throws GitAPIException
166 		 * @throws IOException
167 		 * @since 3.5
168 		 */
169 		public byte[] readFile(String uri, String ref, String path)
170 				throws GitAPIException, IOException;
171 	}
172 
173 	/** A default implementation of {@link RemoteReader} callback. */
174 	public static class DefaultRemoteReader implements RemoteReader {
175 		@Override
176 		public ObjectId sha1(String uri, String ref) throws GitAPIException {
177 			Map<String, Ref> map = Git
178 					.lsRemoteRepository()
179 					.setRemote(uri)
180 					.callAsMap();
181 			Ref r = RefDatabase.findRef(map, ref);
182 			return r != null ? r.getObjectId() : null;
183 		}
184 
185 		@Override
186 		public byte[] readFile(String uri, String ref, String path)
187 				throws GitAPIException, IOException {
188 			File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$
189 			try (Git git = Git.cloneRepository().setBare(true).setDirectory(dir)
190 					.setURI(uri).call()) {
191 				return readFileFromRepo(git.getRepository(), ref, path);
192 			} finally {
193 				FileUtils.delete(dir, FileUtils.RECURSIVE);
194 			}
195 		}
196 
197 		/**
198 		 * Read a file from the repository
199 		 *
200 		 * @param repo
201 		 *            The repository containing the file
202 		 * @param ref
203 		 *            The ref (branch/tag/etc.) to read
204 		 * @param path
205 		 *            The relative path (inside the repo) to the file to read
206 		 * @return the file's content
207 		 * @throws GitAPIException
208 		 * @throws IOException
209 		 * @since 3.5
210 		 */
211 		protected byte[] readFileFromRepo(Repository repo,
212 				String ref, String path) throws GitAPIException, IOException {
213 			try (ObjectReader reader = repo.newObjectReader()) {
214 				ObjectId oid = repo.resolve(ref + ":" + path); //$NON-NLS-1$
215 				return reader.open(oid).getBytes(Integer.MAX_VALUE);
216 			}
217 		}
218 	}
219 
220 	@SuppressWarnings("serial")
221 	private static class ManifestErrorException extends GitAPIException {
222 		ManifestErrorException(Throwable cause) {
223 			super(RepoText.get().invalidManifest, cause);
224 		}
225 	}
226 
227 	@SuppressWarnings("serial")
228 	private static class RemoteUnavailableException extends GitAPIException {
229 		RemoteUnavailableException(String uri) {
230 			super(MessageFormat.format(RepoText.get().errorRemoteUnavailable, uri));
231 		}
232 	}
233 
234 	/**
235 	 * @param repo
236 	 */
237 	public RepoCommand(Repository repo) {
238 		super(repo);
239 	}
240 
241 	/**
242 	 * Set path to the manifest XML file.
243 	 * <p>
244 	 * Calling {@link #setInputStream} will ignore the path set here.
245 	 *
246 	 * @param path
247 	 *            (with <code>/</code> as separator)
248 	 * @return this command
249 	 */
250 	public RepoCommand setPath(String path) {
251 		this.manifestPath = path;
252 		return this;
253 	}
254 
255 	/**
256 	 * Set the input stream to the manifest XML.
257 	 * <p>
258 	 * Setting inputStream will ignore the path set. It will be closed in
259 	 * {@link #call}.
260 	 *
261 	 * @param inputStream
262 	 * @return this command
263 	 * @since 3.5
264 	 */
265 	public RepoCommand setInputStream(InputStream inputStream) {
266 		this.inputStream = inputStream;
267 		return this;
268 	}
269 
270 	/**
271 	 * Set base URI of the paths inside the XML. This is typically the name of
272 	 * the directory holding the manifest repository, eg. for
273 	 * https://android.googlesource.com/platform/manifest, this should be
274 	 * /platform (if you would run this on android.googlesource.com)
275 	 * or https://android.googlesource.com/platform elsewhere.
276 	 *
277 	 * @param uri
278 	 * @return this command
279 	 */
280 	public RepoCommand setURI(String uri) {
281 		this.baseUri = uri;
282 		return this;
283 	}
284 
285 	/**
286 	 * Set the URI of the superproject (this repository), so the .gitmodules
287 	 * file can specify the submodule URLs relative to the superproject.
288 	 *
289 	 * @param uri
290 	 *            the URI of the repository holding the superproject.
291 	 * @return this command
292 	 * @since 4.8
293 	 */
294 	public RepoCommand setTargetURI(String uri) {
295 		// The repo name is interpreted as a directory, for example
296 		// Gerrit (http://gerrit.googlesource.com/gerrit) has a
297 		// .gitmodules referencing ../plugins/hooks, which is
298 		// on http://gerrit.googlesource.com/plugins/hooks,
299 		this.targetUri = URI.create(uri + "/"); //$NON-NLS-1$
300 		return this;
301 	}
302 
303 	/**
304 	 * Set groups to sync
305 	 *
306 	 * @param groups groups separated by comma, examples: default|all|G1,-G2,-G3
307 	 * @return this command
308 	 */
309 	public RepoCommand setGroups(String groups) {
310 		this.groupsParam = groups;
311 		return this;
312 	}
313 
314 	/**
315 	 * Set default branch.
316 	 * <p>
317 	 * This is generally the name of the branch the manifest file was in. If
318 	 * there's no default revision (branch) specified in manifest and no
319 	 * revision specified in project, this branch will be used.
320 	 *
321 	 * @param branch
322 	 * @return this command
323 	 */
324 	public RepoCommand setBranch(String branch) {
325 		this.branch = branch;
326 		return this;
327 	}
328 
329 	/**
330 	 * Set target branch.
331 	 * <p>
332 	 * This is the target branch of the super project to be updated. If not set,
333 	 * default is HEAD.
334 	 * <p>
335 	 * For non-bare repositories, HEAD will always be used and this will be
336 	 * ignored.
337 	 *
338 	 * @param branch
339 	 * @return this command
340 	 * @since 4.1
341 	 */
342 	public RepoCommand setTargetBranch(String branch) {
343 		this.targetBranch = Constants.R_HEADS + branch;
344 		return this;
345 	}
346 
347 	/**
348 	 * Set whether the branch name should be recorded in .gitmodules.
349 	 * <p>
350 	 * Submodule entries in .gitmodules can include a "branch" field
351 	 * to indicate what remote branch each submodule tracks.
352 	 * <p>
353 	 * That field is used by "git submodule update --remote" to update
354 	 * to the tip of the tracked branch when asked and by Gerrit to
355 	 * update the superproject when a change on that branch is merged.
356 	 * <p>
357 	 * Subprojects that request a specific commit or tag will not have
358 	 * a branch name recorded.
359 	 * <p>
360 	 * Not implemented for non-bare repositories.
361 	 *
362 	 * @param enable Whether to record the branch name
363 	 * @return this command
364 	 * @since 4.2
365 	 */
366 	public RepoCommand setRecordRemoteBranch(boolean enable) {
367 		this.recordRemoteBranch = enable;
368 		return this;
369 	}
370 
371 	/**
372 	 * Set whether the labels field should be recorded as a label in
373 	 * .gitattributes.
374 	 * <p>
375 	 * Not implemented for non-bare repositories.
376 	 *
377 	 * @param enable Whether to record the labels in the .gitattributes
378 	 * @return this command
379 	 * @since 4.4
380 	 */
381 	public RepoCommand setRecordSubmoduleLabels(boolean enable) {
382 		this.recordSubmoduleLabels = enable;
383 		return this;
384 	}
385 
386 	/**
387 	 * Set whether the clone-depth field should be recorded as a shallow
388 	 * recommendation in .gitmodules.
389 	 * <p>
390 	 * Not implemented for non-bare repositories.
391 	 *
392 	 * @param enable Whether to record the shallow recommendation.
393 	 * @return this command
394 	 * @since 4.4
395 	 */
396 	public RepoCommand setRecommendShallow(boolean enable) {
397 		this.recordShallowSubmodules = enable;
398 		return this;
399 	}
400 
401 	/**
402 	 * The progress monitor associated with the clone operation. By default,
403 	 * this is set to <code>NullProgressMonitor</code>
404 	 *
405 	 * @see org.eclipse.jgit.lib.NullProgressMonitor
406 	 * @param monitor
407 	 * @return this command
408 	 */
409 	public RepoCommand setProgressMonitor(final ProgressMonitor monitor) {
410 		this.monitor = monitor;
411 		return this;
412 	}
413 
414 	/**
415 	 * Set whether to skip projects whose commits don't exist remotely.
416 	 * <p>
417 	 * When set to true, we'll just skip the manifest entry and continue
418 	 * on to the next one.
419 	 * <p>
420 	 * When set to false (default), we'll throw an error when remote
421 	 * failures occur.
422 	 * <p>
423 	 * Not implemented for non-bare repositories.
424 	 *
425 	 * @param ignore Whether to ignore the remote failures.
426 	 * @return this command
427 	 * @since 4.3
428 	 */
429 	public RepoCommand setIgnoreRemoteFailures(boolean ignore) {
430 		this.ignoreRemoteFailures = ignore;
431 		return this;
432 	}
433 
434 	/**
435 	 * Set the author/committer for the bare repository commit.
436 	 * <p>
437 	 * For non-bare repositories, the current user will be used and this will be
438 	 * ignored.
439 	 *
440 	 * @param author
441 	 * @return this command
442 	 */
443 	public RepoCommand setAuthor(final PersonIdent author) {
444 		this.author = author;
445 		return this;
446 	}
447 
448 	/**
449 	 * Set the GetHeadFromUri callback.
450 	 *
451 	 * This is only used in bare repositories.
452 	 *
453 	 * @param callback
454 	 * @return this command
455 	 */
456 	public RepoCommand setRemoteReader(final RemoteReader callback) {
457 		this.callback = callback;
458 		return this;
459 	}
460 
461 	/**
462 	 * Set the IncludedFileReader callback.
463 	 *
464 	 * @param reader
465 	 * @return this command
466 	 * @since 4.0
467 	 */
468 	public RepoCommand setIncludedFileReader(IncludedFileReader reader) {
469 		this.includedReader = reader;
470 		return this;
471 	}
472 
473 	@Override
474 	public RevCommit call() throws GitAPIException {
475 		try {
476 			checkCallable();
477 			if (baseUri == null) {
478 				baseUri = ""; //$NON-NLS-1$
479 			}
480 			if (inputStream == null) {
481 				if (manifestPath == null || manifestPath.length() == 0)
482 					throw new IllegalArgumentException(
483 							JGitText.get().pathNotConfigured);
484 				try {
485 					inputStream = new FileInputStream(manifestPath);
486 				} catch (IOException e) {
487 					throw new IllegalArgumentException(
488 							JGitText.get().pathNotConfigured);
489 				}
490 			}
491 
492 			if (repo.isBare()) {
493 				bareProjects = new ArrayList<>();
494 				if (author == null)
495 					author = new PersonIdent(repo);
496 				if (callback == null)
497 					callback = new DefaultRemoteReader();
498 			} else
499 				git = new Git(repo);
500 
501 			ManifestParser parser = new ManifestParser(
502 					includedReader, manifestPath, branch, baseUri, groupsParam, repo);
503 			try {
504 				parser.read(inputStream);
505 				for (RepoProject proj : parser.getFilteredProjects()) {
506 					addSubmodule(proj.getUrl(),
507 							proj.getPath(),
508 							proj.getRevision(),
509 							proj.getCopyFiles(),
510 							proj.getLinkFiles(),
511 							proj.getGroups(),
512 							proj.getRecommendShallow());
513 				}
514 			} catch (GitAPIException | IOException e) {
515 				throw new ManifestErrorException(e);
516 			}
517 		} finally {
518 			try {
519 				if (inputStream != null)
520 					inputStream.close();
521 			} catch (IOException e) {
522 				// Just ignore it, it's not important.
523 			}
524 		}
525 
526 		if (repo.isBare()) {
527 			DirCache index = DirCache.newInCore();
528 			DirCacheBuilder builder = index.builder();
529 			ObjectInserter inserter = repo.newObjectInserter();
530 			try (RevWalk rw = new RevWalk(repo)) {
531 				Config cfg = new Config();
532 				StringBuilder attributes = new StringBuilder();
533 				for (RepoProject proj : bareProjects) {
534 					String path = proj.getPath();
535 					String nameUri = proj.getName();
536 					ObjectId objectId;
537 					if (ObjectId.isId(proj.getRevision())
538 							&& !ignoreRemoteFailures) {
539 						objectId = ObjectId.fromString(proj.getRevision());
540 					} else {
541 						objectId = callback.sha1(nameUri, proj.getRevision());
542 						if (objectId == null) {
543 							if (ignoreRemoteFailures) {
544 								continue;
545 							}
546 							throw new RemoteUnavailableException(nameUri);
547 						}
548 						if (recordRemoteBranch) {
549 							// can be branch or tag
550 							cfg.setString("submodule", path, "branch", //$NON-NLS-1$ //$NON-NLS-2$
551 									proj.getRevision());
552 						}
553 
554 						if (recordShallowSubmodules && proj.getRecommendShallow() != null) {
555 							// The shallow recommendation is losing information.
556 							// As the repo manifests stores the recommended
557 							// depth in the 'clone-depth' field, while
558 							// git core only uses a binary 'shallow = true/false'
559 							// hint, we'll map any depth to 'shallow = true'
560 							cfg.setBoolean("submodule", path, "shallow", //$NON-NLS-1$ //$NON-NLS-2$
561 									true);
562 						}
563 					}
564 					if (recordSubmoduleLabels) {
565 						StringBuilder rec = new StringBuilder();
566 						rec.append("/"); //$NON-NLS-1$
567 						rec.append(path);
568 						for (String group : proj.getGroups()) {
569 							rec.append(" "); //$NON-NLS-1$
570 							rec.append(group);
571 						}
572 						rec.append("\n"); //$NON-NLS-1$
573 						attributes.append(rec.toString());
574 					}
575 
576 					URI submodUrl = URI.create(nameUri);
577 					if (targetUri != null) {
578 						submodUrl = relativize(targetUri, submodUrl);
579 					}
580 					cfg.setString("submodule", path, "path", path); //$NON-NLS-1$ //$NON-NLS-2$
581 					cfg.setString("submodule", path, "url", submodUrl.toString()); //$NON-NLS-1$ //$NON-NLS-2$
582 
583 					// create gitlink
584 					DirCacheEntry dcEntry = new DirCacheEntry(path);
585 					dcEntry.setObjectId(objectId);
586 					dcEntry.setFileMode(FileMode.GITLINK);
587 					builder.add(dcEntry);
588 
589 					for (CopyFile copyfile : proj.getCopyFiles()) {
590 						byte[] src = callback.readFile(
591 								nameUri, proj.getRevision(), copyfile.src);
592 						objectId = inserter.insert(Constants.OBJ_BLOB, src);
593 						dcEntry = new DirCacheEntry(copyfile.dest);
594 						dcEntry.setObjectId(objectId);
595 						dcEntry.setFileMode(FileMode.REGULAR_FILE);
596 						builder.add(dcEntry);
597 					}
598 					for (LinkFile linkfile : proj.getLinkFiles()) {
599 						String link;
600 						if (linkfile.dest.contains("/")) { //$NON-NLS-1$
601 							link = FileUtils.relativizeGitPath(
602 									linkfile.dest.substring(0,
603 											linkfile.dest.lastIndexOf('/')),
604 									proj.getPath() + "/" + linkfile.src); //$NON-NLS-1$
605 						} else {
606 							link = proj.getPath() + "/" + linkfile.src; //$NON-NLS-1$
607 						}
608 
609 						objectId = inserter.insert(Constants.OBJ_BLOB,
610 								link.getBytes(
611 										Constants.CHARACTER_ENCODING));
612 						dcEntry = new DirCacheEntry(linkfile.dest);
613 						dcEntry.setObjectId(objectId);
614 						dcEntry.setFileMode(FileMode.SYMLINK);
615 						builder.add(dcEntry);
616 					}
617 				}
618 				String content = cfg.toText();
619 
620 				// create a new DirCacheEntry for .gitmodules file.
621 				final DirCacheEntry dcEntry = new DirCacheEntry(Constants.DOT_GIT_MODULES);
622 				ObjectId objectId = inserter.insert(Constants.OBJ_BLOB,
623 						content.getBytes(Constants.CHARACTER_ENCODING));
624 				dcEntry.setObjectId(objectId);
625 				dcEntry.setFileMode(FileMode.REGULAR_FILE);
626 				builder.add(dcEntry);
627 
628 				if (recordSubmoduleLabels) {
629 					// create a new DirCacheEntry for .gitattributes file.
630 					final DirCacheEntry dcEntryAttr = new DirCacheEntry(Constants.DOT_GIT_ATTRIBUTES);
631 					ObjectId attrId = inserter.insert(Constants.OBJ_BLOB,
632 							attributes.toString().getBytes(Constants.CHARACTER_ENCODING));
633 					dcEntryAttr.setObjectId(attrId);
634 					dcEntryAttr.setFileMode(FileMode.REGULAR_FILE);
635 					builder.add(dcEntryAttr);
636 				}
637 
638 				builder.finish();
639 				ObjectId treeId = index.writeTree(inserter);
640 
641 				// Create a Commit object, populate it and write it
642 				ObjectId headId = repo.resolve(targetBranch + "^{commit}"); //$NON-NLS-1$
643 				CommitBuilder commit = new CommitBuilder();
644 				commit.setTreeId(treeId);
645 				if (headId != null)
646 					commit.setParentIds(headId);
647 				commit.setAuthor(author);
648 				commit.setCommitter(author);
649 				commit.setMessage(RepoText.get().repoCommitMessage);
650 
651 				ObjectId commitId = inserter.insert(commit);
652 				inserter.flush();
653 
654 				RefUpdate ru = repo.updateRef(targetBranch);
655 				ru.setNewObjectId(commitId);
656 				ru.setExpectedOldObjectId(headId != null ? headId : ObjectId.zeroId());
657 				Result rc = ru.update(rw);
658 
659 				switch (rc) {
660 					case NEW:
661 					case FORCED:
662 					case FAST_FORWARD:
663 						// Successful. Do nothing.
664 						break;
665 					case REJECTED:
666 					case LOCK_FAILURE:
667 						throw new ConcurrentRefUpdateException(
668 								MessageFormat.format(
669 										JGitText.get().cannotLock, targetBranch),
670 								ru.getRef(),
671 								rc);
672 					default:
673 						throw new JGitInternalException(MessageFormat.format(
674 								JGitText.get().updatingRefFailed,
675 								targetBranch, commitId.name(), rc));
676 				}
677 
678 				return rw.parseCommit(commitId);
679 			} catch (IOException e) {
680 				throw new ManifestErrorException(e);
681 			}
682 		} else {
683 			return git
684 				.commit()
685 				.setMessage(RepoText.get().repoCommitMessage)
686 				.call();
687 		}
688 	}
689 
690 	private void addSubmodule(String url, String path, String revision,
691 			List<CopyFile> copyfiles, List<LinkFile> linkfiles,
692 			Set<String> groups, String recommendShallow)
693 			throws GitAPIException, IOException {
694 		if (repo.isBare()) {
695 			RepoProject proj = new RepoProject(url, path, revision, null, groups, recommendShallow);
696 			proj.addCopyFiles(copyfiles);
697 			proj.addLinkFiles(linkfiles);
698 			bareProjects.add(proj);
699 		} else {
700 			if (!linkfiles.isEmpty()) {
701 				throw new UnsupportedOperationException(
702 						JGitText.get().nonBareLinkFilesNotSupported);
703 			}
704 
705 			SubmoduleAddCommand add = git
706 				.submoduleAdd()
707 				.setPath(path)
708 				.setURI(url);
709 			if (monitor != null)
710 				add.setProgressMonitor(monitor);
711 
712 			Repository subRepo = add.call();
713 			if (revision != null) {
714 				try (Git sub = new Git(subRepo)) {
715 					sub.checkout().setName(findRef(revision, subRepo))
716 							.call();
717 				}
718 				subRepo.close();
719 				git.add().addFilepattern(path).call();
720 			}
721 			for (CopyFile copyfile : copyfiles) {
722 				copyfile.copy();
723 				git.add().addFilepattern(copyfile.dest).call();
724 			}
725 		}
726 	}
727 
728 	/*
729 	 * Assume we are document "a/b/index.html", what should we put in a href to get to "a/" ?
730 	 * Returns the child if either base or child is not a bare path. This provides a missing feature in
731 	 * java.net.URI (see http://bugs.java.com/view_bug.do?bug_id=6226081).
732 	 */
733 	private static final String SLASH = "/"; //$NON-NLS-1$
734 	static URI relativize(URI current, URI target) {
735 
736 		// We only handle bare paths for now.
737 		if (!target.toString().equals(target.getPath())) {
738 			return target;
739 		}
740 		if (!current.toString().equals(current.getPath())) {
741 			return target;
742 		}
743 
744 		String cur = current.normalize().getPath();
745 		String dest = target.normalize().getPath();
746 
747 		// TODO(hanwen): maybe (absolute, relative) should throw an exception.
748 		if (cur.startsWith(SLASH) != dest.startsWith(SLASH)) {
749 			return target;
750 		}
751 
752 		while (cur.startsWith(SLASH)) {
753 			cur = cur.substring(1);
754 		}
755 		while (dest.startsWith(SLASH)) {
756 			dest = dest.substring(1);
757 		}
758 
759 		if (cur.indexOf('/') == -1 || dest.indexOf('/') == -1) {
760 			// Avoid having to special-casing in the next two ifs.
761 			String prefix = "prefix/"; //$NON-NLS-1$
762 			cur = prefix + cur;
763 			dest = prefix + dest;
764 		}
765 
766 		if (!cur.endsWith(SLASH)) {
767 			// The current file doesn't matter.
768 			int lastSlash = cur.lastIndexOf('/');
769 			cur = cur.substring(0, lastSlash);
770 		}
771 		String destFile = ""; //$NON-NLS-1$
772 		if (!dest.endsWith(SLASH)) {
773 			// We always have to provide the destination file.
774 			int lastSlash = dest.lastIndexOf('/');
775 			destFile = dest.substring(lastSlash + 1, dest.length());
776 			dest = dest.substring(0, dest.lastIndexOf('/'));
777 		}
778 
779 		String[] cs = cur.split(SLASH);
780 		String[] ds = dest.split(SLASH);
781 
782 		int common = 0;
783 		while (common < cs.length && common < ds.length && cs[common].equals(ds[common])) {
784 			common++;
785 		}
786 
787 		StringJoiner j = new StringJoiner(SLASH);
788 		for (int i = common; i < cs.length; i++) {
789 			j.add(".."); //$NON-NLS-1$
790 		}
791 		for (int i = common; i < ds.length; i++) {
792 			j.add(ds[i]);
793 		}
794 
795 		j.add(destFile);
796 		return URI.create(j.toString());
797 	}
798 
799 	private static String findRef(String ref, Repository repo)
800 			throws IOException {
801 		if (!ObjectId.isId(ref)) {
802 			Ref r = repo.exactRef(R_REMOTES + DEFAULT_REMOTE_NAME + "/" + ref); //$NON-NLS-1$
803 			if (r != null)
804 				return r.getName();
805 		}
806 		return ref;
807 	}
808 }