DRY flatPatches function

22ad3bffd34dae766285375b6c6a48c4f54bc285

Tucker McKnight <tmcknight@instructure.com> | Sat Jun 20 2026

DRY flatPatches function
src/flatPatches.ts:13
Before
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
  : Promise<Array<FlatPatchRecord>> => {
  if (cachedFlatPatches !== null) { return cachedFlatPatches }

⁣
⁣
  cachedFlatPatches = repos.flatMap((repo) => {
    const branches = repo.branches.flatMap((branch) => {
      const flatPatches: Array<FlatPatchRecord> = []
      let currentCommit: ReturnType<Repository['commits']['get']> | undefined = repo.commits.get(branch.sha)

      while (currentCommit !== undefined) {
        flatPatches.push({
          type: "branch",
          commit: currentCommit,
          repoName: repo.name,
          refName: branch.name
        })

        currentCommit = repo.commits.get(currentCommit.parent)
      }

      return flatPatches
    })

⁣
⁣
    const tags = repo.tags.flatMap((tag) => {
      const flatPatches: Array<FlatPatchRecord> = []
      let currentCommit: ReturnType<Repository['commits']['get']> = repo.commits.get(tag.sha)
      while (currentCommit !== undefined) {
        flatPatches.push({
          type: "tag",
          commit: currentCommit,
          repoName: repo.name,
          refName: tag.name,
        })

        currentCommit = repo.commits.get(currentCommit.parent)
      }

      return flatPatches
    })

    return [...branches, ...tags]
  })
After
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
  : Promise<Array<FlatPatchRecord>> => {
  if (cachedFlatPatches !== null) { return cachedFlatPatches }

  const itemsForRef: (
    ref: Repository['tags'][0], refType: 'branch' | 'tag', repo: Repository
⁣
  ) => Array<FlatPatchRecord> = (ref, refType, repo) => {
    const flatPatches: Array<FlatPatchRecord> = []
    let currentCommit: ReturnType<Repository['commits']['get']> | undefined = repo.commits.get(ref.sha)

    while (currentCommit !== undefined) {
      flatPatches.push({
        type: refType,
        commit: currentCommit,
        repoName: repo.name,
        refName: ref.name
      })

      currentCommit = repo.commits.get(currentCommit.parent)
    }

    return flatPatches
  }



  cachedFlatPatches = repos.flatMap((repo) => {
⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
    const branches = repo.branches.flatMap(branch => itemsForRef(branch, 'branch', repo))

⁣
⁣
⁣
⁣
    const tags = repo.tags.flatMap(tag => itemsForRef(tag, 'tag', repo))

    return [...branches, ...tags]
  })