build branch commits

This commit is contained in:
Peter Evans 2024-08-08 14:08:16 +00:00
parent ec02db74e9
commit b2a409b0a3
3 changed files with 104 additions and 2 deletions

View file

@ -2,7 +2,8 @@ import {
createOrUpdateBranch,
tryFetch,
getWorkingBaseAndType,
buildBranchFileChanges
buildBranchFileChanges,
buildBranchCommits
} from '../lib/create-or-update-branch'
import * as fs from 'fs'
import {GitCommandManager} from '../lib/git-command-manager'
@ -230,6 +231,73 @@ describe('create-or-update-branch tests', () => {
expect(workingBaseType).toEqual('commit')
})
it('tests buildBranchCommits with no diff', async () => {
await git.checkout(BRANCH, BASE)
const branchCommits = await buildBranchCommits(git, BASE, BRANCH)
expect(branchCommits.length).toEqual(0)
})
it('tests buildBranchCommits with addition and modification', async () => {
await git.checkout(BRANCH, BASE)
await createChanges()
await git.exec(['add', '-A'])
await git.commit(['-m', 'Test changes'])
const branchCommits = await buildBranchCommits(git, BASE, BRANCH)
expect(branchCommits.length).toEqual(1)
expect(branchCommits[0].subject).toEqual('Test changes')
expect(branchCommits[0].changes.length).toEqual(2)
expect(branchCommits[0].changes).toEqual([
{mode: '100644', path: TRACKED_FILE, status: 'M'},
{mode: '100644', path: UNTRACKED_FILE, status: 'A'}
])
})
it('tests buildBranchCommits with addition and deletion', async () => {
await git.checkout(BRANCH, BASE)
await createChanges()
const TRACKED_FILE_NEW_PATH = 'c/tracked-file.txt'
const filepath = path.join(REPO_PATH, TRACKED_FILE_NEW_PATH)
await fs.promises.mkdir(path.dirname(filepath), {recursive: true})
await fs.promises.rename(path.join(REPO_PATH, TRACKED_FILE), filepath)
await git.exec(['add', '-A'])
await git.commit(['-m', 'Test changes'])
const branchCommits = await buildBranchCommits(git, BASE, BRANCH)
expect(branchCommits.length).toEqual(1)
expect(branchCommits[0].subject).toEqual('Test changes')
expect(branchCommits[0].changes.length).toEqual(3)
expect(branchCommits[0].changes).toEqual([
{mode: '000000', path: TRACKED_FILE, status: 'D'},
{mode: '100644', path: UNTRACKED_FILE, status: 'A'},
{mode: '100644', path: TRACKED_FILE_NEW_PATH, status: 'A'}
])
})
it('tests buildBranchCommits with multiple commits', async () => {
await git.checkout(BRANCH, BASE)
for (let i = 0; i < 3; i++) {
await createChanges()
await git.exec(['add', '-A'])
await git.commit(['-m', `Test changes ${i}`])
}
const branchCommits = await buildBranchCommits(git, BASE, BRANCH)
expect(branchCommits.length).toEqual(3)
for (let i = 0; i < 3; i++) {
expect(branchCommits[i].subject).toEqual(`Test changes ${i}`)
expect(branchCommits[i].changes.length).toEqual(2)
const untrackedFileStatus = i == 0 ? 'A' : 'M'
expect(branchCommits[i].changes).toEqual([
{mode: '100644', path: TRACKED_FILE, status: 'M'},
{mode: '100644', path: UNTRACKED_FILE, status: untrackedFileStatus}
])
}
})
it('tests buildBranchFileChanges with no diff', async () => {
await git.checkout(BRANCH, BASE)
const branchFileChanges = await buildBranchFileChanges(git, BASE, BRANCH)

16
dist/index.js vendored
View file

@ -42,6 +42,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.WorkingBaseType = void 0;
exports.getWorkingBaseAndType = getWorkingBaseAndType;
exports.tryFetch = tryFetch;
exports.buildBranchCommits = buildBranchCommits;
exports.buildBranchFileChanges = buildBranchFileChanges;
exports.createOrUpdateBranch = createOrUpdateBranch;
const core = __importStar(__nccwpck_require__(2186));
@ -83,6 +84,21 @@ function tryFetch(git, remote, branch, depth) {
}
});
}
function buildBranchCommits(git, base, branch) {
return __awaiter(this, void 0, void 0, function* () {
const output = yield git.exec(['log', '--format=%H', `${base}..${branch}`]);
const shas = output.stdout
.split('\n')
.filter(x => x !== '')
.reverse();
const commits = [];
for (const sha of shas) {
const commit = yield git.getCommit(sha);
commits.push(commit);
}
return commits;
});
}
function buildBranchFileChanges(git, base, branch) {
return __awaiter(this, void 0, void 0, function* () {
const branchFileChanges = {

View file

@ -1,5 +1,5 @@
import * as core from '@actions/core'
import {GitCommandManager} from './git-command-manager'
import {GitCommandManager, Commit} from './git-command-manager'
import {v4 as uuidv4} from 'uuid'
import * as utils from './utils'
@ -48,6 +48,24 @@ export async function tryFetch(
}
}
export async function buildBranchCommits(
git: GitCommandManager,
base: string,
branch: string
): Promise<Commit[]> {
const output = await git.exec(['log', '--format=%H', `${base}..${branch}`])
const shas = output.stdout
.split('\n')
.filter(x => x !== '')
.reverse()
const commits: Commit[] = []
for (const sha of shas) {
const commit = await git.getCommit(sha)
commits.push(commit)
}
return commits
}
export async function buildBranchFileChanges(
git: GitCommandManager,
base: string,