Repository: isomorphic-git/isomorphic-git
Pull request #813 (2019-08-21 20:00:30)

feat: Added 'parent' parameter to commit command

## I'm adding a parameter to an existing command:

- [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 f3e41351e323de82af2ac0769b4f803fd90895bc:
| Browser | Passed | Skipped | Failed | Time | Disconnected | Error |
|---------|--------|---------|--------|------|--------------|-------|
| HeadlessChrome 0.0.0 (Linux 0.0.0) | 270 | 1 | 0 | 1 min 26.795 secs | false | undefined |
| Firefox 68.0.0 (Ubuntu 0.0.0) | 270 | 1 | 0 | 2 mins 2.445 secs | false | undefined |
| Safari 11.1.2 (Mac OS X 10.13.6) | 270 | 1 | 0 | 3 mins 57.705 secs | false | undefined |
| Chrome Mobile 67.0.3396 (Android 7.1.1) | 270 | 1 | 0 | 4 mins 22.078 secs | false | undefined |
| Mobile Safari 11.0.0 (iOS 11.2.0) | 270 | 1 | 0 | 4 mins 45.069 secs | false | undefined |
| Edge 17.17134.0 (Windows 10 0.0.0) | 270 | 1 | 0 | 6 mins 3.276 secs | false | undefined |

Patch:
diff --git a/src/commands/commit.js b/src/commands/commit.js
index 14081752..328634c9 100644
--- a/src/commands/commit.js
+++ b/src/commands/commit.js
@@ -20,6 +20,7 @@ import { cores } from '../utils/plugins.js'
  * @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.
@@ -51,6 +52,7 @@ export async function commit ({
   gitdir = join(dir, '.git'),
   fs: _fs = cores.get(core).get('fs'),
   ref,
+  parent,
   message,
   author,
   committer,
@@ -97,21 +99,23 @@ export async function commit ({
         const inodes = flatFileListToDirectoryStructure(index.entries)
         const inode = inodes.get('.')
         const treeRef = await constructTree({ fs, gitdir, inode })
-        let parents
-        try {
-          const parent = await GitRefManager.resolve({
-            fs,
-            gitdir,
-            ref
-          })
-          parents = [parent]
-        } catch (err) {
-          // Probably an initial commit
-          parents = []
+        if (!parent) {
+          try {
+            parent = [
+              await GitRefManager.resolve({
+                fs,
+                gitdir,
+                ref
+              })
+            ]
+          } catch (err) {
+            // Probably an initial commit
+            parent = []
+          }
         }
         let comm = GitCommit.from({
           tree: treeRef,
-          parent: parents,
+          parent,
           author,
           committer,
           message
diff --git a/src/index.d.ts b/src/index.d.ts
index 80db38da..5e470b8f 100644
--- a/src/index.d.ts
+++ b/src/index.d.ts
@@ -299,6 +299,7 @@ export function commit(args: GitDir & {
   core?: string;
   fs?: any;
   ref?: string;
+  parent?: string[];
   message: string;
   author: {
     name?: string;


Test patch:
diff --git a/__tests__/test-commit.js b/__tests__/test-commit.js
index 7ae29cfe..74235084 100644
--- a/__tests__/test-commit.js
+++ b/__tests__/test-commit.js
@@ -26,7 +26,8 @@ describe('commit', () => {
     })
     expect(sha).toBe('7a51c0b1181d738198ff21c4679d3aa32eb52fe0')
     // updates branch pointer
-    const { oid: currentOid } = (await log({ gitdir, depth: 1 }))[0]
+    const { oid: currentOid, parent } = (await log({ gitdir, depth: 1 }))[0]
+    expect(parent).toEqual([originalOid])
     expect(currentOid).not.toEqual(originalOid)
     expect(currentOid).toEqual(sha)
   })
@@ -84,6 +85,34 @@ describe('commit', () => {
     expect(sha).toEqual(copyOid)
   })
 
+  it('custom parents', async () => {
+    // Setup
+    const { gitdir } = await makeFixture('test-commit')
+    const { oid: originalOid } = (await log({ gitdir, depth: 1 }))[0]
+    // Test
+    const parent = [
+      '1111111111111111111111111111111111111111',
+      '2222222222222222222222222222222222222222',
+      '3333333333333333333333333333333333333333'
+    ]
+    const sha = await commit({
+      gitdir,
+      parent,
+      author: {
+        name: 'Mr. Test',
+        email: 'mrtest@example.com',
+        timestamp: 1262356920,
+        timezoneOffset: -0
+      },
+      message: 'Initial commit'
+    })
+    expect(sha).toBe('e11b6803f58bc2218f2e92a1bae0eeee0101a06c')
+    // does NOT update master branch pointer
+    const { parent: parents } = (await log({ gitdir, depth: 1 }))[0]
+    expect(parents).not.toEqual([originalOid])
+    expect(parents).toEqual(parent)
+  })
+
   it('throw error if missing author', async () => {
     // Setup
     const { gitdir } = await makeFixture('test-commit')