@simbo/git-changes 2.1.2 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,37 @@
1
1
  # @simbo/git-changes
2
2
 
3
+ ## 3.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 6f304e0: Include the original sources in source maps (`inlineSources`), so
8
+ debuggers and stack traces show the TypeScript code without publishing `src`.
9
+ - 760573e: Update `execa` to ^10.2.1.
10
+ - Updated dependencies [6f304e0]
11
+ - @simbo/find-git-repository-root@2.0.1
12
+
13
+ ## 3.0.0
14
+
15
+ ### Major Changes
16
+
17
+ - 97bc732: Fix and extend how Git changes are read and parsed.
18
+
19
+ - Renamed and copied files are now reported with the new path as `path` and
20
+ the original path as `originPath`. Previously, both paths were swapped.
21
+ - Untracked directories are now reported as individual files instead of a
22
+ single `dir/` entry, also when `status.showUntrackedFiles` is configured
23
+ otherwise.
24
+ - Add `GitChangeStatus.TypeChanged` (`T`) for files whose type changed, such
25
+ as a file replaced by a symlink. These files were previously missing.
26
+ - Add `GitChangeStatus.Unmerged` (`U`) for files with merge conflicts. Both
27
+ `staged` and `unstaged` are set to `Unmerged`. These files were previously
28
+ missing, and files added or deleted on both sides were reported as `Added`
29
+ or `Deleted`.
30
+
31
+ To migrate, remove workarounds that swap `path` and `originPath` or expand
32
+ untracked directories, and handle the new statuses where you check all status
33
+ values.
34
+
3
35
  ## 2.1.2
4
36
 
5
37
  ### Patch Changes
package/README.md CHANGED
@@ -20,12 +20,65 @@ npm i [-D] @simbo/git-changes
20
20
  ## Usage
21
21
 
22
22
  ```ts
23
- import { getGitChanges } from '@simbo/git-changes';
23
+ import { getGitChanges, GitChangeStatus } from '@simbo/git-changes';
24
24
 
25
25
  // get a map of changed files in the repository located at /path/to/repo
26
26
  const changes = await getGitChanges('/path/to/repo');
27
+
28
+ for (const change of changes.values()) {
29
+ if (change.staged === GitChangeStatus.Renamed) {
30
+ console.log(`${change.originPath} → ${change.path}`);
31
+ }
32
+ }
33
+ ```
34
+
35
+ `getGitChanges()` accepts any directory inside the repository and defaults to
36
+ the current working directory. It reads `git status --porcelain --null` and
37
+ returns a `ReadonlyMap` from file path to change. Example:
38
+
39
+ ```ts
40
+ Map {
41
+ 'new.txt' => { path: 'new.txt', originPath: 'old.txt', staged: 'R', unstaged: undefined },
42
+ 'src/index.ts' => { path: 'src/index.ts', staged: 'M', unstaged: 'M' },
43
+ 'docs/draft.md' => { path: 'docs/draft.md', staged: undefined, unstaged: '?' },
44
+ }
27
45
  ```
28
46
 
