Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include deletions in modified files mask #718

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
191 changes: 178 additions & 13 deletions docs/src/content/docs/api/namespaces/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ All content is auto-generated using a oneRepo command:
-->

<!-- start-onerepo-sentinel -->
<!-- @generated SignedSource<<1665a5998f94c28ec13035a22848de34>> -->
<!-- @generated SignedSource<<9114277ef130070f1052217a1aa66068>> -->

Special handlers for managing complex queries and manipulation of the git repository's state.

Expand Down Expand Up @@ -74,10 +74,134 @@ To restore the unstaged changes, call [`restoreUnstaged()`](#restoreunstaged).

## Type Aliases

### ModifiedFromThrough
### ModifiedBaseOptions\<ByStatus\>

```ts
type ModifiedFromThrough: {
type ModifiedBaseOptions<ByStatus>: {
allStatus: boolean;
byStatus: ByStatus;
};
```

**Type parameters:**

| Type parameter | Value |
| :--------------------------- | :------ |
| `ByStatus` extends `boolean` | `false` |

#### Type declaration

##### allStatus?

```ts
optional allStatus: boolean;
```

By default, this function will not return `deleted` and `unmerged` files unless either `allStatus` or `byStatus` is set to `true`

##### byStatus?

```ts
optional byStatus: ByStatus;
```

Return modified files categorized by the [type of modification](#modifiedbystatus) (added, deleted, modified, etc)

**Source:** [modules/git/src/index.ts](https://github.com/paularmstrong/onerepo/blob/main/modules/git/src/index.ts)

---

### ModifiedByStatus

```ts
type ModifiedByStatus: {
added: string[];
copied: string[];
deleted: string[];
fileTypeChanged: string[];
modified: string[];
renamed: string[];
unknown: string[];
unmerged: string[];
};
```

This type defines the different statuses of files when running a git-diff. More information around the file statuses can be found in the official git documentation for [git-diff](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-git-diff-filesltpatterngt82308203).

#### Type declaration

##### added

```ts
added: string[];
```

Git status `A`: addition of a file

##### copied

```ts
copied: string[];
```

Git status `C`: copy of a file into a new one

##### deleted

```ts
deleted: string[];
```

Git status `D`: deletion of a file

##### fileTypeChanged

```ts
fileTypeChanged: string[];
```

Git status `T`: change in the type of the file (regular file, symbolic link or submodule)

##### modified

```ts
modified: string[];
```

Git status `M`: modification of the contents or mode of a file

##### renamed

```ts
renamed: string[];
```

Git status `R`: renaming of a file

##### unknown

```ts
unknown: string[];
```

Git status `X`: addition of a file

##### unmerged

```ts
unmerged: string[];
```

Git status `U`: "unknown" change type (most probably a bug, please report it)

**Source:** [modules/git/src/index.ts](https://github.com/paularmstrong/onerepo/blob/main/modules/git/src/index.ts)

---

### ModifiedFromThrough\<ByStatus\>

```ts
type ModifiedFromThrough<ByStatus>: ModifiedBaseOptions<ByStatus> & {
from: string;
staged: false;
through: string;
Expand Down Expand Up @@ -110,14 +234,20 @@ optional through: string;

Git ref for end (inclusive) to get list of modified files

**Type parameters:**

| Type parameter |
| :--------------------------- |
| `ByStatus` extends `boolean` |

**Source:** [modules/git/src/index.ts](https://github.com/paularmstrong/onerepo/blob/main/modules/git/src/index.ts)

---

### ModifiedStaged
### ModifiedStaged\<ByStatus\>

```ts
type ModifiedStaged: {
type ModifiedStaged<ByStatus>: ModifiedBaseOptions<ByStatus> & {
from: never;
staged: true;
through: never;
Expand Down Expand Up @@ -150,6 +280,12 @@ optional through: never;

Disallowed when `staged: true`

**Type parameters:**

| Type parameter |
| :--------------------------- |
| `ByStatus` extends `boolean` |

**Source:** [modules/git/src/index.ts](https://github.com/paularmstrong/onerepo/blob/main/modules/git/src/index.ts)

---
Expand Down Expand Up @@ -296,24 +432,53 @@ const mergeBase = await getMergeBase();
### getModifiedFiles()

```ts
getModifiedFiles(modified?, options?): Promise<string[]>
getModifiedFiles<ByStatus>(modified?, options?): Promise<ByStatus extends true ? ModifiedByStatus : string[]>
```

Get a map of the currently modified files based on their status. If `from` and `through` are not provided, this will current merge-base determination to best get the change to the working tree using `git diff` and `git diff-tree`.
Get a map of the currently modified files based on their status. If `from` and `through` are not provided, this will use merge-base determination to get the changes to the working tree using `git diff` and `git diff-tree`.

By default, this function will not return `deleted` and `unmerged` files. If you wish to include files with those statuses, set the option `allStatus: true` or get a map of all files by status using `byStatus: true`.

```ts
const changesSinceMergeBase = await git.getModifiedFiles();
const betweenRefs = await git.getModifiedFiles('v1.2.3', 'v2.0.0');
const betweenRefs = await git.getModifiedFiles({ from: 'v1.2.3', through: 'v2.0.0' });
```

Get modified files categorized by modification type:

```ts
const allChanges = await git.getModifiedFiles({ byStatus: true });
```

Will result in `allChanges` equal to an object containing arrays of strings:

```ts
{
added: [/* ... */],
copied: [/* ... */],
modified: [/* ... */],
deleted: [/* ... */],
renamed: [/* ... */],
fileTypeChanged: [/* ... */],
unmerged: [/* ... */],
unknown: [/* ... */],
}
```

**Type parameters:**

| Type parameter | Value |
| :--------------------------- | :------ |
| `ByStatus` extends `boolean` | `false` |

**Parameters:**

| Parameter | Type |
| :---------- | :----------------------------------------------------------------------------------- |
| `modified`? | [`ModifiedStaged`](#modifiedstaged) \| [`ModifiedFromThrough`](#modifiedfromthrough) |
| `options`? | [`Options`](#options) |
| Parameter | Type |
| :---------- | :------------------------------------------------------------------------------------------------------------------------------- |
| `modified`? | [`ModifiedStaged`](#modifiedstagedbystatus)\<`ByStatus`\> \| [`ModifiedFromThrough`](#modifiedfromthroughbystatus)\<`ByStatus`\> |
| `options`? | [`Options`](#options) |

**Returns:** `Promise`\<`string`[]\>
**Returns:** `Promise`\<`ByStatus` extends `true` ? [`ModifiedByStatus`](#modifiedbystatus) : `string`[]\>
**Source:** [modules/git/src/index.ts](https://github.com/paularmstrong/onerepo/blob/main/modules/git/src/index.ts)

---
Expand Down
5 changes: 5 additions & 0 deletions modules/builders/.changes/000-evil-groups-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
type: patch
---

Includes more file statuses in `getFilepaths` and determining workspace changes.
3 changes: 2 additions & 1 deletion modules/builders/src/getters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path';
import { minimatch } from 'minimatch';
import { stepWrapper } from '@onerepo/logger';
import type { ModifiedFromThrough, ModifiedStaged } from '@onerepo/git';
import { getModifiedFiles } from '@onerepo/git';
import type { Graph, Workspace } from '@onerepo/graph';
import type { LogStep } from '@onerepo/logger';
Expand Down Expand Up @@ -213,7 +214,7 @@ export async function getFilepaths(

if ('affected' in argv && argv.affected) {
if (!workspaces.length) {
let opts: Parameters<typeof getModifiedFiles>[0];
let opts: ModifiedFromThrough<false> | ModifiedStaged<false>;
if (staged || argv.staged) {
opts = { staged: true };
} else {
Expand Down
5 changes: 5 additions & 0 deletions modules/git/.changes/000-full-pears-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
type: patch
---

The git.getModifiedFiles results now includes deleted files
6 changes: 6 additions & 0 deletions modules/git/.changes/001-new-monkeys-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
type: minor
---

Added a git utility method to get modified files and bucket them based on the modification operation.
Updated tasks command to use the new git getModifiedByStatus method so deleted files are included as changes when generating tasks.
Loading
Loading