@@ -4,60 +4,75 @@ import * as github from "@actions/github";
44import { commitChangesFromRepo } from "@changesets/ghcommit/git" ;
55import { Octokit } from "./octokit" ;
66
7- const push = async ( branch : string , { force } : { force ?: boolean } = { } ) => {
8- await exec (
9- "git" ,
10- [ "push" , "origin" , `HEAD:${ branch } ` , force && "--force" ] . filter < string > (
11- Boolean as any
12- )
13- ) ;
7+ type GitOptions = {
8+ cwd : string ;
149} ;
1510
16- const switchToMaybeExistingBranch = async ( branch : string ) => {
11+ const push = async ( branch : string , options : GitOptions ) => {
12+ await exec ( "git" , [ "push" , "origin" , `HEAD:${ branch } ` , "--force" ] , options ) ;
13+ } ;
14+
15+ const switchToMaybeExistingBranch = async (
16+ branch : string ,
17+ options : GitOptions
18+ ) => {
1719 let { stderr } = await getExecOutput ( "git" , [ "checkout" , branch ] , {
1820 ignoreReturnCode : true ,
21+ ...options ,
1922 } ) ;
2023 let isCreatingBranch = ! stderr
2124 . toString ( )
2225 . includes ( `Switched to a new branch '${ branch } '` ) ;
2326 if ( isCreatingBranch ) {
24- await exec ( "git" , [ "checkout" , "-b" , branch ] ) ;
27+ await exec ( "git" , [ "checkout" , "-b" , branch ] , options ) ;
2528 }
2629} ;
2730
28- const reset = async (
29- pathSpec : string ,
30- mode : "hard" | "soft" | "mixed" = "hard"
31- ) => {
32- await exec ( "git" , [ "reset" , `--${ mode } ` , pathSpec ] ) ;
31+ const reset = async ( pathSpec : string , options : GitOptions ) => {
32+ await exec ( "git" , [ "reset" , `--hard` , pathSpec ] , options ) ;
3333} ;
3434
35- const commitAll = async ( message : string ) => {
36- await exec ( "git" , [ "add" , "." ] ) ;
37- await exec ( "git" , [ "commit" , "-m" , message ] ) ;
35+ const commitAll = async ( message : string , options : GitOptions ) => {
36+ await exec ( "git" , [ "add" , "." ] , options ) ;
37+ await exec ( "git" , [ "commit" , "-m" , message ] , options ) ;
3838} ;
3939
40- const checkIfClean = async ( ) : Promise < boolean > => {
41- const { stdout } = await getExecOutput ( "git" , [ "status" , "--porcelain" ] ) ;
40+ const checkIfClean = async ( options : GitOptions ) : Promise < boolean > => {
41+ const { stdout } = await getExecOutput (
42+ "git" ,
43+ [ "status" , "--porcelain" ] ,
44+ options
45+ ) ;
4246 return ! stdout . length ;
4347} ;
4448
4549export class Git {
46- octokit ;
47- constructor ( octokit ?: Octokit ) {
48- this . octokit = octokit ;
50+ readonly octokit : Octokit | null ;
51+ readonly cwd : string ;
52+
53+ constructor ( args : { octokit ?: Octokit ; cwd : string } ) {
54+ this . octokit = args . octokit ?? null ;
55+ this . cwd = args . cwd ;
4956 }
5057
5158 async setupUser ( ) {
5259 if ( this . octokit ) {
5360 return ;
5461 }
55- await exec ( "git" , [ "config" , "user.name" , `"github-actions[bot]"` ] ) ;
56- await exec ( "git" , [
57- "config" ,
58- "user.email" ,
59- `"41898282+github-actions[bot]@users.noreply.github.com"` ,
60- ] ) ;
62+ await exec ( "git" , [ "config" , "user.name" , `"github-actions[bot]"` ] , {
63+ cwd : this . cwd ,
64+ } ) ;
65+ await exec (
66+ "git" ,
67+ [
68+ "config" ,
69+ "user.email" ,
70+ `"41898282+github-actions[bot]@users.noreply.github.com"` ,
71+ ] ,
72+ {
73+ cwd : this . cwd ,
74+ }
75+ ) ;
6176 }
6277
6378 async pushTag ( tag : string ) {
@@ -73,20 +88,27 @@ export class Git {
7388 core . warning ( `Failed to create tag ${ tag } : ${ err . message } ` ) ;
7489 } ) ;
7590 }
76- await exec ( "git" , [ "push" , "origin" , tag ] ) ;
91+ await exec ( "git" , [ "push" , "origin" , tag ] , { cwd : this . cwd } ) ;
7792 }
7893
7994 async prepareBranch ( branch : string ) {
8095 if ( this . octokit ) {
8196 // Preparing a new local branch is not necessary when using the API
8297 return ;
8398 }
84- await switchToMaybeExistingBranch ( branch ) ;
85- await reset ( github . context . sha ) ;
99+ await switchToMaybeExistingBranch ( branch , { cwd : this . cwd } ) ;
100+ await reset ( github . context . sha , { cwd : this . cwd } ) ;
86101 }
87102
88103 async pushChanges ( { branch, message } : { branch : string ; message : string } ) {
89104 if ( this . octokit ) {
105+ /**
106+ * Only add files form the current working directory
107+ *
108+ * This will emulate the behavior of `git add .`,
109+ * used in {@link commitAll}.
110+ */
111+ const addFromDirectory = this . cwd ;
90112 return commitChangesFromRepo ( {
91113 octokit : this . octokit ,
92114 ...github . context . repo ,
@@ -95,12 +117,13 @@ export class Git {
95117 base : {
96118 commit : github . context . sha ,
97119 } ,
120+ addFromDirectory,
98121 force : true ,
99122 } ) ;
100123 }
101- if ( ! ( await checkIfClean ( ) ) ) {
102- await commitAll ( message ) ;
124+ if ( ! ( await checkIfClean ( { cwd : this . cwd } ) ) ) {
125+ await commitAll ( message , { cwd : this . cwd } ) ;
103126 }
104- await push ( branch , { force : true } ) ;
127+ await push ( branch , { cwd : this . cwd } ) ;
105128 }
106129}
0 commit comments