Repository: atlassian/changesets
Pull request #301 (2020-03-26 03:20:00)

chore: ensure we use the diverged ref when checking changed changesets
I noticed that we weren't using the result of the call to `spawn` on line 73 here: https://github.com/atlassian/changesets/blob/master/packages/git/src/index.ts#L72-L77.

So, I've abstracted the common code into a function, and added a test for it.

changeset-bot[bot]: ###  💥  No Changeset

Latest commit: 0405b77372992d31f01c80295e5b64c484523870

Merging this PR will not cause any packages to be released. If these changes should not cause updates to packages in this repo, this is fine 🙂

**If these changes should be published to npm, you need to add a changeset.**

<details><summary>This PR includes no changesets</summary>

  When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

</details>

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/atlassian/changesets/new/chore/fixup-minor-issue?filename=.changeset/pretty-kangaroos-check.md&value=---%0A%22%40changesets%2Fgit%22%3A%20patch%0A---%0A%0Achore%3A%20ensure%20we%20use%20the%20diverged%20ref%20when%20checking%20changed%20changesets%0A)


codecov[bot]: # [Codecov](https://codecov.io/gh/atlassian/changesets/pull/301?src=pr&el=h1) Report
> Merging [#301](https://codecov.io/gh/atlassian/changesets/pull/301?src=pr&el=desc) into [master](https://codecov.io/gh/atlassian/changesets/commit/92960a28584da044f762c5e6229ced1f3bfb7251&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/atlassian/changesets/pull/301/graphs/tree.svg?width=650&height=150&src=pr&token=j0W25zrXm2)](https://codecov.io/gh/atlassian/changesets/pull/301?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #301      +/-   ##
==========================================
+ Coverage   77.70%   77.72%   +0.02%     
==========================================
  Files          40       40              
  Lines        1045     1046       +1     
  Branches      228      225       -3     
==========================================
+ Hits          812      813       +1     
  Misses        223      223              
  Partials       10       10              
```


| [Impacted Files](https://codecov.io/gh/atlassian/changesets/pull/301?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/git/src/index.ts](https://codecov.io/gh/atlassian/changesets/pull/301/diff?src=pr&el=tree#diff-cGFja2FnZXMvZ2l0L3NyYy9pbmRleC50cw==) | `91.42% <100.00%> (+0.25%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/atlassian/changesets/pull/301?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/atlassian/changesets/pull/301?src=pr&el=footer). Last update [92960a2...fc4acc9](https://codecov.io/gh/atlassian/changesets/pull/301?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/git/src/index.ts b/packages/git/src/index.ts
index 4bd4db7..60b5b6c 100644
--- a/packages/git/src/index.ts
+++ b/packages/git/src/index.ts
@@ -29,6 +29,12 @@ async function tag(tagStr: string, cwd: string) {
   return gitCmd.code === 0;
 }
 
+export async function getDivergedCommit(cwd: string, ref: string) {
+  // First we need to find the commit where we diverged from `ref` at using `git merge-base`
+  const cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
+  return cmd.stdout.toString().trim();
+}
+
 async function getCommitThatAddsFile(gitPath: string, cwd: string) {
   const gitCmd = await spawn(
     "git",
@@ -47,11 +53,9 @@ async function getChangedFilesSince({
   ref: string;
   fullPath?: boolean;
 }): Promise<Array<string>> {
-  // First we need to find the commit where we diverged from `ref` at using `git merge-base`
-  let cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
-  const divergedAt = cmd.stdout.toString().trim();
+  const divergedAt = await getDivergedCommit(cwd, ref);
   // Now we can find which files we added
-  cmd = await spawn("git", ["diff", "--name-only", divergedAt], { cwd });
+  const cmd = await spawn("git", ["diff", "--name-only", divergedAt], { cwd });
   const files = cmd.stdout
     .toString()
     .trim()
@@ -69,12 +73,15 @@ async function getChangedChangesetFilesSinceRef({
   ref: string;
 }): Promise<Array<string>> {
   try {
-    // First we need to find the commit where we diverged from `ref` at using `git merge-base`
-    let cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
+    const divergedAt = await getDivergedCommit(cwd, ref);
     // Now we can find which files we added
-    cmd = await spawn("git", ["diff", "--name-only", "--diff-filter=d", ref], {
-      cwd
-    });
+    const cmd = await spawn(
+      "git",
+      ["diff", "--name-only", "--diff-filter=d", divergedAt],
+      {
+        cwd
+      }
+    );
 
     let tester = /.changeset\/[^/]+\.md$/;
 


Test patch:
diff --git a/packages/git/src/index.test.ts b/packages/git/src/index.test.ts
index 8dafd1f..5451c6e 100644
--- a/packages/git/src/index.test.ts
+++ b/packages/git/src/index.test.ts
@@ -7,15 +7,61 @@ import {
   add,
   commit,
   tag,
+  getDivergedCommit,
   getChangedPackagesSinceRef,
   getChangedChangesetFilesSinceRef
 } from "./";
 
+async function getCurrentCommit(cwd: string) {
+  const cmd = await spawn("git", ["rev-parse", "HEAD"], { cwd });
+  return cmd.stdout.toString().trim();
+}
+
 describe("git", () => {
   let cwd: string;
   beforeEach(async () => {
     cwd = await copyFixtureIntoTempDir(__dirname, "with-git");
     await spawn("git", ["init"], { cwd });
+    await spawn("git", ["config", "user.email", "x@y.z"], { cwd });
+    await spawn("git", ["config", "user.name", "xyz"], { cwd });
+  });
+
+  describe("getDivergedCommit", () => {
+    it("should return same commit when branches have not diverged", async () => {
+      await add("packages/pkg-a/package.json", cwd);
+      await commit("added packageA package.json", cwd);
+
+      const firstSha = await getCurrentCommit(cwd);
+
+      await add("packages/pkg-b/package.json", cwd);
+      await commit("added packageB package.json", cwd);
+
+      const secondSha = await getCurrentCommit(cwd);
+      const divergedSha = await getDivergedCommit(cwd, "master");
+      expect(firstSha).not.toBe(secondSha);
+      expect(divergedSha).toBe(secondSha);
+    });
+
+    it("should find commit where branch diverged", async () => {
+      await add("packages/pkg-a/package.json", cwd);
+      await commit("added packageA package.json", cwd);
+
+      // This is the first commit. We branch (diverge) from here.
+      const masterSha = await getCurrentCommit(cwd);
+
+      // Create a new branch, and add a commit to it.
+      await spawn("git", ["checkout", "-b", "my-branch"], { cwd });
+      await add("packages/pkg-b/package.json", cwd);
+      await commit("added packageB package.json", cwd);
+
+      // Now, get the latest commit from our new branch.
+      const branchSha = await getCurrentCommit(cwd);
+
+      // Finally, get the divergent commit.
+      const divergedSha = await getDivergedCommit(cwd, "master");
+      expect(masterSha).not.toBe(branchSha);
+      expect(divergedSha).toBe(masterSha);
+    });
   });
 
   describe("add", () => {