Skip to content

Commit

Permalink
streamline a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
rindeal committed Sep 7, 2024
1 parent bf9591d commit 670a3e3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ npm install https://github.com/rindeal-js/git-remote-refs-cmp
```ts
import { gitRemoteRefsCmp } from 'git-remote-refs-cmp'

async function compareRemotes() {
(async () => {
const sourceRemote = 'https://github.com/source-repo.git'
const targetRemote = 'https://github.com/target-repo.git'
const diff = await gitRemoteRefsCmp(sourceRemote, targetRemote)
Expand All @@ -40,9 +40,47 @@ async function compareRemotes() {
} else {
console.log('No differences found.')
}
}
})()
```

If you want to run more than one comparison query in your script:

```ts
import { GitRemoteRefsCmp } from 'git-remote-refs-cmp'

(async () => {
const cmp = GitRemoteRefsCmp()
cmp.init()

compareRemotes()
const diff = cmp.compare(sourceRemote, targetRemote)
const diff2 = cmp.compare(sourceRemote2, targetRemote2)
...
})()
```

For a lower level access and handling:

```ts
import {
GitCommandManager,
GitLsRemoteParser,
GitLsRemoteOutputCmp,
} from 'git-remote-refs-cmp'

(async () => {
const git = new GitCommandManager()
await git.init()
const parser = new GitLsRemoteParser()
const lsRemoteCmp = new GitLsRemoteOutputCmp()

let [source, target] = await Promise.all(
[sourceRemote, targetRemote].map(async (remote) =>
parser.parse(await git.lsRemote({ remote }), remote)
)
)
let diff = this.lsRemoteCmp.compare(source, target)
...
})()
```

## ⚠️ Limitations
Expand Down
17 changes: 14 additions & 3 deletions src/GitRemoteRefsCmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,26 @@ class GitRemoteRefsCmp {
await this.git.init()
}

public isInitialized(): boolean {
return this.git.isInitialized()
}

public async ensureIsInitialized() {
if ( ! this.isInitialized() ) {
await this.init()
}
}

private async processRemote(remote: string): Promise<GitLsRemoteOutput> {
const rawOutput = await this.git.lsRemote({ remote })
return this.parser.parse(rawOutput, remote)
}

public async compare(sourceRemote: string, targetRemote: string): Promise<GitLsRemoteRefDiff | null> {
console.assert(this.git.isInitialized())
const promises = [sourceRemote, targetRemote].map(this.processRemote)
const [source, target] = await Promise.all(promises)
await this.ensureIsInitialized()
const [source, target] = await Promise.all(
[sourceRemote, targetRemote].map(this.processRemote)
)
return this.lsRemoteCmp.compare(source, target)
}
}
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import {
} from './GitRemoteRefsCmp'


export type {
GitCommandManager,
} from './GitCommandManager'
export type {
GitLsRemoteOutput,
} from './GitLsRemoteOutput'
export type {
GitLsRemoteOutputCmp,
} from './GitLsRemoteOutputCmp'
export type {
GitLsRemoteParser,
} from './GitLsRemoteParser'
export type {
GitLsRemoteRefDiff,
GitLsRemoteZeroRefsDiff,
Expand Down

0 comments on commit 670a3e3

Please sign in to comment.