public async Task <ModpackInfo> GetLatestModpackInfo() { if (Repository == null) { return(await DownloadLatestModpackInfo()); } return(await Task.Run(() => { var id = Repository.Resolve("origin/" + Settings.Branch); ObjectReader reader = null; try { reader = Repository.NewObjectReader(); var walk = new RevWalk(reader); var commit = walk.ParseCommit(id); var treeWalk = TreeWalk.ForPath(reader, ModpackInfo.FileName, commit.Tree); if (treeWalk == null) { return null; } byte[] data = reader.Open(treeWalk.GetObjectId(0)).GetBytes(); var modpackJson = Encoding.UTF8.GetString(data); return ModpackInfo.Parse(modpackJson); } finally { if (reader != null) { reader.Release(); } } })); }
internal WalkFetchConnection(WalkTransport t, WalkRemoteObjectDatabase w) { NGit.Transport.Transport wt = (NGit.Transport.Transport)t; local = wt.local; objCheck = wt.IsCheckFetchedObjects() ? new ObjectChecker() : null; inserter = local.NewObjectInserter(); reader = local.NewObjectReader(); remotes = new AList <WalkRemoteObjectDatabase>(); remotes.AddItem(w); unfetchedPacks = new List <WalkFetchConnection.RemotePack>(); packsConsidered = new HashSet <string>(); noPacksYet = new List <WalkRemoteObjectDatabase>(); noPacksYet.AddItem(w); noAlternatesYet = new List <WalkRemoteObjectDatabase>(); noAlternatesYet.AddItem(w); fetchErrors = new Dictionary <ObjectId, IList <Exception> >(); packLocks = new AList <PackLock>(4); revWalk = new RevWalk(reader); revWalk.SetRetainBody(false); treeWalk = new TreeWalk(reader); COMPLETE = revWalk.NewFlag("COMPLETE"); IN_WORK_QUEUE = revWalk.NewFlag("IN_WORK_QUEUE"); LOCALLY_SEEN = revWalk.NewFlag("LOCALLY_SEEN"); localCommitQueue = new DateRevQueue(); workQueue = new List <ObjectId>(); }
/// <summary>Generate and write the bundle to the output stream.</summary> /// <remarks> /// Generate and write the bundle to the output stream. /// <p> /// This method can only be called once per BundleWriter instance. /// </remarks> /// <param name="monitor">progress monitor to report bundle writing status to.</param> /// <param name="os"> /// the stream the bundle is written to. The stream should be /// buffered by the caller. The caller is responsible for closing /// the stream. /// </param> /// <exception cref="System.IO.IOException"> /// an error occurred reading a local object's data to include in /// the bundle, or writing compressed object data to the output /// stream. /// </exception> public virtual void WriteBundle(ProgressMonitor monitor, OutputStream os) { PackConfig pc = packConfig; if (pc == null) { pc = new PackConfig(db); } PackWriter packWriter = new PackWriter(pc, db.NewObjectReader()); try { HashSet <ObjectId> inc = new HashSet <ObjectId>(); HashSet <ObjectId> exc = new HashSet <ObjectId>(); Sharpen.Collections.AddAll(inc, include.Values); foreach (RevCommit r in assume) { exc.AddItem(r.Id); } packWriter.SetDeltaBaseAsOffset(true); packWriter.SetThin(exc.Count > 0); packWriter.SetReuseValidatingObjects(false); if (exc.Count == 0) { packWriter.SetTagTargets(tagTargets); } packWriter.PreparePack(monitor, inc, exc); TextWriter w = new OutputStreamWriter(os, Constants.CHARSET); w.Write(NGit.Transport.TransportBundleConstants.V2_BUNDLE_SIGNATURE); w.Write('\n'); char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH]; foreach (RevCommit a in assume) { w.Write('-'); a.CopyTo(tmp, w); if (a.RawBuffer != null) { w.Write(' '); w.Write(a.GetShortMessage()); } w.Write('\n'); } foreach (KeyValuePair <string, ObjectId> e in include.EntrySet()) { e.Value.CopyTo(tmp, w); w.Write(' '); w.Write(e.Key); w.Write('\n'); } w.Write('\n'); w.Flush(); packWriter.WritePack(monitor, monitor, os); } finally { packWriter.Release(); } }
/// <summary> /// Constructs a NoteMapMerger with custom /// <see cref="NoteMerger">NoteMerger</see> /// and custom /// <see cref="NGit.Merge.MergeStrategy">NGit.Merge.MergeStrategy</see> /// . /// </summary> /// <param name="db">Git repository</param> /// <param name="noteMerger">note merger for merging conflicting changes on a note</param> /// <param name="nonNotesMergeStrategy">merge strategy for merging non-note entries</param> public NoteMapMerger(Repository db, NoteMerger noteMerger, MergeStrategy nonNotesMergeStrategy ) { this.db = db; this.reader = db.NewObjectReader(); this.inserter = db.NewObjectInserter(); this.noteMerger = noteMerger; this.nonNotesMergeStrategy = nonNotesMergeStrategy; this.objectIdPrefix = new MutableObjectId(); }
/// <summary>Open a tree walk and filter to exactly one path.</summary> /// <remarks> /// Open a tree walk and filter to exactly one path. /// <p> /// The returned tree walk is already positioned on the requested path, so /// the caller should not need to invoke /// <see cref="Next()">Next()</see> /// unless they are /// looking for a possible directory/file name conflict. /// </remarks> /// <param name="db">repository to read tree object data from.</param> /// <param name="path">single path to advance the tree walk instance into.</param> /// <param name="trees">one or more trees to walk through, all with the same root.</param> /// <returns> /// a new tree walk configured for exactly this one path; null if no /// path was found in any of the trees. /// </returns> /// <exception cref="System.IO.IOException">reading a pack file or loose object failed. /// </exception> /// <exception cref="NGit.Errors.CorruptObjectException"> /// an tree object could not be read as its data stream did not /// appear to be a tree, or could not be inflated. /// </exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException">an object we expected to be a tree was not a tree. /// </exception> /// <exception cref="NGit.Errors.MissingObjectException">a tree object was not found. /// </exception> public static NGit.Treewalk.TreeWalk ForPath(Repository db, string path, params AnyObjectId [] trees) { NGit.ObjectReader reader = db.NewObjectReader(); try { return(ForPath(reader, path, trees)); } finally { reader.Release(); } }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> private static byte[] Read(Repository db, AnyObjectId blobId) { ObjectReader or = db.NewObjectReader(); try { return(Read(or, blobId)); } finally { or.Release(); } }
/// <summary>Detect renames in the current file set.</summary> /// <remarks>Detect renames in the current file set.</remarks> /// <param name="pm">report progress during the detection phases.</param> /// <returns> /// an unmodifiable list of /// <see cref="DiffEntry">DiffEntry</see> /// s representing all files /// that have been changed. /// </returns> /// <exception cref="System.IO.IOException">file contents cannot be read from the repository. /// </exception> public virtual IList <DiffEntry> Compute(ProgressMonitor pm) { if (!done) { ObjectReader reader = repo.NewObjectReader(); try { return(Compute(reader, pm)); } finally { reader.Release(); } } return(Sharpen.Collections.UnmodifiableList(entries)); }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> private static byte[] Read(Repository db, AnyObjectId treeish, string path) { ObjectReader or = db.NewObjectReader(); try { TreeWalk tree = TreeWalk.ForPath(or, path, AsTree(or, treeish)); if (tree == null) { throw new FileNotFoundException(MessageFormat.Format(JGitText.Get().entryNotFoundByPath , path)); } return(Read(or, tree.GetObjectId(0))); } finally { or.Release(); } }
private static AbstractTreeIterator GetTreeIterator(string name, Repository db) { ObjectId id = db.Resolve(name); if (id == null) { throw new ArgumentException(name); } CanonicalTreeParser p = new CanonicalTreeParser(); ObjectReader or = db.NewObjectReader(); try { p.Reset(or, new RevWalk(db).ParseTree(id)); return(p); } finally { or.Release(); } }
/// <summary>Set the repository the formatter can load object contents from.</summary> /// <remarks> /// Set the repository the formatter can load object contents from. /// Once a repository has been set, the formatter must be released to ensure /// the internal ObjectReader is able to release its resources. /// </remarks> /// <param name="repository">source repository holding referenced objects.</param> public virtual void SetRepository(Repository repository) { if (reader != null) { reader.Release(); } db = repository; reader = db.NewObjectReader(); ContentSource cs = ContentSource.Create(reader); source = new ContentSource.Pair(cs, cs); DiffConfig dc = db.GetConfig().Get(DiffConfig.KEY); if (dc.IsNoPrefix()) { SetOldPrefix(string.Empty); SetNewPrefix(string.Empty); } SetDetectRenames(dc.IsRenameDetectionEnabled()); diffAlgorithm = DiffAlgorithm.GetAlgorithm(db.GetConfig().GetEnum(ConfigConstants .CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_ALGORITHM, DiffAlgorithm.SupportedAlgorithm .HISTOGRAM)); }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> private static byte[] Read(Repository db, AnyObjectId treeish, string path) { ObjectReader or = db.NewObjectReader(); try { TreeWalk tree = TreeWalk.ForPath(or, path, AsTree(or, treeish)); if (tree == null) { throw new FileNotFoundException(MessageFormat.Format(JGitText.Get().entryNotFoundByPath , path)); } return Read(or, tree.GetObjectId(0)); } finally { or.Release(); } }
internal WalkFetchConnection(WalkTransport t, WalkRemoteObjectDatabase w) { NGit.Transport.Transport wt = (NGit.Transport.Transport)t; local = wt.local; objCheck = wt.IsCheckFetchedObjects() ? new ObjectChecker() : null; inserter = local.NewObjectInserter(); reader = local.NewObjectReader(); remotes = new AList<WalkRemoteObjectDatabase>(); remotes.AddItem(w); unfetchedPacks = new List<WalkFetchConnection.RemotePack>(); packsConsidered = new HashSet<string>(); noPacksYet = new List<WalkRemoteObjectDatabase>(); noPacksYet.AddItem(w); noAlternatesYet = new List<WalkRemoteObjectDatabase>(); noAlternatesYet.AddItem(w); fetchErrors = new Dictionary<ObjectId, IList<Exception>>(); packLocks = new AList<PackLock>(4); revWalk = new RevWalk(reader); revWalk.SetRetainBody(false); treeWalk = new TreeWalk(reader); COMPLETE = revWalk.NewFlag("COMPLETE"); IN_WORK_QUEUE = revWalk.NewFlag("IN_WORK_QUEUE"); LOCALLY_SEEN = revWalk.NewFlag("LOCALLY_SEEN"); localCommitQueue = new DateRevQueue(); workQueue = new List<ObjectId>(); }
/// <exception cref="NGit.Errors.TransportException"></exception> private void Sendpack(IList <RemoteRefUpdate> updates, ProgressMonitor monitor) { string pathPack = null; string pathIdx = null; PackWriter writer = new PackWriter(transport.GetPackConfig(), local.NewObjectReader ()); try { ICollection <ObjectId> need = new HashSet <ObjectId>(); ICollection <ObjectId> have = new HashSet <ObjectId>(); foreach (RemoteRefUpdate r in updates) { need.AddItem(r.GetNewObjectId()); } foreach (Ref r_1 in GetRefs()) { have.AddItem(r_1.GetObjectId()); if (r_1.GetPeeledObjectId() != null) { have.AddItem(r_1.GetPeeledObjectId()); } } writer.PreparePack(monitor, need, have); // We don't have to continue further if the pack will // be an empty pack, as the remote has all objects it // needs to complete this change. // if (writer.GetObjectCount() == 0) { return; } packNames = new LinkedHashMap <string, string>(); foreach (string n in dest.GetPackNames()) { packNames.Put(n, n); } string @base = "pack-" + writer.ComputeName().Name; string packName = @base + ".pack"; pathPack = "pack/" + packName; pathIdx = "pack/" + @base + ".idx"; if (Sharpen.Collections.Remove(packNames, packName) != null) { // The remote already contains this pack. We should // remove the index before overwriting to prevent bad // offsets from appearing to clients. // dest.WriteInfoPacks(packNames.Keys); dest.DeleteFile(pathIdx); } // Write the pack file, then the index, as readers look the // other direction (index, then pack file). // string wt = "Put " + Sharpen.Runtime.Substring(@base, 0, 12); OutputStream os = dest.WriteFile(pathPack, monitor, wt + "..pack"); try { os = new SafeBufferedOutputStream(os); writer.WritePack(monitor, monitor, os); } finally { os.Close(); } os = dest.WriteFile(pathIdx, monitor, wt + "..idx"); try { os = new SafeBufferedOutputStream(os); writer.WriteIndex(os); } finally { os.Close(); } // Record the pack at the start of the pack info list. This // way clients are likely to consult the newest pack first, // and discover the most recent objects there. // AList <string> infoPacks = new AList <string>(); infoPacks.AddItem(packName); Sharpen.Collections.AddAll(infoPacks, packNames.Keys); dest.WriteInfoPacks(infoPacks); } catch (IOException err) { SafeDelete(pathIdx); SafeDelete(pathPack); throw new TransportException(uri, JGitText.Get().cannotStoreObjects, err); } finally { writer.Release(); } }
/// <summary>Create a new revision and object walker for a given repository.</summary> /// <remarks>Create a new revision and object walker for a given repository.</remarks> /// <param name="repo">the repository the walker will obtain data from.</param> public ObjectWalk(Repository repo) : this(repo.NewObjectReader()) { }
/// <summary> /// Updates the file in the working tree with content and mode from an entry /// in the index. /// </summary> /// <remarks> /// Updates the file in the working tree with content and mode from an entry /// in the index. The new content is first written to a new temporary file in /// the same directory as the real file. Then that new file is renamed to the /// final filename. Use this method only for checkout of a single entry. /// Otherwise use /// <code>checkoutEntry(Repository, File f, DirCacheEntry, ObjectReader)</code> /// instead which allows to reuse one /// <code>ObjectReader</code> /// for multiple /// entries. /// <p> /// TODO: this method works directly on File IO, we may need another /// abstraction (like WorkingTreeIterator). This way we could tell e.g. /// Eclipse that Files in the workspace got changed /// </p> /// </remarks> /// <param name="repository"></param> /// <param name="f"> /// the file to be modified. The parent directory for this file /// has to exist already /// </param> /// <param name="entry">the entry containing new mode and content</param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> public static void CheckoutEntry(Repository repository, FilePath f, DirCacheEntry entry) { ObjectReader or = repository.NewObjectReader(); try { CheckoutEntry(repository, f, entry, repository.NewObjectReader()); } finally { or.Release(); } }
/// <summary>Create a new tree walker for a given repository.</summary> /// <remarks>Create a new tree walker for a given repository.</remarks> /// <param name="repo">the repository the walker will obtain data from.</param> public TreeWalk(Repository repo) : this(repo.NewObjectReader()) { }
/// <summary>Create a new merge instance for a repository.</summary> /// <remarks>Create a new merge instance for a repository.</remarks> /// <param name="local">the repository this merger will read and write data on.</param> protected internal Merger(Repository local) { db = local; reader = db.NewObjectReader(); walk = new RevWalk(reader); }
/// <summary>Create a new tree walker for a given repository.</summary> /// <remarks>Create a new tree walker for a given repository.</remarks> /// <param name="repo">the repository the walker will obtain data from.</param> public NameConflictTreeWalk(Repository repo) : this(repo.NewObjectReader()) { }
} // End Function GetDiff // https://stackoverflow.com/questions/13537734/how-to-use-jgit-to-get-list-of-changed-files // https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowChangedFilesBetweenCommits.java public static void GetChanges(Git git, Repository repo, RevCommit oldCommit, RevCommit newCommit) { System.Console.WriteLine("Printing diff between commit: " + oldCommit.ToString() + " and " + newCommit.ToString()); ObjectReader reader = repo.NewObjectReader(); // prepare the two iterators to compute the diff between CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); oldTreeIter.Reset(reader, oldCommit.Tree.Id); CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); newTreeIter.Reset(reader, newCommit.Tree.Id); // DiffStatFormatter df = new DiffStatFormatter(newCommit.Name, repo); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { DiffFormatter diffFormatter = new DiffFormatter(ms); diffFormatter.SetRepository(repo); int entryCount = 0; foreach (DiffEntry entry in diffFormatter.Scan(oldCommit, newCommit)) { string pathToUse = null; TreeWalk treeWalk = new TreeWalk(repo); treeWalk.Recursive = true; if (entry.GetChangeType() == DiffEntry.ChangeType.DELETE) { treeWalk.AddTree(oldCommit.Tree); pathToUse = entry.GetOldPath(); } else { treeWalk.AddTree(newCommit.Tree); pathToUse = entry.GetNewPath(); } treeWalk.Filter = PathFilter.Create(pathToUse); if (!treeWalk.Next()) { throw new System.Exception("Did not find expected file '" + pathToUse + "'"); } ObjectId objectId = treeWalk.GetObjectId(0); ObjectLoader loader = repo.Open(objectId); string strModifiedFile = ReadFile(loader); System.Console.WriteLine(strModifiedFile); ////////////// // https://stackoverflow.com/questions/27361538/how-to-show-changes-between-commits-with-jgit diffFormatter.Format(diffFormatter.ToFileHeader(entry)); string diff = GetDiff(repo, entry); System.Console.WriteLine(diff); entryCount++; } // Next entry System.Console.WriteLine(entryCount); ms.Position = 0; using (System.IO.StreamReader sr = new System.IO.StreamReader(ms)) { string strAllDiffs = sr.ReadToEnd(); System.Console.WriteLine(strAllDiffs); } // End Using sr } // End Using ms System.Collections.Generic.IList<DiffEntry> diffs = git.Diff() .SetNewTree(newTreeIter) .SetOldTree(oldTreeIter) .Call(); foreach (DiffEntry entry in diffs) { System.Console.WriteLine("Entry: " + entry); System.Console.WriteLine("Entry: " + entry.GetChangeType()); } // Next entry System.Console.WriteLine("Done"); } // End Sub GetChanges
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> private static byte[] Read(Repository db, AnyObjectId blobId) { ObjectReader or = db.NewObjectReader(); try { return Read(or, blobId); } finally { or.Release(); } }