diff --git a/docs/ko/reference/Commit/Methods/amend.md b/docs/ko/reference/Commit/Methods/amend.md new file mode 100644 index 0000000..d324faa --- /dev/null +++ b/docs/ko/reference/Commit/Methods/amend.md @@ -0,0 +1,121 @@ +# amend + +입력한 값을 사용해서 이 기존 커밋을 수정해요 + +이 메서드는 기존 커밋과 완전히 동일하지만 값들이 업데이트된 새 커밋을 만들어요. 새 커밋은 이전 커밋과 동일한 부모를 가져요. + +## 시그니처 + +```ts +class Commit { + amend(options?: AmendOptions, tree?: Tree): string; +} +``` + +### 파라미터 + + + +### 반환 값 + + \ No newline at end of file diff --git a/docs/reference/Commit/Methods/amend.md b/docs/reference/Commit/Methods/amend.md new file mode 100644 index 0000000..eb6b7b5 --- /dev/null +++ b/docs/reference/Commit/Methods/amend.md @@ -0,0 +1,123 @@ +# amend + +Amend this existing commit with all non-nullable values + +This creates a new commit that is exactly the same as the old commit, +except that any non-nullable values will be updated. The new commit has +the same parents as the old commit. + +## Signature + +```ts +class Commit { + amend(options?: AmendOptions, tree?: Tree): string; +} +``` + +### Parameters + + + +### Returns + + \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index c27342a..8dd323f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -547,6 +547,23 @@ export declare class Commit { * @returns Commit time of a commit. */ time(): Date + /** + * Get the id of the tree pointed to by this commit. + * + * No attempts are made to fetch an object from the ODB. + * + * @category Commit/Methods + * + * @signature + * ```ts + * class Commit { + * treeId(): string; + * } + * ``` + * + * @returns Get the id of the tree pointed to by a commit. + */ + treeId(): string /** * Get the tree pointed to by a commit. * @@ -577,6 +594,27 @@ export declare class Commit { * @returns `GitObject` that casted from this commit. */ asObject(): GitObject + /** + * Amend this existing commit with all non-nullable values + * + * This creates a new commit that is exactly the same as the old commit, + * except that any non-nullable values will be updated. The new commit has + * the same parents as the old commit. + * + * @category Commit/Methods + * + * @signature + * ```ts + * class Commit { + * amend(options?: AmendOptions, tree?: Tree): string; + * } + * ``` + * + * @param {AmendOptions} [options] - Options for amending commit. + * @param {Tree} [tree] - Tree to use for amending commit. + * @returns ID(SHA1) of amended commit. + */ + amend(options?: AmendOptions | undefined | null, tree?: Tree | undefined | null): string /** * Get the author of this commit, using the mailmap to map it to the canonical name and email. * @@ -5254,6 +5292,30 @@ export interface AddMailmapEntryData { replaceEmail: string } +export interface AmendOptions { + /** + * If not NULL, name of the reference that will be updated to point to this commit. + * If the reference is not direct, it will be resolved to a direct reference. + * Use "HEAD" to update the HEAD of the current branch and make it point to this commit. + * + * If the reference doesn't exist yet, it will be created. + * If it does exist, the first parent must be the tip of this branch. + */ + updateRef?: string + /** Signature for author. */ + author?: SignaturePayload + /** Signature for committer. */ + committer?: SignaturePayload + /** Full message for this commit */ + message?: string + /** + * The encoding for the message in the commit, represented with a standard encoding name. + * E.g. "UTF-8". + * If NULL, no encoding header is written and UTF-8 is assumed. + */ + messageEncoding?: string +} + /** * - `Unspecified` : Use the setting from the remote's configuration * - `Auto` : Ask the server for tags pointing to objects we're already downloading diff --git a/src/commit.rs b/src/commit.rs index 4d51122..48c0959 100644 --- a/src/commit.rs +++ b/src/commit.rs @@ -31,6 +31,28 @@ pub struct CommitOptions { pub signature_field: Option, } +#[napi(object)] +#[derive(Default)] +pub struct AmendOptions { + /// If not NULL, name of the reference that will be updated to point to this commit. + /// If the reference is not direct, it will be resolved to a direct reference. + /// Use "HEAD" to update the HEAD of the current branch and make it point to this commit. + /// + /// If the reference doesn't exist yet, it will be created. + /// If it does exist, the first parent must be the tip of this branch. + pub update_ref: Option, + /// Signature for author. + pub author: Option, + /// Signature for committer. + pub committer: Option, + /// Full message for this commit + pub message: Option, + /// The encoding for the message in the commit, represented with a standard encoding name. + /// E.g. "UTF-8". + /// If NULL, no encoding header is written and UTF-8 is assumed. + pub message_encoding: Option, +} + pub(crate) enum CommitInner { Repo(SharedReference>), Owned(git2::Commit<'static>), @@ -246,6 +268,50 @@ impl Commit { inner: ObjectInner::Owned(obj), } } + + #[napi] + /// Amend this existing commit with all non-nullable values + /// + /// This creates a new commit that is exactly the same as the old commit, + /// except that any non-nullable values will be updated. The new commit has + /// the same parents as the old commit. + /// + /// @category Commit/Methods + /// + /// @signature + /// ```ts + /// class Commit { + /// amend(options?: AmendOptions, tree?: Tree): string; + /// } + /// ``` + /// + /// @param {AmendOptions} [options] - Options for amending commit. + /// @param {Tree} [tree] - Tree to use for amending commit. + /// @returns ID(SHA1) of amended commit. + pub fn amend(&self, options: Option, tree: Option<&Tree>) -> crate::Result { + let opts = options.unwrap_or_default(); + let update_ref = opts.update_ref; + let author = opts + .author + .and_then(|x| Signature::try_from(x).ok()) + .and_then(|x| git2::Signature::try_from(x).ok()); + let committer = opts + .committer + .and_then(|x| Signature::try_from(x).ok()) + .and_then(|x| git2::Signature::try_from(x).ok()); + let message = opts.message; + let message_encoding = opts.message_encoding; + + let oid = self.inner.amend( + update_ref.as_deref(), + author.as_ref(), + committer.as_ref(), + message_encoding.as_deref(), + message.as_deref(), + tree.map(|x| x.inner.deref()), + )?; + Ok(oid.to_string()) + } } #[napi]