47
+ ### Changes
48
+
49
+ Each change has these properties:
50
+
51
+ | Property | Description |
52
+ | ------------ | --------------------------------------------------------------------------- |
53
+ | `path` | File path relative to the repository root; the new path for renames/copies. |
54
+ | `originPath` | Original path relative to the repository root; only for renames/copies. |
55
+ | `staged` | Status in the index, or `undefined` if the file has no staged change. |
56
+ | `unstaged` | Status in the working tree, or `undefined` if it has no unstaged change. |
57
+
58
+ ### Statuses
59
+
60
+ | `GitChangeStatus` | Value | Meaning |
61
+ | ----------------- | ----- | ---------------------------------------------- |
62
+ | `Added` | `A` | File was added. |
63
+ | `Modified` | `M` | File was modified. |
64
+ | `TypeChanged` | `T` | File type changed, such as file to symlink. |
65
+ | `Deleted` | `D` | File was deleted. |
66
+ | `Renamed` | `R` | File was renamed; see `originPath`. |
67
+ | `Copied` | `C` | File was copied; see `originPath`. |
68
+ | `Unmerged` | `U` | File has merge conflicts; set for both fields. |
69
+ | `Untracked` | `?` | File is not tracked; only set for `unstaged`. |
70
+
71
+ ### Notes
72
+
73
+ - Untracked directories are listed as individual files, regardless of the
74
+ `status.showUntrackedFiles` Git configuration. Ignored files are not listed.
75
+ - Copies are only detected if Git is configured to detect them
76
+ (`status.renames=copies`).
77
+ - Files with merge conflicts (for example both modified or both added) have
78
+ `staged` and `unstaged` set to `Unmerged`.
79
+ - The lower-level helpers `parseGitStatusOutput()` and `parseGitChangeStatus()`
80
+ are exported to parse existing `git status --porcelain --null` output.
81
+
29
82
  For a complete API reference, see the
