@hanseltime/template-repo-sync 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.3.1](https://github.com/HanseltimeIndustries/template-repo-sync/compare/v1.3.0...v1.3.1) (2024-06-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * skipping checkout default is specified ([ad7ba7d](https://github.com/HanseltimeIndustries/template-repo-sync/commit/ad7ba7dc795883f813f70ed2e9140ac6a9030845))
7
+
1
8
  # [1.3.0](https://github.com/HanseltimeIndustries/template-repo-sync/compare/v1.2.0...v1.3.0) (2024-06-09)
2
9
 
3
10
 
@@ -4,14 +4,26 @@ exports.gitCheckout = void 0;
4
4
  const child_process_1 = require("child_process");
5
5
  async function gitCheckout(options) {
6
6
  const { branch, remoteName, tmpDir } = options;
7
- (0, child_process_1.execSync)(`git fetch ${remoteName} ${branch}`, {
7
+ const remoteInfo = (0, child_process_1.execSync)(`git remote show ${remoteName}`, {
8
8
  cwd: tmpDir,
9
9
  env: process.env,
10
- });
11
- (0, child_process_1.execSync)(`git checkout -b ${branch} --track ${remoteName}/${branch}`, {
12
- cwd: tmpDir,
13
- env: process.env,
14
- });
10
+ }).toString();
11
+ const defaultMatch = /HEAD branch:\s*([a-zA-Z0-9_-]+)/.exec(remoteInfo);
12
+ if (!defaultMatch || !defaultMatch[1]) {
13
+ throw new Error(`Could not determine default branch of cloned repo.\nAttempted to find in remote info:\n${remoteInfo} `);
14
+ }
15
+ const defaultBranch = defaultMatch[1];
16
+ // Skip this if the default branch is already pulled
17
+ if (defaultBranch !== branch) {
18
+ (0, child_process_1.execSync)(`git fetch ${remoteName} ${branch}`, {
19
+ cwd: tmpDir,
20
+ env: process.env,
21
+ });
22
+ (0, child_process_1.execSync)(`git checkout -b ${branch} --track ${remoteName}/${branch}`, {
23
+ cwd: tmpDir,
24
+ env: process.env,
25
+ });
26
+ }
15
27
  return true;
16
28
  }
17
29
  exports.gitCheckout = gitCheckout;
@@ -4,14 +4,26 @@ exports.gitCheckout = void 0;
4
4
  const child_process_1 = require("child_process");
5
5
  async function gitCheckout(options) {
6
6
  const { branch, remoteName, tmpDir } = options;
7
- (0, child_process_1.execSync)(`git fetch ${remoteName} ${branch}`, {
7
+ const remoteInfo = (0, child_process_1.execSync)(`git remote show ${remoteName}`, {
8
8
  cwd: tmpDir,
9
9
  env: process.env,
10
- });
11
- (0, child_process_1.execSync)(`git checkout -b ${branch} --track ${remoteName}/${branch}`, {
12
- cwd: tmpDir,
13
- env: process.env,
14
- });
10
+ }).toString();
11
+ const defaultMatch = /HEAD branch:\s*([a-zA-Z0-9_-]+)/.exec(remoteInfo);
12
+ if (!defaultMatch || !defaultMatch[1]) {
13
+ throw new Error(`Could not determine default branch of cloned repo.\nAttempted to find in remote info:\n${remoteInfo} `);
14
+ }
15
+ const defaultBranch = defaultMatch[1];
16
+ // Skip this if the default branch is already pulled
17
+ if (defaultBranch !== branch) {
18
+ (0, child_process_1.execSync)(`git fetch ${remoteName} ${branch}`, {
19
+ cwd: tmpDir,
20
+ env: process.env,
21
+ });
22
+ (0, child_process_1.execSync)(`git checkout -b ${branch} --track ${remoteName}/${branch}`, {
23
+ cwd: tmpDir,
24
+ env: process.env,
25
+ });
26
+ }
15
27
  return true;
16
28
  }
17
29
  exports.gitCheckout = gitCheckout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanseltime/template-repo-sync",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "An npm library that enables pluggable, customizable synchronization between template repos",
5
5
  "main": "lib/cjs/index.js",
6
6
  "types": "lib/cjs/index.d.ts",
@@ -55,4 +55,15 @@ describe("gitCheckout", () => {
55
55
  }),
56
56
  ).rejects.toThrow();
57
57
  });
58
+ it("does not throw if the branch was the default", async () => {
59
+ await gitCheckout({
60
+ tmpDir: tmpRepoDir,
61
+ remoteName: "origin",
62
+ branch: "master", // We set this in the gitDir
63
+ });
64
+
65
+ expect(readFileSync(join(tmpRepoDir, "README.md")).toString()).toContain(
66
+ "# This is the master branch",
67
+ );
68
+ });
58
69
  });
@@ -9,14 +9,30 @@ export async function gitCheckout(options: {
9
9
  branch: string;
10
10
  }): Promise<boolean> {
11
11
  const { branch, remoteName, tmpDir } = options;
12
- execSync(`git fetch ${remoteName} ${branch}`, {
13
- cwd: tmpDir,
14
- env: process.env,
15
- });
16
- execSync(`git checkout -b ${branch} --track ${remoteName}/${branch}`, {
12
+
13
+ const remoteInfo = execSync(`git remote show ${remoteName}`, {
17
14
  cwd: tmpDir,
18
15
  env: process.env,
19
- });
16
+ }).toString();
17
+ const defaultMatch = /HEAD branch:\s*([a-zA-Z0-9_-]+)/.exec(remoteInfo);
18
+ if (!defaultMatch || !defaultMatch[1]) {
19
+ throw new Error(
20
+ `Could not determine default branch of cloned repo.\nAttempted to find in remote info:\n${remoteInfo} `,
21
+ );
22
+ }
23
+
24
+ const defaultBranch = defaultMatch[1];
25
+ // Skip this if the default branch is already pulled
26
+ if (defaultBranch !== branch) {
27
+ execSync(`git fetch ${remoteName} ${branch}`, {
28
+ cwd: tmpDir,
29
+ env: process.env,
30
+ });
31
+ execSync(`git checkout -b ${branch} --track ${remoteName}/${branch}`, {
32
+ cwd: tmpDir,
33
+ env: process.env,
34
+ });
35
+ }
20
36
 
21
37
  return true;
22
38
  }