This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitUtils.ts
145 lines (113 loc) · 4.03 KB
/
GitUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2022. Heusala Group Oy <[email protected]>. All rights reserved.
import {
resolve as pathResolve,
dirname as pathDirname,
relative as relativePath
} from "path";
import { SyncFileUtils } from "../core/SyncFileUtils";
import { LogService } from "../core/LogService";
import { isString } from "../core/types/String";
import { SystemService } from "../core/SystemService";
const LOG = LogService.createLogger('GitUtils');
export class GitUtils {
/**
* Returns the git directory path
*
* @param filePath
*/
static getGitDir (filePath: string): string | undefined {
let dirPath: string = filePath;
let newDirPath: string = dirPath;
do {
LOG.debug(`getGitDir: Searching git directory from `, dirPath);
dirPath = newDirPath;
if ( SyncFileUtils.fileExists(pathResolve(dirPath, '.git')) ) {
return dirPath;
}
newDirPath = pathDirname(dirPath);
} while ( newDirPath !== dirPath );
return undefined;
}
static async initGit (
projectDir: string
) : Promise<string> {
const currentGitDir = GitUtils.getGitDir(projectDir);
if ( !currentGitDir ) {
LOG.debug(`Creating git directory`);
await GitUtils._git([ "init" ]);
return projectDir;
} else {
LOG.debug(`Git directory already exists: `, currentGitDir);
return currentGitDir;
}
}
static async addFiles (
filePath: string | string[],
projectPath: string
) {
const files = isString(filePath) ? [ filePath ] : filePath;
LOG.debug(`addFiles: Adding files: `, filePath);
await GitUtils._git([ '-C', projectPath, "add", ...files ]);
}
static async commit (
message: string,
projectPath: string
) {
LOG.debug(`commit with: `, message);
await GitUtils._git([ '-C', projectPath, "commit", '-m', message ]);
}
/**
*
* git branch -M main
* @param newName
*/
static async renameMainBranch (
newName: string,
projectPath: string
) {
LOG.debug(`rename branch: `, newName);
await GitUtils._git([ '-C', projectPath, "branch", '-M', newName ]);
}
static async addSubModule (
moduleUrl: string,
modulePath: string,
projectPath: string
) {
if ( !SyncFileUtils.fileExists(modulePath) ) {
await GitUtils._git([ '-C', projectPath, "submodule", 'add', moduleUrl, relativePath( projectPath, modulePath ) ]);
} else {
LOG.warn(`Warning! Git sub module directory already exists: `, modulePath);
}
}
static async setSubModuleBranch (
modulePath: string,
moduleBranch: string,
projectPath: string
) {
await GitUtils._git(
[ '-C', projectPath, "config", '-f', '.gitmodules', `submodule.${relativePath( projectPath, modulePath )}.branch`, moduleBranch ]);
}
static async initSubModule (
moduleUrl : string,
modulePath : string,
moduleBranch : string,
projectPath : string
) : Promise<void> {
const parentPath = pathDirname(modulePath);
// mkdir -p src/fi/hg
LOG.debug(`initSubModule: Creating: `, parentPath);
SyncFileUtils.mkdirp(parentPath);
// git submodule add [email protected]:sendanor/typescript.git src/fi/hg/ts
LOG.debug(`initSubModule: Adding submodule: `, moduleUrl, modulePath);
await GitUtils.addSubModule(moduleUrl, modulePath, projectPath);
// git config -f .gitmodules submodule.src/fi/hg/ts.branch main
LOG.debug(
`initSubModule: Configuring branch for `, moduleUrl, modulePath, ': ', moduleBranch);
await GitUtils.setSubModuleBranch(modulePath, moduleBranch, projectPath);
}
private static async _git (
args: string[]
): Promise<void> {
await SystemService.executeCommand('git', args, {stdio: 'inherit'});
}
}