|
1 | | -import { execSync } from 'child_process'; |
| 1 | +import {execSync} from 'child_process'; |
2 | 2 | import * as core from '@actions/core'; |
3 | 3 |
|
4 | | -function sh(cmd: string, opts: { silent?: boolean } = {}): string { |
| 4 | +function run() { |
5 | 5 | try { |
6 | | - return execSync(cmd, { encoding: 'utf-8', stdio: opts.silent ? ['ignore', 'pipe', 'pipe'] : 'pipe' }).trim(); |
7 | | - } catch (e) { |
8 | | - if (!opts.silent) core.debug(`Command failed: ${cmd}\n${String(e)}`); |
9 | | - throw e; |
10 | | - } |
11 | | -} |
12 | | - |
13 | | -function trySh(cmd: string, opts: { silent?: boolean } = {}): string { |
14 | | - try { |
15 | | - return sh(cmd, opts); |
16 | | - } catch { |
17 | | - return ''; |
18 | | - } |
19 | | -} |
| 6 | + const subprojectPrefixes = core.getInput('project_prefixes')?.split(",") ?? []; |
| 7 | + const requiredProjects = core.getInput('required_projects')?.split(",") ?? []; |
20 | 8 |
|
21 | | -function isShallowRepo(): boolean { |
22 | | - const out = trySh('git rev-parse --is-shallow-repository', { silent: true }); |
23 | | - return out === 'true'; |
24 | | -} |
25 | | - |
26 | | -function ensureHistoryAndTags(): void { |
27 | | - // Detect shallow/non-shallow |
28 | | - let isShallow: boolean; |
29 | | - try { |
30 | | - const out = execSync('git rev-parse --is-shallow-repository', { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'] }).trim(); |
31 | | - isShallow = out === 'true'; |
32 | | - } catch { |
33 | | - // If the command isn't supported (very old git), assume not shallow |
34 | | - isShallow = false; |
35 | | - } |
36 | | - |
37 | | - core.info(`Repo shallow: ${isShallow}`); |
38 | | - |
39 | | - if (isShallow) { |
40 | | - // Shallow checkout → unshallow and fetch tags |
41 | | - core.info('Fetching to unshallow repository and include tags…'); |
42 | | - execSync('git fetch --unshallow --tags --force', { encoding: 'utf-8' }); |
43 | | - } else { |
44 | | - // Already complete → just refresh tags and prune |
45 | | - core.info('Repository already complete; fetching tags and pruning…'); |
46 | | - execSync('git fetch --tags --force --prune', { encoding: 'utf-8' }); |
47 | | - } |
48 | | -} |
49 | | -/** |
50 | | - * Determine a sensible base for diff: |
51 | | - * - On pull_request events: origin/<base_branch> |
52 | | - * - On push events: HEAD~1 (previous commit on same branch) if exists |
53 | | - * - Fallback: merge-base with origin/main (adjust default if needed) |
54 | | - */ |
55 | | -function resolveBaseRef(): string { |
56 | | - const eventName = process.env.GITHUB_EVENT_NAME || ''; |
57 | | - const prBase = process.env.GITHUB_BASE_REF; // set on pull_request events |
58 | | - if (eventName.startsWith('pull_request') && prBase) { |
59 | | - core.info(`PR build detected. Using origin/${prBase} as diff base`); |
60 | | - // Ensure we have the PR base |
61 | | - trySh(`git fetch origin ${prBase} --force`, { silent: true }); |
62 | | - return `origin/${prBase}`; |
63 | | - } |
64 | | - |
65 | | - // Push builds: previous commit if available |
66 | | - const prev = trySh('git rev-parse HEAD~1', { silent: true }); |
67 | | - if (prev) { |
68 | | - core.info('Push build detected. Using HEAD~1 as diff base'); |
69 | | - return prev; |
70 | | - } |
71 | | - |
72 | | - // Fallback: merge-base with origin/main (change "main" if your default is different) |
73 | | - core.info('Fallback diff base: merge-base with origin/main'); |
74 | | - trySh('git fetch origin main --force', { silent: true }); |
75 | | - const mergeBase = trySh('git merge-base HEAD origin/main', { silent: true }); |
76 | | - return mergeBase || 'HEAD'; |
77 | | -} |
78 | | - |
79 | | -function run(): void { |
80 | | - try { |
81 | | - const subprojectPrefixes = (core.getInput('project_prefixes') || '') |
82 | | - .split(',') |
83 | | - .map(s => s.trim()) |
84 | | - .filter(Boolean); |
85 | | - const requiredProjects = (core.getInput('required_projects') || '') |
86 | | - .split(',') |
87 | | - .map(s => s.trim()) |
88 | | - .filter(Boolean); |
89 | | - |
90 | | - core.debug('Ensuring full history and tags…'); |
91 | | - ensureHistoryAndTags(); |
| 9 | + core.debug("executing git fetch"); |
| 10 | + execSync('git fetch --unshallow', {encoding: 'utf-8'}); |
92 | 11 |
|
93 | 12 | const githubSha = process.env.GITHUB_SHA; |
94 | 13 | if (!githubSha) { |
95 | | - core.setFailed('GITHUB_SHA not set'); |
96 | | - return; |
| 14 | + core.setFailed('GITHUB_SHA not set') |
97 | 15 | } |
98 | | - |
99 | | - const base = resolveBaseRef(); |
100 | | - const diffCmd = `git diff --name-only ${base}..${githubSha}`; |
| 16 | + const diffCmd = `git diff --name-only HEAD~1..${githubSha}`; |
101 | 17 |
|
102 | 18 | core.debug(`Executing: ${diffCmd}`); |
103 | 19 | core.debug(`Git Status: ${execSync(`git status`, {encoding: 'utf-8'}).trim()}`); |
|
0 commit comments