gitInitSetBranchAddOriginAddAndForcePush static method
gitInitSetBranchAddOriginAddAndForcePush Merupakan command github cli yang memungkinkan kamu untuk push ke repo langsung
Implementation
static Future<Map> gitInitSetBranchAddOriginAddAndForcePush({
required String username,
required String repo_name,
required String token_github,
bool isOrg = false,
bool isPrivate = false,
String branch = "main",
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
Client? httpClient,
}) async {
var res = await Process.run("which", ["git"]);
if (res.exitCode != 0) {
return {"@type": "error", "message": "git_not_found", "description": "Please install git first"};
}
GitHub gitHub = GitHub(auth: Authentication.withToken(token_github), client: httpClient);
RepositorySlug repositorySlug = RepositorySlug(username, repo_name);
Repository? repository = await Future(() async {
try {
return await gitHub.repositories.getRepository(repositorySlug);
} catch (e) {
return null;
}
});
if (repository == null) {
try {
await gitHub.repositories.createRepository(
CreateRepository(
repo_name,
private: isPrivate,
autoInit: true,
),
org: (isOrg) ? username : null,
);
} catch (e) {
return {"@type": "error", "message": "cant_create_repo", "description": "Please Check Permission Token Github First"};
}
}
List<String> git_adds = [
"init",
"add .",
"commit -m update",
"branch -M ${branch.trim()}",
"remote add origin https://github.com/${username}/${repo_name}.git",
"remote set-url --push origin https://${username}:${token_github}@github.com/${username}/${repo_name}",
"push origin ${branch.trim()} -f",
];
for (String element in git_adds) {
Process process = await Process.start(
"git",
element.split(" "),
workingDirectory: workingDirectory,
environment: environment,
includeParentEnvironment: includeParentEnvironment,
);
String message = "";
process.stderr.listen((event) {
try {
message += utf8.decode(event, allowMalformed: true);
} catch (e) {}
});
process.stdout.listen((event) {
try {
message += utf8.decode(event, allowMalformed: true);
} catch (e) {}
});
int exit_code = await process.exitCode;
bool is_error = false;
if (exit_code != 0) {
if (!RegExp("^(commit)", caseSensitive: false).hasMatch(element)) {
is_error = true;
}
if (RegExp("(remote (.*) already exists)", caseSensitive: false).hasMatch(message.trim())) {
is_error = false;
}
}
if (is_error) {
return {
"@type": "error",
"message": "",
"description": "${message.trim()}".replaceAll(RegExp("(ghp_.*)", caseSensitive: false), ""),
};
}
}
return {
"@type": "ok",
};
}