Repository: isomorphic-git/isomorphic-git
Pull request #814 (2019-08-21 21:13:35)

feat: Added 'tree' parameter to commit command

## I'm adding a parameter to an existing command:

- [x] if this is your first time contributing, run `npm run add-contributor` and follow the prompts to add yourself to the README
- [x] add parameter to the function in `src/commands/X.js`
- [x] document the parameter in the JSDoc comment above the function
- [x] add parameter to the TypeScript library definition for X in `src/index.d.ts`
- [x] add a test case in `__tests__/test-X.js` if possible
- [ ] squash merge the PR with commit message "feat: Added 'bar' parameter to X command"


isomorphic-git-bot[bot]: Thank you wmhilton! I auto-formatted the code using [prettier-standard](https://github.com/sheerun/prettier-standard). :robot:
karma-pr-reporter: ## Test Results for 9e8c33c1f3080d1644ebdd2e8af53ad8ecf2fb6a:
| Browser | Passed | Skipped | Failed | Time | Disconnected | Error |
|---------|--------|---------|--------|------|--------------|-------|
| HeadlessChrome 0.0.0 (Linux 0.0.0) | 270 | 1 | 0 | 1 min 28.087 secs | false | undefined |
| Firefox 68.0.0 (Ubuntu 0.0.0) | 270 | 1 | 0 | 2 mins 5.335 secs | false | undefined |
| Safari 11.1.2 (Mac OS X 10.13.6) | 270 | 1 | 0 | 3 mins 52.889 secs | false | undefined |
| Chrome Mobile 67.0.3396 (Android 7.1.1) | 270 | 1 | 0 | 4 mins 14.754 secs | false | undefined |
| Mobile Safari 11.0.0 (iOS 11.2.0) | 270 | 1 | 0 | 5 mins 54.326 secs | false | undefined |
| Edge 17.17134.0 (Windows 10 0.0.0) | 270 | 1 | 0 | 6 mins 25.641 secs | false | undefined |

Patch:
diff --git a/src/commands/commit.js b/src/commands/commit.js
index 328634c9..1f7f78ee 100644
--- a/src/commands/commit.js
+++ b/src/commands/commit.js
@@ -19,8 +19,6 @@ import { cores } from '../utils/plugins.js'
  * @param {FileSystem} [args.fs] - [deprecated] The filesystem containing the git repo. Overrides the fs provided by the [plugin system](./plugin_fs.md).
  * @param {string} [args.dir] - The [working tree](dir-vs-gitdir.md) directory path
  * @param {string} [args.gitdir=join(dir,'.git')] - [required] The [git directory](dir-vs-gitdir.md) path
- * @param {string} [args.ref] - The fully expanded name of the branch to commit to. Default is the current branch pointed to by HEAD. (TODO: fix it so it can expand branch names without throwing if the branch doesn't exist yet.)
- * @param {string[]} [args.parent] - The SHA-1 object ids of the commits to use as parents. If not specified, the commit pointed to by `ref` is used.
  * @param {string} args.message - The commit message to use.
  * @param {Object} [args.author] - The details about the author.
  * @param {string} [args.author.name] - Default is `user.name` config.
@@ -31,6 +29,9 @@ import { cores } from '../utils/plugins.js'
  * @param {Object} [args.committer = author] - The details about the commit committer, in the same format as the author parameter. If not specified, the author details are used.
  * @param {string} [args.signingKey] - Sign the tag object using this private PGP key.
  * @param {boolean} [args.noUpdateBranch = false] - If true, does not update the branch pointer after creating the commit.
+ * @param {string} [args.ref] - The fully expanded name of the branch to commit to. Default is the current branch pointed to by HEAD. (TODO: fix it so it can expand branch names without throwing if the branch doesn't exist yet.)
+ * @param {string[]} [args.parent] - The SHA-1 object ids of the commits to use as parents. If not specified, the commit pointed to by `ref` is used.
+ * @param {string} [args.tree] - The SHA-1 object id of the tree to use. If not specified, a new tree object is created from the current git index.
  *
  * @returns {Promise<string>} Resolves successfully with the SHA-1 object id of the newly created commit.
  *
@@ -51,13 +52,14 @@ export async function commit ({
   dir,
   gitdir = join(dir, '.git'),
   fs: _fs = cores.get(core).get('fs'),
-  ref,
-  parent,
   message,
   author,
   committer,
   signingKey,
-  noUpdateBranch = false
+  noUpdateBranch = false,
+  ref,
+  parent,
+  tree
 }) {
   try {
     const fs = new FileSystem(_fs)
@@ -98,7 +100,9 @@ export async function commit ({
       async function (index) {
         const inodes = flatFileListToDirectoryStructure(index.entries)
         const inode = inodes.get('.')
-        const treeRef = await constructTree({ fs, gitdir, inode })
+        if (!tree) {
+          tree = await constructTree({ fs, gitdir, inode })
+        }
         if (!parent) {
           try {
             parent = [
@@ -114,7 +118,7 @@ export async function commit ({
           }
         }
         let comm = GitCommit.from({
-          tree: treeRef,
+          tree,
           parent,
           author,
           committer,
diff --git a/src/index.d.ts b/src/index.d.ts
index 5e470b8f..e6f96c23 100644
--- a/src/index.d.ts
+++ b/src/index.d.ts
@@ -298,8 +298,6 @@ export function clone(args: WorkDir & GitDir & {
 export function commit(args: GitDir & {
   core?: string;
   fs?: any;
-  ref?: string;
-  parent?: string[];
   message: string;
   author: {
     name?: string;
@@ -317,6 +315,9 @@ export function commit(args: GitDir & {
   };
   signingKey?: string;
   noUpdateBranch?: boolean;
+  ref?: string;
+  parent?: string[];
+  tree?: string;
 }): Promise<string>;
 
 export function config(args: GitDir & {


Test patch:
diff --git a/__tests__/test-commit.js b/__tests__/test-commit.js
index 74235084..a198b131 100644
--- a/__tests__/test-commit.js
+++ b/__tests__/test-commit.js
@@ -85,7 +85,7 @@ describe('commit', () => {
     expect(sha).toEqual(copyOid)
   })
 
-  it('custom parents', async () => {
+  it('custom parents and tree', async () => {
     // Setup
     const { gitdir } = await makeFixture('test-commit')
     const { oid: originalOid } = (await log({ gitdir, depth: 1 }))[0]
@@ -95,9 +95,11 @@ describe('commit', () => {
       '2222222222222222222222222222222222222222',
       '3333333333333333333333333333333333333333'
     ]
+    const tree = '4444444444444444444444444444444444444444'
     const sha = await commit({
       gitdir,
       parent,
+      tree,
       author: {
         name: 'Mr. Test',
         email: 'mrtest@example.com',
@@ -106,11 +108,15 @@ describe('commit', () => {
       },
       message: 'Initial commit'
     })
-    expect(sha).toBe('e11b6803f58bc2218f2e92a1bae0eeee0101a06c')
+    expect(sha).toBe('43fbc94f2c1db655a833e08c72d005954ff32f32')
     // does NOT update master branch pointer
-    const { parent: parents } = (await log({ gitdir, depth: 1 }))[0]
+    const { parent: parents, tree: _tree } = (await log({
+      gitdir,
+      depth: 1
+    }))[0]
     expect(parents).not.toEqual([originalOid])
     expect(parents).toEqual(parent)
+    expect(_tree).toEqual(tree)
   })
 
   it('throw error if missing author', async () => {