30
83
  [documentation](https://simbo.de/packages/modules/_simbo_git-changes/).
31
84
 
@@ -17,19 +17,23 @@ export async function getGitChanges(workingDir = cwd()) {
17
17
  /**
18
18
  * This command provides a machine-readable, null-separated output of the
19
19
  * current status of the Git repository, including staged and unstaged changes,
20
- * as well as untracked files.
20
+ * as well as untracked files. Untracked directories are listed as individual
21
+ * files, regardless of the `status.showUntrackedFiles` configuration.
22
+ * Paths are relative to the repository root.
21
23
  *
22
- * Example output:
24
+ * Example output (renamed and copied entries list the new path first):
23
25
  * '?? untracked.ts\0'
24
26
  * ' M unstaged-modified.ts\0'
25
27
  * 'M staged-modified.ts\0'
26
28
  * 'MM both-modified.ts\0'
27
29
  * ' D unstaged-deleted.ts\0'
28
30
  * 'A staged-added.ts\0'
29
- * 'R old.ts\0new.ts\0'
30
- * 'C source.ts\0copied.ts\0'
31
+ * ' T type-changed.ts\0'
32
+ * 'UU conflicted.ts\0'
33
+ * 'R new.ts\0old.ts\0'
34
+ * 'C copied.ts\0source.ts\0'
31
35
  */
32
- const { stdout: output } = await execa({ cwd: repoRoot }) `git status --porcelain --short --null`;
36
+ const { stdout: output } = await execa({ cwd: repoRoot }) `git status --porcelain --null --untracked-files=all`;
33
37
  return parseGitStatusOutput(output);
34
38
  }
35
39
  catch (error) {
@@ -1 +1 @@
1
- {"version":3,"file":"get-git-changes.js","sourceRoot":"","sources":["../src/get-git-changes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AACxE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAG1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEpE;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,UAAU,GAAG,GAAG,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QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,kBAAkB,UAAU,oCAAoC,CAAC,CAAC;IACpF,CAAC;IAED,IAAI,CAAC;QACH;;;;;;;;;;;;;;;;;;WAkBG;QACH,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,KAAK,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,CAAA,qDAAqD,CAAC;QAE/G,OAAO,oBAAoB,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,MAAM,GACV,KAAK,YAAY,UAAU;YACzB,CAAC,CAAC,KAAK,CAAC,YAAY;YACpB,CAAC,CAAC,KAAK,YAAY,KAAK;gBACtB,CAAC,CAAC,KAAK,CAAC,OAAO;gBACf,CAAC,CAAC,kBAAkB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;QAC3C,MAAM,IAAI,KAAK,CAAC,8BAA8B,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IAC5E,CAAC;AACH,CAAC","sourcesContent":["import { cwd } from 'node:process';\n\nimport { findGitRepositoryRoot } from '@simbo/find-git-repository-root';\nimport { execa, ExecaError } from 'execa';\n\nimport type { GitChange } from './git-changes.types.js';\nimport { parseGitStatusOutput } from './parse-git-status-output.js';\n\n/**\n * Gets the uncommitted Git changes in the specified working directory.\n *\n * @param workingDir - The working directory of the Git repository. Defaults to the current working directory.\n * @returns A ReadonlyMap of file paths to their corresponding GitChange objects.\n */\nexport async function getGitChanges(workingDir = cwd()): Promise<ReadonlyMap<string, GitChange>> {\n const repoRoot = await findGitRepositoryRoot(workingDir);\n\n if (!repoRoot) {\n throw new Error(`The directory \"${workingDir}\" is not part of a Git repository.`);\n }\n\n try {\n /**\n * This command provides a machine-readable, null-separated output of the\n * current status of the Git repository, including staged and unstaged changes,\n * as well as untracked files. Untracked directories are listed as individual\n * files, regardless of the `status.showUntrackedFiles` configuration.\n * Paths are relative to the repository root.\n *\n * Example output (renamed and copied entries list the new path first):\n * '?? untracked.ts\\0'\n * ' M unstaged-modified.ts\\0'\n * 'M staged-modified.ts\\0'\n * 'MM both-modified.ts\\0'\n * ' D unstaged-deleted.ts\\0'\n * 'A staged-added.ts\\0'\n * ' T type-changed.ts\\0'\n * 'UU conflicted.ts\\0'\n * 'R new.ts\\0old.ts\\0'\n * 'C copied.ts\\0source.ts\\0'\n */\n const { stdout: output } = await execa({ cwd: repoRoot })`git status --porcelain --null --untracked-files=all`;\n\n return parseGitStatusOutput(output);\n } catch (error) {\n const reason =\n error instanceof ExecaError\n ? error.shortMessage\n : error instanceof Error\n ? error.message\n : `Unknown error (${String(error)})`;\n throw new Error(`Failed to get Git changes: ${reason}`, { cause: error });\n }\n}\n"]}
@@ -5,8 +5,10 @@
5
5
  export declare enum GitChangeStatus {
6
6
  Added = "A",
7
7
  Modified = "M",
8
+ TypeChanged = "T",
8
9
  Deleted = "D",
9
10
  Renamed = "R",
10
11
  Copied = "C",
12
+ Unmerged = "U",
11
13
  Untracked = "?"
12
14
  }
@@ -6,9 +6,11 @@ export var GitChangeStatus;
6
6
  (function (GitChangeStatus) {
7
7
  GitChangeStatus["Added"] = "A";
8
8
  GitChangeStatus["Modified"] = "M";
9
+ GitChangeStatus["TypeChanged"] = "T";
9
10
  GitChangeStatus["Deleted"] = "D";
10
11
  GitChangeStatus["Renamed"] = "R";
11
12
  GitChangeStatus["Copied"] = "C";
13
+ GitChangeStatus["Unmerged"] = "U";
12
14
  GitChangeStatus["Untracked"] = "?";
13
15
  })(GitChangeStatus || (GitChangeStatus = {}));
14
16
  //# sourceMappingURL=git-change-status.enum.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"git-change-status.enum.js","sourceRoot":"","sources":["../src/git-change-status.enum.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAN,IAAY,eAOX;AAPD,WAAY,eAAe;IACzB,8BAAW,CAAA;IACX,iCAAc,CAAA;IACd,gCAAa,CAAA;IACb,gCAAa,CAAA;IACb,+BAAY,CAAA;IACZ,kCAAe,CAAA;AACjB,CAAC,EAPW,eAAe,KAAf,eAAe,QAO1B"}
1
+ {"version":3,"file":"git-change-status.enum.js","sourceRoot":"","sources":["../src/git-change-status.enum.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAN,IAAY,eASX;AATD,WAAY,eAAe;IACzB,8BAAW,CAAA;IACX,iCAAc,CAAA;IACd,oCAAiB,CAAA;IACjB,gCAAa,CAAA;IACb,gCAAa,CAAA;IACb,+BAAY,CAAA;IACZ,iCAAc,CAAA;IACd,kCAAe,CAAA;AACjB,CAAC,EATW,eAAe,KAAf,eAAe,QAS1B","sourcesContent":["/**\n * Enum representing the possible statuses of a change in a Git repository.\n * Each status corresponds to a single character used in the output of `git status`.\n */\nexport enum GitChangeStatus {\n Added = 'A',\n Modified = 'M',\n TypeChanged = 'T',\n Deleted = 'D',\n Renamed = 'R',\n Copied = 'C',\n Unmerged = 'U',\n Untracked = '?',\n}\n"]}
@@ -2,7 +2,7 @@ import type { GitChangeStatus } from './git-change-status.enum.js';
2
2
  /**
3
3
  * Common Git change statuses for both staged and unstaged changes.
4
4
  */
5
- export type GitChangeStatusCommon = GitChangeStatus.Modified | GitChangeStatus.Deleted;
5
+ export type GitChangeStatusCommon = GitChangeStatus.Modified | GitChangeStatus.TypeChanged | GitChangeStatus.Deleted;
6
6
  /**
7
7
  * Git change statuses that include an origin path.
8
8
  */
@@ -15,20 +15,26 @@ export type GitChangeStatusAdded = GitChangeStatus.Added;
15
15
  * Untracked Git change status (only for unstaged changes).
16
16
  */
17
17
  export type GitChangeStatusUntracked = GitChangeStatus.Untracked;
18
+ /**
19
+ * Unmerged Git change status (always set for both staged and unstaged changes
20
+ * of a file with merge conflicts).
21
+ */
22
+ export type GitChangeStatusUnmerged = GitChangeStatus.Unmerged;
18
23
  /**
19
24
  * Combined type for staged changes field.
20
25
  */
21
- export type GitChangeStagedStatus = GitChangeStatusCommon | GitChangeStatusAdded | GitChangeStatusWithOriginPath;
26
+ export type GitChangeStagedStatus = GitChangeStatusCommon | GitChangeStatusAdded | GitChangeStatusWithOriginPath | GitChangeStatusUnmerged;
22
27
  /**
23
28
  * Combined type for unstaged changes field.
24
29
  */
25
- export type GitChangeUnstagedStatus = GitChangeStatusCommon | GitChangeStatusAdded | GitChangeStatusUntracked | GitChangeStatusWithOriginPath;
30
+ export type GitChangeUnstagedStatus = GitChangeStatusCommon | GitChangeStatusAdded | GitChangeStatusUntracked | GitChangeStatusWithOriginPath | GitChangeStatusUnmerged;
26
31
  /**
27
32
  * Interface for a change in a Git repository.
28
33
  */
29
34
  export interface GitChange {
30
35
  /**
31
- * The file path of the change.
36
+ * The file path of the change, relative to the repository root.
37
+ * For renamed or copied files, this is the new path.
32
38
  */
33
39
  path: string;
34
40
  /**
@@ -42,7 +48,8 @@ export interface GitChange {
42
48
  */
43
49
  unstaged?: GitChangeUnstagedStatus;
44
50
  /**
45
- * The original file path before the change (for renamed or copied files).
51
+ * The original file path before the change (for renamed or copied files),
52
+ * relative to the repository root.
46
53
  */
47
54
  originPath?: string;
48
55
  }
@@ -1 +1 @@
1
- {"version":3,"file":"git-changes.types.js","sourceRoot":"","sources":["../src/git-changes.types.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"git-changes.types.js","sourceRoot":"","sources":["../src/git-changes.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { GitChangeStatus } from './git-change-status.enum.js';\n\n/**\n * Common Git change statuses for both staged and unstaged changes.\n */\nexport type GitChangeStatusCommon = GitChangeStatus.Modified | GitChangeStatus.TypeChanged | GitChangeStatus.Deleted;\n\n/**\n * Git change statuses that include an origin path.\n */\nexport type GitChangeStatusWithOriginPath = GitChangeStatus.Renamed | GitChangeStatus.Copied;\n\n/**\n * Added Git change status.\n */\nexport type GitChangeStatusAdded = GitChangeStatus.Added;\n\n/**\n * Untracked Git change status (only for unstaged changes).\n */\nexport type GitChangeStatusUntracked = GitChangeStatus.Untracked;\n\n/**\n * Unmerged Git change status (always set for both staged and unstaged changes\n * of a file with merge conflicts).\n */\nexport type GitChangeStatusUnmerged = GitChangeStatus.Unmerged;\n\n/**\n * Combined type for staged changes field.\n */\nexport type GitChangeStagedStatus =\n GitChangeStatusCommon | GitChangeStatusAdded | GitChangeStatusWithOriginPath | GitChangeStatusUnmerged;\n\n/**\n * Combined type for unstaged changes field.\n */\nexport type GitChangeUnstagedStatus =\n | GitChangeStatusCommon\n | GitChangeStatusAdded\n | GitChangeStatusUntracked\n | GitChangeStatusWithOriginPath\n | GitChangeStatusUnmerged;\n\n/**\n * Interface for a change in a Git repository.\n */\nexport interface GitChange {\n /**\n * The file path of the change, relative to the repository root.\n * For renamed or copied files, this is the new path.\n */\n path: string;\n\n /**\n * The status of the change if it is staged.\n * This can be one of the GitChangeStatus values or undefined if not staged.\n */\n staged?: GitChangeStagedStatus;\n\n /**\n * The status of the change if it is unstaged.\n * This can be one of the GitChangeStatus values or undefined if not unstaged.\n */\n unstaged?: GitChangeUnstagedStatus;\n\n /**\n * The original file path before the change (for renamed or copied files),\n * relative to the repository root.\n */\n originPath?: string;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"git-status-constants.js","sourceRoot":"","sources":["../src/git-status-constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,CAAC;AAEpC;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAEpC;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,GAAG,mBAAmB,GAAG,CAAC,CAAC;AAE7E;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,IAAI,GAAG,CAAS,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAwB,CAAC"}
1
+ {"version":3,"file":"git-status-constants.js","sourceRoot":"","sources":["../src/git-status-constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,CAAC;AAEpC;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAEpC;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,GAAG,mBAAmB,GAAG,CAAC,CAAC;AAE7E;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,IAAI,GAAG,CAAS,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAwB,CAAC","sourcesContent":["import { GitChangeStatus } from './git-change-status.enum.js';\n\n/**\n * The separator used in the output of the Git status command.\n * This is a null character, which is used to separate entries in the output.\n */\nexport const ENTRY_SEPARATOR = '\\0';\n\n/**\n * The index in the Git change entry where the staged status is located.\n */\nexport const ENTRY_STATUS_INDEX = 0;\n\n/**\n * The length of the status string in a Git change entry.\n */\nexport const ENTRY_STATUS_LENGTH = 2;\n\n/**\n * The index in the Git change entry where the file path starts.\n */\nexport const ENTRY_PATH_INDEX = ENTRY_STATUS_INDEX + ENTRY_STATUS_LENGTH + 1;\n\n/**\n * A set of known GitChangeStatus values for validation purposes.\n */\nexport const KNOWN_GIT_CHANGE_STATUSES = new Set<string>(Object.values(GitChangeStatus)) as ReadonlySet<string>;\n"]}
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAE5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,8BAA8B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAE5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,8BAA8B,CAAC","sourcesContent":["export * from './get-git-changes.js';\nexport * from './git-change-status.enum.js';\nexport type * from './git-changes.types.js';\nexport * from './git-status-constants.js';\nexport * from './parse-git-change-status.js';\nexport * from './parse-git-status-output.js';\n"]}
@@ -8,6 +8,9 @@ import type { GitChange } from './git-changes.types.js';
8
8
  *
9
9
  * If the status is '??', it indicates an untracked file.
10
10
  *
11
+ * If the status is one of the unmerged combinations (DD, AU, UD, UA, DU, AA, UU),
12
+ * it indicates a file with merge conflicts and both statuses are `Unmerged`.
13
+ *
11
14
  * This should match the output of `git status --porcelain`.
12
15
  *
13
16
  * @param status - A two-character string representing the Git change status.
@@ -1,5 +1,16 @@
1
1
  import { GitChangeStatus } from './git-change-status.enum.js';
2
- const trackedStatuses = Object.values(GitChangeStatus).filter(value => value !== GitChangeStatus.Untracked);
2
+ const trackedStatuses = Object.values(GitChangeStatus).filter(value => value !== GitChangeStatus.Untracked && value !== GitChangeStatus.Unmerged);
3
+ /**
4
+ * Status combinations that mark a file with merge conflicts ("unmerged").
5
+ * - `DD`: both deleted
6
+ * - `AU`: added by us
7
+ * - `UD`: deleted by them
8
+ * - `UA`: added by them
9
+ * - `DU`: deleted by us
10
+ * - `AA`: both added
11
+ * - `UU`: both modified
12
+ */
13
+ const unmergedStatuses = new Set(['DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU']);
3
14
  /**
4
15
  * Parses a Git change status string into staged and unstaged statuses.
5
16
  *
@@ -9,6 +20,9 @@ const trackedStatuses = Object.values(GitChangeStatus).filter(value => value !==
9
20
  *
10
21
  * If the status is '??', it indicates an untracked file.
11
22
  *
23
+ * If the status is one of the unmerged combinations (DD, AU, UD, UA, DU, AA, UU),
24
+ * it indicates a file with merge conflicts and both statuses are `Unmerged`.
25
+ *
12
26
  * This should match the output of `git status --porcelain`.
13
27
  *
14
28
  * @param status - A two-character string representing the Git change status.
@@ -21,6 +35,9 @@ export function parseGitChangeStatus(status) {
21
35
  if (status.startsWith('??')) {
22
36
  return { staged: undefined, unstaged: GitChangeStatus.Untracked };
23
37
  }
38
+ if (unmergedStatuses.has(status.slice(0, 2))) {
39
+ return { staged: GitChangeStatus.Unmerged, unstaged: GitChangeStatus.Unmerged };
40
+ }
24
41
  const staged = trackedStatuses.find((value) => value === status[0]);
25
42
  const unstaged = trackedStatuses.find((value) => value === status[1]);
26
43
  return { staged, unstaged };
@@ -1 +1 @@
1
- {"version":3,"file":"parse-git-change-status.js","sourceRoot":"","sources":["../src/parse-git-change-status.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAG9D,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,KAAK,eAAe,CAAC,SAAS,CAAC,CAAC;AAE5G;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAc;IACjD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpD,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;IACpD,CAAC;IAED,IAAI,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5B,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,eAAe,CAAC,SAAS,EAAE,CAAC;IACpE,CAAC;IAED,MAAM,MAAM,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,KAAK,MAAM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EAAE,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,eAAe,CAAC,QAAQ,EAAE,CAAC;IAClF,CAAC;IAED,MAAM,MAAM,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5E,MAAM,QAAQ,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAE9E,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["import { GitChangeStatus } from './git-change-status.enum.js';\nimport type { GitChange } from './git-changes.types.js';\n\nconst trackedStatuses = Object.values(GitChangeStatus).filter(\n value => value !== GitChangeStatus.Untracked && value !== GitChangeStatus.Unmerged,\n);\n\n/**\n * Status combinations that mark a file with merge conflicts (\"unmerged\").\n * - `DD`: both deleted\n * - `AU`: added by us\n * - `UD`: deleted by them\n * - `UA`: added by them\n * - `DU`: deleted by us\n * - `AA`: both added\n * - `UU`: both modified\n */\nconst unmergedStatuses = new Set(['DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU']);\n\n/**\n * Parses a Git change status string into staged and unstaged statuses.\n *\n * The status string is expected to be at least two characters long, where:\n * - The first character represents the staged status.\n * - The second character represents the unstaged status.\n *\n * If the status is '??', it indicates an untracked file.\n *\n * If the status is one of the unmerged combinations (DD, AU, UD, UA, DU, AA, UU),\n * it indicates a file with merge conflicts and both statuses are `Unmerged`.\n *\n * This should match the output of `git status --porcelain`.\n *\n * @param status - A two-character string representing the Git change status.\n * @returns An object containing the staged and unstaged statuses.\n */\nexport function parseGitChangeStatus(status: string): Pick<GitChange, 'staged' | 'unstaged'> {\n if (typeof status !== 'string' || status.length < 2) {\n return { staged: undefined, unstaged: undefined };\n }\n\n if (status.startsWith('??')) {\n return { staged: undefined, unstaged: GitChangeStatus.Untracked };\n }\n\n if (unmergedStatuses.has(status.slice(0, 2))) {\n return { staged: GitChangeStatus.Unmerged, unstaged: GitChangeStatus.Unmerged };\n }\n\n const staged = trackedStatuses.find((value: string) => value === status[0]);\n const unstaged = trackedStatuses.find((value: string) => value === status[1]);\n\n return { staged, unstaged };\n}\n"]}
@@ -18,21 +18,21 @@ export function parseGitStatusOutput(output) {
18
18
  }
19
19
  const { staged, unstaged } = parseGitChangeStatus(entry.slice(ENTRY_STATUS_INDEX, ENTRY_STATUS_LENGTH));
20
20
  const path = entry.slice(ENTRY_PATH_INDEX);
21
- if (!path || (!staged && !unstaged)) {
22
- continue;
23
- }
24
- // For entries that are renamed or copied, the current entry contains the old path,
25
- // and the next entry contains the new path.
21
+ // Renamed or copied entries contain the new path, followed by an entry with the original path.
22
+ // The original path is consumed even if the entry is invalid, so it is never parsed as an entry.
26
23
  if (staged === GitChangeStatus.Renamed ||
27
24
  staged === GitChangeStatus.Copied ||
28
25
  unstaged === GitChangeStatus.Renamed ||
29
26
  unstaged === GitChangeStatus.Copied) {
30
- if (entries[index]) {
31
- const targetPath = entries[index++];
32
- changes.set(targetPath, { path: targetPath, originPath: path, staged, unstaged });
27
+ const originPath = entries[index++];
28
+ if (path && originPath) {
29
+ changes.set(path, { path, originPath, staged, unstaged });
33
30
  }
34
31
  continue;
35
32
  }
33
+ if (!path || (!staged && !unstaged)) {
34
+ continue;
35
+ }
36
36
  changes.set(path, { path, staged, unstaged });
37
37
  }
38
38
  return changes;
@@ -1 +1 @@
1
- {"version":3,"file":"parse-git-status-output.js","sourceRoot":"","sources":["../src/parse-git-status-output.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AACvH,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEpE;;;;;GAKG;A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AAC,IAAI,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;YAC5D,CAAC;YACD,SAAS;QACX,CAAC;QAED,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;YACpC,SAAS;QACX,CAAC;QAED,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { GitChangeStatus } from './git-change-status.enum.js';\nimport type { GitChange } from './git-changes.types.js';\nimport { ENTRY_PATH_INDEX, ENTRY_SEPARATOR, ENTRY_STATUS_INDEX, ENTRY_STATUS_LENGTH } from './git-status-constants.js';\nimport { parseGitChangeStatus } from './parse-git-change-status.js';\n\n/**\n * Parses the output of the Git status command and returns a map of Git changes.\n *\n * @param output - The output string from the Git status command.\n * @returns A ReadonlyMap of file paths to their corresponding GitChange objects.\n */\nexport function parseGitStatusOutput(output: string): ReadonlyMap<string, GitChange> {\n const changes = new Map<string, GitChange>();\n const entries = output.split(ENTRY_SEPARATOR);\n\n let index = 0;\n\n while (index < entries.length) {\n const entry = entries[index++];\n\n if (!entry) {\n continue;\n }\n\n const { staged, unstaged } = parseGitChangeStatus(entry.slice(ENTRY_STATUS_INDEX, ENTRY_STATUS_LENGTH));\n\n const path = entry.slice(ENTRY_PATH_INDEX);\n\n // Renamed or copied entries contain the new path, followed by an entry with the original path.\n // The original path is consumed even if the entry is invalid, so it is never parsed as an entry.\n if (\n staged === GitChangeStatus.Renamed ||\n staged === GitChangeStatus.Copied ||\n unstaged === GitChangeStatus.Renamed ||\n unstaged === GitChangeStatus.Copied\n ) {\n const originPath = entries[index++];\n if (path && originPath) {\n changes.set(path, { path, originPath, staged, unstaged });\n }\n continue;\n }\n\n if (!path || (!staged && !unstaged)) {\n continue;\n }\n\n changes.set(path, { path, staged, unstaged });\n }\n\n return changes;\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@simbo/git-changes",
3
3
  "description": "A library to read, parse and provide Git changes information from `git status` for a local repository.",
4
- "version": "2.1.2",
4
+ "version": "3.0.1",
5
5
  "keywords": [
6
6
  "git",
7
7
  "changes",
@@ -36,28 +36,24 @@
36
36
  "README.md"
37
37
  ],
38
38
  "dependencies": {
39
- "@simbo/find-git-repository-root": "2.0.0",
40
- "execa": "^10.2.0"
39
+ "@simbo/find-git-repository-root": "2.0.1",
40
+ "execa": "^10.2.1"
41
41
  },
42
42
  "devDependencies": {
43
- "@ianvs/prettier-plugin-sort-imports": "^4.7.1",
44
- "@simbo/eslint-config": "3.2.1",
45
- "@simbo/prettier-config": "3.0.1",
46
- "@simbo/tsconfig": "2.0.0",
43
+ "@simbo/eslint-config": "3.3.0",
44
+ "@simbo/tsconfig": "2.1.0",
47
45
  "@types/node": "^24.19.2",
48
- "@vitest/coverage-v8": "^5.0.5",
49
- "@vitest/ui": "^5.0.5",
50
- "eslint": "^10.13.0",
46
+ "@vitest/coverage-v8": "^5.1.0",
47
+ "@vitest/ui": "^5.1.0",
48
+ "eslint": "^10.14.0",
51
49
  "jiti": "^2.7.0",
52
- "prettier": "^3.9.10",
53
50
  "typescript": "^6.0.3",
54
- "vitest": "^5.0.5"
51
+ "vitest": "^5.1.0"
55
52
  },
56
53
  "scripts": {
57
54
  "build": "tsc --project tsconfig.build.json",
58
- "check": "pnpm run check:eslint && pnpm run check:format && pnpm run check:types",
55
+ "check": "pnpm run check:eslint && pnpm run check:types",
59
56
  "check:eslint": "eslint --config ./eslint.config.ts --max-warnings 0",
60
- "check:format": "prettier --check .",
61
57
  "check:types": "tsc --noEmit",
62
58
  "clean": "pnpm run clean:dist && pnpm run clean:coverage && pnpm run clean:turbo && pnpm run clean:node_modules",
63
59
  "clean:dist": "rm -rf dist",
@@ -65,7 +61,6 @@
65
61
  "clean:turbo": "rm -rf .turbo",
66
62
  "clean:coverage": "rm -rf coverage",
67
63
  "fix:eslint": "pnpm run check:eslint --fix",
68
- "fix:format": "prettier --write .",
69
64
  "test": "vitest run --coverage",
70
65
  "test:ui": "pnpm run test:watch --ui",
71
66
  "test:watch": "vitest dev --coverage"