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.Objects;
58  import java.util.Set;
59  import java.util.StringJoiner;
60  
61  import org.eclipse.jgit.annotations.Nullable;
62  import org.eclipse.jgit.api.Git;
63  import org.eclipse.jgit.api.GitCommand;
64  import org.eclipse.jgit.api.SubmoduleAddCommand;
65  import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
66  import org.eclipse.jgit.api.errors.GitAPIException;
67  import org.eclipse.jgit.api.errors.JGitInternalException;
68  import org.eclipse.jgit.dircache.DirCache;
69  import org.eclipse.jgit.dircache.DirCacheBuilder;
70  import org.eclipse.jgit.dircache.DirCacheEntry;
71  import org.eclipse.jgit.gitrepo.ManifestParser.IncludedFileReader;
72  import org.eclipse.jgit.gitrepo.RepoProject.CopyFile;
73  import org.eclipse.jgit.gitrepo.RepoProject.LinkFile;
74  import org.eclipse.jgit.gitrepo.internal.RepoText;
75  import org.eclipse.jgit.internal.JGitText;
76  import org.eclipse.jgit.lib.CommitBuilder;
77  import org.eclipse.jgit.lib.Config;
78  import org.eclipse.jgit.lib.Constants;
79  import org.eclipse.jgit.lib.FileMode;
80  import org.eclipse.jgit.lib.ObjectId;
81  import org.eclipse.jgit.lib.ObjectInserter;
82  import org.eclipse.jgit.lib.ObjectReader;
83  import org.eclipse.jgit.lib.PersonIdent;
84  import org.eclipse.jgit.lib.ProgressMonitor;
85  import org.eclipse.jgit.lib.Ref;
86  import org.eclipse.jgit.lib.RefDatabase;
87  import org.eclipse.jgit.lib.RefUpdate;
88  import org.eclipse.jgit.lib.RefUpdate.Result;
89  import org.eclipse.jgit.lib.Repository;
90  import org.eclipse.jgit.revwalk.RevCommit;
91  import org.eclipse.jgit.revwalk.RevWalk;
92  import org.eclipse.jgit.util.FileUtils;
93  
94  /**
95   * A class used to execute a repo command.
96   *
97   * This will parse a repo XML manifest, convert it into .gitmodules file and the
98   * repository config file.
99   *
100  * If called against a bare repository, it will replace all the existing content
101  * of the repository with the contents populated from the manifest.
102  *
103  * repo manifest allows projects overlapping, e.g. one project's manifestPath is
104  * "foo" and another project's manifestPath is "foo/bar". This won't
105  * work in git submodule, so we'll skip all the sub projects
106  * ("foo/bar" in the example) while converting.
107  *
108  * @see <a href="https://code.google.com/p/git-repo/">git-repo project page</a>
109  * @since 3.4
110  */
111 public class RepoCommand extends GitCommand<RevCommit> {
112 	private String manifestPath;
113 	private String baseUri;
114 	private URI targetUri;
115 	private String groupsParam;
116 	private String branch;
117 	private String targetBranch = Constants.HEAD;
118 	private boolean recordRemoteBranch = false;
119 	private boolean recordSubmoduleLabels = false;
120 	private boolean recordShallowSubmodules = false;
121 	private PersonIdent author;
122 	private RemoteReader callback;
123 	private InputStream inputStream;
124 	private IncludedFileReader includedReader;
125 	private boolean ignoreRemoteFailures = false;
126 
127 	private List<RepoProject> bareProjects;
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 	 * Constructor for RepoCommand
236 	 *
237 	 * @param repo
238 	 *            the {@link org.eclipse.jgit.lib.Repository}
239 	 */
240 	public RepoCommand(Repository repo) {
241 		super(repo);
242 	}
243 
244 	/**
245 	 * Set path to the manifest XML file.
246 	 * <p>
247 	 * Calling {@link #setInputStream} will ignore the path set here.
248 	 *
249 	 * @param path
250 	 *            (with <code>/</code> as separator)
251 	 * @return this command
252 	 */
253 	public RepoCommand setPath(String path) {
254 		this.manifestPath = path;
255 		return this;
256 	}
257 
258 	/**
259 	 * Set the input stream to the manifest XML.
260 	 * <p>
261 	 * Setting inputStream will ignore the path set. It will be closed in
262 	 * {@link #call}.
263 	 *
264 	 * @param inputStream a {@link java.io.InputStream} object.
265 	 * @return this command
266 	 * @since 3.5
267 	 */
268 	public RepoCommand setInputStream(InputStream inputStream) {
269 		this.inputStream = inputStream;
270 		return this;
271 	}
272 
273 	/**
274 	 * Set base URI of the paths inside the XML. This is typically the name of
275 	 * the directory holding the manifest repository, eg. for
276 	 * https://android.googlesource.com/platform/manifest, this should be
277 	 * /platform (if you would run this on android.googlesource.com) or
278 	 * https://android.googlesource.com/platform elsewhere.
279 	 *
280 	 * @param uri
281 	 *            the base URI
282 	 * @return this command
283 	 */
284 	public RepoCommand setURI(String uri) {
285 		this.baseUri = uri;
286 		return this;
287 	}
288 
289 	/**
290 	 * Set the URI of the superproject (this repository), so the .gitmodules
291 	 * file can specify the submodule URLs relative to the superproject.
292 	 *
293 	 * @param uri
294 	 *            the URI of the repository holding the superproject.
295 	 * @return this command
296 	 * @since 4.8
297 	 */
298 	public RepoCommand setTargetURI(String uri) {
299 		// The repo name is interpreted as a directory, for example
300 		// Gerrit (http://gerrit.googlesource.com/gerrit) has a
301 		// .gitmodules referencing ../plugins/hooks, which is
302 		// on http://gerrit.googlesource.com/plugins/hooks,
303 		this.targetUri = URI.create(uri + "/"); //$NON-NLS-1$
304 		return this;
305 	}
306 
307 	/**
308 	 * Set groups to sync
309 	 *
310 	 * @param groups groups separated by comma, examples: default|all|G1,-G2,-G3
311 	 * @return this command
312 	 */
313 	public RepoCommand setGroups(String groups) {
314 		this.groupsParam = groups;
315 		return this;
316 	}
317 
318 	/**
319 	 * Set default branch.
320 	 * <p>
321 	 * This is generally the name of the branch the manifest file was in. If
322 	 * there's no default revision (branch) specified in manifest and no
323 	 * revision specified in project, this branch will be used.
324 	 *
325 	 * @param branch
326 	 *            a branch name
327 	 * @return this command
328 	 */
329 	public RepoCommand setBranch(String branch) {
330 		this.branch = branch;
331 		return this;
332 	}
333 
334 	/**
335 	 * Set target branch.
336 	 * <p>
337 	 * This is the target branch of the super project to be updated. If not set,
338 	 * default is HEAD.
339 	 * <p>
340 	 * For non-bare repositories, HEAD will always be used and this will be
341 	 * ignored.
342 	 *
343 	 * @param branch
344 	 *            branch name
345 	 * @return this command
346 	 * @since 4.1
347 	 */
348 	public RepoCommand setTargetBranch(String branch) {
349 		this.targetBranch = Constants.R_HEADS + branch;
350 		return this;
351 	}
352 
353 	/**
354 	 * Set whether the branch name should be recorded in .gitmodules.
355 	 * <p>
356 	 * Submodule entries in .gitmodules can include a "branch" field
357 	 * to indicate what remote branch each submodule tracks.
358 	 * <p>
359 	 * That field is used by "git submodule update --remote" to update
360 	 * to the tip of the tracked branch when asked and by Gerrit to
361 	 * update the superproject when a change on that branch is merged.
362 	 * <p>
363 	 * Subprojects that request a specific commit or tag will not have
364 	 * a branch name recorded.
365 	 * <p>
366 	 * Not implemented for non-bare repositories.
367 	 *
368 	 * @param enable Whether to record the branch name
369 	 * @return this command
370 	 * @since 4.2
371 	 */
372 	public RepoCommand setRecordRemoteBranch(boolean enable) {
373 		this.recordRemoteBranch = enable;
374 		return this;
375 	}
376 
377 	/**
378 	 * Set whether the labels field should be recorded as a label in
379 	 * .gitattributes.
380 	 * <p>
381 	 * Not implemented for non-bare repositories.
382 	 *
383 	 * @param enable Whether to record the labels in the .gitattributes
384 	 * @return this command
385 	 * @since 4.4
386 	 */
387 	public RepoCommand setRecordSubmoduleLabels(boolean enable) {
388 		this.recordSubmoduleLabels = enable;
389 		return this;
390 	}
391 
392 	/**
393 	 * Set whether the clone-depth field should be recorded as a shallow
394 	 * recommendation in .gitmodules.
395 	 * <p>
396 	 * Not implemented for non-bare repositories.
397 	 *
398 	 * @param enable Whether to record the shallow recommendation.
399 	 * @return this command
400 	 * @since 4.4
401 	 */
402 	public RepoCommand setRecommendShallow(boolean enable) {
403 		this.recordShallowSubmodules = enable;
404 		return this;
405 	}
406 
407 	/**
408 	 * The progress monitor associated with the clone operation. By default,
409 	 * this is set to <code>NullProgressMonitor</code>
410 	 *
411 	 * @see org.eclipse.jgit.lib.NullProgressMonitor
412 	 * @param monitor
413 	 *            a {@link org.eclipse.jgit.lib.ProgressMonitor}
414 	 * @return this command
415 	 */
416 	public RepoCommand setProgressMonitor(ProgressMonitor monitor) {
417 		this.monitor = monitor;
418 		return this;
419 	}
420 
421 	/**
422 	 * Set whether to skip projects whose commits don't exist remotely.
423 	 * <p>
424 	 * When set to true, we'll just skip the manifest entry and continue
425 	 * on to the next one.
426 	 * <p>
427 	 * When set to false (default), we'll throw an error when remote
428 	 * failures occur.
429 	 * <p>
430 	 * Not implemented for non-bare repositories.
431 	 *
432 	 * @param ignore Whether to ignore the remote failures.
433 	 * @return this command
434 	 * @since 4.3
435 	 */
436 	public RepoCommand setIgnoreRemoteFailures(boolean ignore) {
437 		this.ignoreRemoteFailures = ignore;
438 		return this;
439 	}
440 
441 	/**
442 	 * Set the author/committer for the bare repository commit.
443 	 * <p>
444 	 * For non-bare repositories, the current user will be used and this will be
445 	 * ignored.
446 	 *
447 	 * @param author
448 	 *            the author's {@link org.eclipse.jgit.lib.PersonIdent}
449 	 * @return this command
450 	 */
451 	public RepoCommand setAuthor(PersonIdent author) {
452 		this.author = author;
453 		return this;
454 	}
455 
456 	/**
457 	 * Set the GetHeadFromUri callback.
458 	 *
459 	 * This is only used in bare repositories.
460 	 *
461 	 * @param callback
462 	 *            a {@link org.eclipse.jgit.gitrepo.RepoCommand.RemoteReader}
463 	 *            object.
464 	 * @return this command
465 	 */
466 	public RepoCommand setRemoteReader(RemoteReader callback) {
467 		this.callback = callback;
468 		return this;
469 	}
470 
471 	/**
472 	 * Set the IncludedFileReader callback.
473 	 *
474 	 * @param reader
475 	 *            a
476 	 *            {@link org.eclipse.jgit.gitrepo.ManifestParser.IncludedFileReader}
477 	 *            object.
478 	 * @return this command
479 	 * @since 4.0
480 	 */
481 	public RepoCommand setIncludedFileReader(IncludedFileReader reader) {
482 		this.includedReader = reader;
483 		return this;
484 	}
485 
486 	/** {@inheritDoc} */
487 	@Override
488 	public RevCommit call() throws GitAPIException {
489 		checkCallable();
490 		if (baseUri == null) {
491 			baseUri = ""; //$NON-NLS-1$
492 		}
493 		if (inputStream == null) {
494 			if (manifestPath == null || manifestPath.length() == 0)
495 				throw new IllegalArgumentException(
496 						JGitText.get().pathNotConfigured);
497 			try {
498 				inputStream = new FileInputStream(manifestPath);
499 			} catch (IOException e) {
500 				throw new IllegalArgumentException(
501 						JGitText.get().pathNotConfigured);
502 			}
503 		}
504 
505 		List<RepoProject> filteredProjects;
506 		try {
507 			ManifestParser parser = new ManifestParser(includedReader,
508 					manifestPath, branch, baseUri, groupsParam, repo);
509 			parser.read(inputStream);
510 			filteredProjects = parser.getFilteredProjects();
511 		} catch (IOException e) {
512 			throw new ManifestErrorException(e);
513 		} finally {
514 			try {
515 				inputStream.close();
516 			} catch (IOException e) {
517 				// Just ignore it, it's not important.
518 			}
519 		}
520 
521 		if (repo.isBare()) {
522 			bareProjects = new ArrayList<>();
523 			if (author == null)
524 				author = new PersonIdent(repo);
525 			if (callback == null)
526 				callback = new DefaultRemoteReader();
527 			for (RepoProject proj : filteredProjects) {
528 				addSubmoduleBare(proj.getUrl(), proj.getPath(),
529 						proj.getRevision(), proj.getCopyFiles(),
530 						proj.getLinkFiles(), proj.getGroups(),
531 						proj.getRecommendShallow());
532 			}
533 			DirCache index = DirCache.newInCore();
534 			DirCacheBuilder builder = index.builder();
535 			ObjectInserter inserter = repo.newObjectInserter();
536 			try (RevWalk rw = new RevWalk(repo)) {
537 				Config cfg = new Config();
538 				StringBuilder attributes = new StringBuilder();
539 				for (RepoProject proj : bareProjects) {
540 					String path = proj.getPath();
541 					String nameUri = proj.getName();
542 					ObjectId objectId;
543 					if (ObjectId.isId(proj.getRevision())) {
544 						objectId = ObjectId.fromString(proj.getRevision());
545 					} else {
546 						objectId = callback.sha1(nameUri, proj.getRevision());
547 						if (objectId == null && !ignoreRemoteFailures) {
548 							throw new RemoteUnavailableException(nameUri);
549 						}
550 						if (recordRemoteBranch) {
551 							// can be branch or tag
552 							cfg.setString("submodule", path, "branch", //$NON-NLS-1$ //$NON-NLS-2$
553 									proj.getRevision());
554 						}
555 
556 						if (recordShallowSubmodules && proj.getRecommendShallow() != null) {
557 							// The shallow recommendation is losing information.
558 							// As the repo manifests stores the recommended
559 							// depth in the 'clone-depth' field, while
560 							// git core only uses a binary 'shallow = true/false'
561 							// hint, we'll map any depth to 'shallow = true'
562 							cfg.setBoolean("submodule", path, "shallow", //$NON-NLS-1$ //$NON-NLS-2$
563 									true);
564 						}
565 					}
566 					if (recordSubmoduleLabels) {
567 						StringBuilder rec = new StringBuilder();
568 						rec.append("/"); //$NON-NLS-1$
569 						rec.append(path);
570 						for (String group : proj.getGroups()) {
571 							rec.append(" "); //$NON-NLS-1$
572 							rec.append(group);
573 						}
574 						rec.append("\n"); //$NON-NLS-1$
575 						attributes.append(rec.toString());
576 					}
577 
578 					URI submodUrl = URI.create(nameUri);
579 					if (targetUri != null) {
580 						submodUrl = relativize(targetUri, submodUrl);
581 					}
582 					cfg.setString("submodule", path, "path", path); //$NON-NLS-1$ //$NON-NLS-2$
583 					cfg.setString("submodule", path, "url", submodUrl.toString()); //$NON-NLS-1$ //$NON-NLS-2$
584 
585 					// create gitlink
586 					if (objectId != null) {
587 						DirCacheEntry dcEntry = new DirCacheEntry(path);
588 						dcEntry.setObjectId(objectId);
589 						dcEntry.setFileMode(FileMode.GITLINK);
590 						builder.add(dcEntry);
591 
592 						for (CopyFile copyfile : proj.getCopyFiles()) {
593 							byte[] src = callback.readFile(
594 								nameUri, proj.getRevision(), copyfile.src);
595 							objectId = inserter.insert(Constants.OBJ_BLOB, src);
596 							dcEntry = new DirCacheEntry(copyfile.dest);
597 							dcEntry.setObjectId(objectId);
598 							dcEntry.setFileMode(FileMode.REGULAR_FILE);
599 							builder.add(dcEntry);
600 						}
601 						for (LinkFile linkfile : proj.getLinkFiles()) {
602 							String link;
603 							if (linkfile.dest.contains("/")) { //$NON-NLS-1$
604 								link = FileUtils.relativizeGitPath(
605 									linkfile.dest.substring(0,
606 										linkfile.dest.lastIndexOf('/')),
607 									proj.getPath() + "/" + linkfile.src); //$NON-NLS-1$
608 							} else {
609 								link = proj.getPath() + "/" + linkfile.src; //$NON-NLS-1$
610 							}
611 
612 							objectId = inserter.insert(Constants.OBJ_BLOB,
613 								link.getBytes(
614 									Constants.CHARACTER_ENCODING));
615 							dcEntry = new DirCacheEntry(linkfile.dest);
616 							dcEntry.setObjectId(objectId);
617 							dcEntry.setFileMode(FileMode.SYMLINK);
618 							builder.add(dcEntry);
619 						}
620 					}
621 				}
622 				String content = cfg.toText();
623 
624 				// create a new DirCacheEntry for .gitmodules file.
625 				final DirCacheEntry dcEntry = new DirCacheEntry(Constants.DOT_GIT_MODULES);
626 				ObjectId objectId = inserter.insert(Constants.OBJ_BLOB,
627 						content.getBytes(Constants.CHARACTER_ENCODING));
628 				dcEntry.setObjectId(objectId);
629 				dcEntry.setFileMode(FileMode.REGULAR_FILE);
630 				builder.add(dcEntry);
631 
632 				if (recordSubmoduleLabels) {
633 					// create a new DirCacheEntry for .gitattributes file.
634 					final DirCacheEntry dcEntryAttr = new DirCacheEntry(Constants.DOT_GIT_ATTRIBUTES);
635 					ObjectId attrId = inserter.insert(Constants.OBJ_BLOB,
636 							attributes.toString().getBytes(Constants.CHARACTER_ENCODING));
637 					dcEntryAttr.setObjectId(attrId);
638 					dcEntryAttr.setFileMode(FileMode.REGULAR_FILE);
639 					builder.add(dcEntryAttr);
640 				}
641 
642 				builder.finish();
643 				ObjectId treeId = index.writeTree(inserter);
644 
645 				// Create a Commit object, populate it and write it
646 				ObjectId headId = repo.resolve(targetBranch + "^{commit}"); //$NON-NLS-1$
647 				if (headId != null && rw.parseCommit(headId).getTree().getId().equals(treeId)) {
648 					// No change. Do nothing.
649 					return rw.parseCommit(headId);
650 				}
651 
652 				CommitBuilder commit = new CommitBuilder();
653 				commit.setTreeId(treeId);
654 				if (headId != null)
655 					commit.setParentIds(headId);
656 				commit.setAuthor(author);
657 				commit.setCommitter(author);
658 				commit.setMessage(RepoText.get().repoCommitMessage);
659 
660 				ObjectId commitId = inserter.insert(commit);
661 				inserter.flush();
662 
663 				RefUpdate ru = repo.updateRef(targetBranch);
664 				ru.setNewObjectId(commitId);
665 				ru.setExpectedOldObjectId(headId != null ? headId : ObjectId.zeroId());
666 				Result rc = ru.update(rw);
667 
668 				switch (rc) {
669 					case NEW:
670 					case FORCED:
671 					case FAST_FORWARD:
672 						// Successful. Do nothing.
673 						break;
674 					case REJECTED:
675 					case LOCK_FAILURE:
676 						throw new ConcurrentRefUpdateException(
677 								MessageFormat.format(
678 										JGitText.get().cannotLock, targetBranch),
679 								ru.getRef(),
680 								rc);
681 					default:
682 						throw new JGitInternalException(MessageFormat.format(
683 								JGitText.get().updatingRefFailed,
684 								targetBranch, commitId.name(), rc));
685 				}
686 
687 				return rw.parseCommit(commitId);
688 			} catch (GitAPIException | IOException e) {
689 				throw new ManifestErrorException(e);
690 			}
691 		} else {
692 			try (Git git = new Git(repo)) {
693 				for (RepoProject proj : filteredProjects) {
694 					addSubmodule(proj.getUrl(), proj.getPath(),
695 							proj.getRevision(), proj.getCopyFiles(),
696 							proj.getLinkFiles(), git);
697 				}
698 				return git.commit().setMessage(RepoText.get().repoCommitMessage)
699 						.call();
700 			} catch (GitAPIException | IOException e) {
701 				throw new ManifestErrorException(e);
702 			}
703 		}
704 	}
705 
706 	private void addSubmodule(String url, String path, String revision,
707 			List<CopyFile> copyfiles, List<LinkFile> linkfiles, Git git)
708 			throws GitAPIException, IOException {
709 		assert (!repo.isBare());
710 		assert (git != null);
711 		if (!linkfiles.isEmpty()) {
712 			throw new UnsupportedOperationException(
713 					JGitText.get().nonBareLinkFilesNotSupported);
714 		}
715 
716 		SubmoduleAddCommand add = git.submoduleAdd().setPath(path).setURI(url);
717 		if (monitor != null)
718 			add.setProgressMonitor(monitor);
719 
720 		Repository subRepo = add.call();
721 		if (revision != null) {
722 			try (Git sub = new Git(subRepo)) {
723 				sub.checkout().setName(findRef(revision, subRepo)).call();
724 			}
725 			subRepo.close();
726 			git.add().addFilepattern(path).call();
727 		}
728 		for (CopyFile copyfile : copyfiles) {
729 			copyfile.copy();
730 			git.add().addFilepattern(copyfile.dest).call();
731 		}
732 	}
733 
734 	private void addSubmoduleBare(String url, String path, String revision,
735 			List<CopyFile> copyfiles, List<LinkFile> linkfiles,
736 			Set<String> groups, String recommendShallow) {
737 		assert (repo.isBare());
738 		assert (bareProjects != null);
739 		RepoProject proj = new RepoProject(url, path, revision, null, groups,
740 				recommendShallow);
741 		proj.addCopyFiles(copyfiles);
742 		proj.addLinkFiles(linkfiles);
743 		bareProjects.add(proj);
744 	}
745 
746 	/*
747 	 * Assume we are document "a/b/index.html", what should we put in a href to get to "a/" ?
748 	 * Returns the child if either base or child is not a bare path. This provides a missing feature in
749 	 * java.net.URI (see http://bugs.java.com/view_bug.do?bug_id=6226081).
750 	 */
751 	private static final String SLASH = "/"; //$NON-NLS-1$
752 	static URI relativize(URI current, URI target) {
753 		if (!Objects.equals(current.getHost(), target.getHost())) {
754 			return target;
755 		}
756 
757 		String cur = current.normalize().getPath();
758 		String dest = target.normalize().getPath();
759 
760 		// TODO(hanwen): maybe (absolute, relative) should throw an exception.
761 		if (cur.startsWith(SLASH) != dest.startsWith(SLASH)) {
762 			return target;
763 		}
764 
765 		while (cur.startsWith(SLASH)) {
766 			cur = cur.substring(1);
767 		}
768 		while (dest.startsWith(SLASH)) {
769 			dest = dest.substring(1);
770 		}
771 
772 		if (cur.indexOf('/') == -1 || dest.indexOf('/') == -1) {
773 			// Avoid having to special-casing in the next two ifs.
774 			String prefix = "prefix/"; //$NON-NLS-1$
775 			cur = prefix + cur;
776 			dest = prefix + dest;
777 		}
778 
779 		if (!cur.endsWith(SLASH)) {
780 			// The current file doesn't matter.
781 			int lastSlash = cur.lastIndexOf('/');
782 			cur = cur.substring(0, lastSlash);
783 		}
784 		String destFile = ""; //$NON-NLS-1$
785 		if (!dest.endsWith(SLASH)) {
786 			// We always have to provide the destination file.
787 			int lastSlash = dest.lastIndexOf('/');
788 			destFile = dest.substring(lastSlash + 1, dest.length());
789 			dest = dest.substring(0, dest.lastIndexOf('/'));
790 		}
791 
792 		String[] cs = cur.split(SLASH);
793 		String[] ds = dest.split(SLASH);
794 
795 		int common = 0;
796 		while (common < cs.length && common < ds.length && cs[common].equals(ds[common])) {
797 			common++;
798 		}
799 
800 		StringJoiner j = new StringJoiner(SLASH);
801 		for (int i = common; i < cs.length; i++) {
802 			j.add(".."); //$NON-NLS-1$
803 		}
804 		for (int i = common; i < ds.length; i++) {
805 			j.add(ds[i]);
806 		}
807 
808 		j.add(destFile);
809 		return URI.create(j.toString());
810 	}
811 
812 	private static String findRef(String ref, Repository repo)
813 			throws IOException {
814 		if (!ObjectId.isId(ref)) {
815 			Ref r = repo.exactRef(R_REMOTES + DEFAULT_REMOTE_NAME + "/" + ref); //$NON-NLS-1$
816 			if (r != null)
817 				return r.getName();
818 		}
819 		return ref;
820 	}
821 }