import 'dart:ffi'; import 'package:ffi/ffi.dart'; import '../error.dart'; import '../util.dart'; import 'libgit2_bindings.dart'; /// Returns list of notes for repository. /// /// Notes must be freed manually by the user. /// /// Throws a [LibGit2Error] if error occured. List> list(Pointer repo) { final notesRef = 'refs/notes/commits'.toNativeUtf8().cast(); final iterator = calloc>(); final iteratorError = libgit2.git_note_iterator_new(iterator, repo, notesRef); if (iteratorError < 0) { throw LibGit2Error(libgit2.git_error_last()); } var result = >[]; var nextError = 0; while (nextError >= 0) { final noteId = calloc(); var annotatedId = calloc(); nextError = libgit2.git_note_next(noteId, annotatedId, iterator.value); if (nextError >= 0) { final out = calloc>(); final error = libgit2.git_note_read(out, repo, notesRef, annotatedId); calloc.free(noteId); if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); } else { result.add({'note': out.value, 'annotatedId': annotatedId}); } } else { break; } } calloc.free(notesRef); libgit2.git_note_iterator_free(iterator.value); return result; } /// Read the note for an object. /// /// The note must be freed manually by the user. /// /// Throws a [LibGit2Error] if error occured. Pointer lookup({ required Pointer repoPointer, required Pointer oidPointer, String notesRef = 'refs/notes/commits', }) { final out = calloc>(); final notesRefC = notesRef.toNativeUtf8().cast(); final error = libgit2.git_note_read(out, repoPointer, notesRefC, oidPointer); calloc.free(notesRefC); if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; } } /// Add a note for an object. /// /// Throws a [LibGit2Error] if error occured. Pointer create({ required Pointer repoPointer, String notesRef = 'refs/notes/commits', required Pointer authorPointer, required Pointer committerPointer, required Pointer oidPointer, required String note, bool force = false, }) { final out = calloc(); final notesRefC = notesRef.toNativeUtf8().cast(); final noteC = note.toNativeUtf8().cast(); final forceC = force ? 1 : 0; final error = libgit2.git_note_create( out, repoPointer, notesRefC, authorPointer, committerPointer, oidPointer, noteC, forceC, ); calloc.free(notesRefC); calloc.free(noteC); if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); } else { return out; } } /// Delete the note for an object. /// /// Throws a [LibGit2Error] if error occured. void delete({ required Pointer repoPointer, String notesRef = 'refs/notes/commits', required Pointer authorPointer, required Pointer committerPointer, required Pointer oidPointer, }) { final notesRefC = notesRef.toNativeUtf8().cast(); final error = libgit2.git_note_remove( repoPointer, notesRefC, authorPointer, committerPointer, oidPointer, ); calloc.free(notesRefC); if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); } } /// Get the note object's id. Pointer id(Pointer note) => libgit2.git_note_id(note); /// Get the note message. String message(Pointer note) { return libgit2.git_note_message(note).cast().toDartString(); } /// Free memory allocated for note object. void free(Pointer note) => libgit2.git_note_free(note);