Skip to content

Commit

Permalink
Attempt to update local branches
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Feb 4, 2024
1 parent c143c42 commit 685a3ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/angry-berries-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"every-ts": patch
---

Attempt to update local branches on `every-ts fetch`
23 changes: 22 additions & 1 deletion src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,28 @@ export class Fetch extends BaseCommand {

override async executeOrThrow(): Promise<number | void> {
await ensureRepo();
await execa(`git`, [`fetch`, `--all`, `--tags`], { cwd: tsDir });
await execa(`git`, [`fetch`, `--all`, `--tags`, `--update-head-ok`], { cwd: tsDir });

// Attempt to fast forward all branches.
// https://stackoverflow.com/a/24451300
const { stdout: currentBranch } = await execa(`git`, [`rev-parse`, `--abbrev-ref`, `HEAD`], { cwd: tsDir });
const { stdout: branches } = await execa(`git`, [`for-each-ref`, `refs/heads`, `--format=%(refname:short)`], {
cwd: tsDir,
});

for (const branch of branches.split(/\r?\n/)) {
let { stdout: originBranch } = await execa(`git`, [`config`, `--get`, `branch.${branch}.merge`], {
cwd: tsDir,
});
originBranch = originBranch.slice(`refs/heads/`.length);
// eslint-disable-next-line unicorn/prefer-ternary
if (branch === currentBranch) {
await execa(`git`, [`merge`, `--ff-only`, `origin/${originBranch}`], { cwd: tsDir });
} else {
await execa(`git`, [`fetch`, `.`, `origin/${originBranch}:${branch}`], { cwd: tsDir });
}
}

// This will usually be a noop, but if this is what's used to fetch the first time,
// it will be unbuilt which is less than good.
await ensureBuilt();
Expand Down

0 comments on commit 685a3ed

Please sign in to comment.