-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
247 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export interface GitRemote { | ||
[key: string]: unknown; | ||
name?: string; | ||
url?: string; | ||
fetch?: string; | ||
} | ||
|
||
export interface GitBranch { | ||
[key: string]: unknown; | ||
remote?: string; | ||
merge?: string; | ||
description?: string; | ||
rebase?: boolean; | ||
} | ||
|
||
export interface GitCoreConfig { | ||
[key: string]: unknown; | ||
} | ||
|
||
export interface GitConfigUser { | ||
[key: string]: unknown; | ||
name?: string; | ||
email?: string; | ||
} | ||
|
||
export interface GitConfig { | ||
[key: string]: unknown; | ||
core?: GitCoreConfig; | ||
user?: GitConfigUser; | ||
remote?: Record<string, GitRemote>; | ||
branch?: Record<string, GitBranch>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { GitConfig } from "./types"; | ||
import { readFile, writeFile } from "node:fs/promises"; | ||
import { findNearestFile } from "../resolve/utils"; | ||
import { parseINI, stringifyINI } from "confbox/ini"; | ||
import type { ResolveOptions } from "../resolve/types"; | ||
|
||
/** | ||
* Defines a git config object. | ||
*/ | ||
export function defineGitConfig(config: GitConfig): GitConfig { | ||
return config; | ||
} | ||
|
||
/** | ||
* Finds closest `.git/config` file. | ||
*/ | ||
export async function resolveGitConfig( | ||
dir: string, | ||
opts?: ResolveOptions, | ||
): Promise<string> { | ||
return findNearestFile(".git/config", { ...opts, startingFrom: dir }); | ||
} | ||
|
||
/** | ||
* Finds and reads closest `.git/config` file into a JS object. | ||
*/ | ||
export async function readGitConfig(dir: string, opts?: ResolveOptions) { | ||
const path = await resolveGitConfig(dir, opts); | ||
const ini = await readFile(path, "utf8"); | ||
return parseGitConfig(ini); | ||
} | ||
|
||
/** | ||
* Stringifies git config object into INI text format and writes it to a file. | ||
*/ | ||
export async function writeGitConfig(path: string, config: GitConfig) { | ||
await writeFile(path, stringifyGitConfig(config)); | ||
} | ||
|
||
/** | ||
* Parses a git config file in INI text format into a JavaScript object. | ||
*/ | ||
export function parseGitConfig(ini: string): GitConfig { | ||
return parseINI(ini.replaceAll(/^\[(\w+) "(.+)"\]$/gm, "[$1.$2]")); | ||
} | ||
|
||
/** | ||
* Stringifies a git config object into a git config file INI text format. | ||
*/ | ||
export function stringifyGitConfig(config: GitConfig): string { | ||
return stringifyINI(config).replaceAll(/^\[(\w+)\.(\w+)\]$/gm, '[$1 "$2"]'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[core] | ||
repositoryformatversion = 0 | ||
filemode = true | ||
bare = false | ||
logallrefupdates = true | ||
ignorecase = true | ||
precomposeunicode = true | ||
|
||
[branch "main"] | ||
remote = origin | ||
merge = refs/heads/main | ||
|
||
[branch "develop"] | ||
remote = origin | ||
merge = refs/heads/develop | ||
|
||
[remote "origin"] | ||
url = https://github.com/username/repo.git | ||
fetch = +refs/heads/*:refs/remotes/origin/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters