@apexdevtools/git-ops 1.0.1 → 1.1.0

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,4 +1,13 @@
1
- # test-runner - Changelog
1
+ # git-ops - Changelog
2
+
3
+ ## 1.1.0 - 2023-03-21
4
+
5
+ - add new method `getDiffRange`
6
+ - add new method `getLocalChanges`
7
+
8
+ ## 1.0.2 - 2023-03-20
9
+
10
+ - downgrade min git version to 2.30.0
2
11
 
3
12
  ## 1.0.1 - 2023-03-14
4
13
 
package/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Library to do git operations to find changed files in a given git repository.
4
4
 
5
+ ## Prerequisite
6
+
7
+ Minimum `git` version of `2.30.0` must already be installed on the host machine.
8
+
5
9
  ## Getting Started
6
10
 
7
11
  To build run
@@ -34,14 +38,40 @@ The output of the command is same as running `git diff branchName...ref` combine
34
38
 
35
39
  **Note:** files with the status of deleted (`D`) and ignored (`!`) will not be included in the change set.
36
40
 
41
+ ### `getDefaultBranchDiffByRef`
42
+
43
+ Works out changed files using the default branch in that repo given a ref.
44
+ This find the default branch in the repo using `git symbolic-ref 'refs/remotes/origin/HEAD'`
45
+ so the `HEAD` must be set. The output of the command is same as running `git diff branchName...ref` combined with `git status`.
46
+
47
+ Files with the status of deleted (`D`) and ignored (`!`) will not be included in the change set.
48
+
37
49
  ```TypeScript
38
- getDefaultBranchDiffByRef(repoRootDir: string, ref: string): Promise<Set<string>>
50
+ getDefaultBranchDiffByRef(repoDir: string, ref: string): Promise<Set<string>>
39
51
  ```
40
52
 
41
- Getting changed files using the default branch in that repo.
53
+ ### `getDefaultBranchDiff`
42
54
 
43
55
  This command is same as calling `getDefaultBranchDiffByRef(repoRootDir, 'HEAD')`.
44
56
 
45
57
  ```TypeScript
46
- getDefaultBranchDiff(repoRootDir: string): Promise<Set<string>>
58
+ getDefaultBranchDiff(repoDir: string): Promise<Set<string>>
59
+ ```
60
+
61
+ ### `getDiffRange`
62
+
63
+ Works out the diff between a given range. Equivalent to `git diff ref1...ref2`
64
+
65
+ ```Typescript
66
+ getDiffRange(dir: string, fromRef: string, toRef: string): Promise<Set<string>>
67
+ ```
68
+
69
+ ### `getLocalChanges`
70
+
71
+ Get the local changes that not have been committed. Equivalent to `git status`
72
+
73
+ Files with the status of deleted (`D`) and ignored (`!`) will not be included in the change set
74
+
75
+ ```TypeScript
76
+ getLocalChanges(dir: string): Promise<Set<string>>
47
77
  ```
@@ -11,7 +11,22 @@ export declare function getDefaultBranchDiff(dir: string): Promise<Set<string>>;
11
11
  * combined with `git status`.
12
12
  * Files with the status of deleted (`D`) and ignored (`!`) will not be included in the change set.
13
13
  * @param dir: string of the directory thr operation should be performed on
14
- * @param red: string git ref. i.e HEAD, or commit hash
14
+ * @param refTo: string git ref. i.e HEAD, or commit hash
15
15
  * @returns set of absolute paths of changed files
16
16
  */
17
- export declare function getDefaultBranchDiffByRef(dir: string, ref: string): Promise<Set<string>>;
17
+ export declare function getDefaultBranchDiffByRef(dir: string, refTo: string): Promise<Set<string>>;
18
+ /**
19
+ * Works out the diff between a given range. Equivalent to `git diff ref1...ref2`
20
+ * @param dir string of the directory thr operation should be performed on
21
+ * @param fromRef string git ref. i.e HEAD, or commit hash
22
+ * @param toRef string git ref. i.e HEAD, or commit hash
23
+ * @returns set of absolute paths of changed files
24
+ */
25
+ export declare function getDiffRange(dir: string, fromRef: string, toRef: string): Promise<Set<string>>;
26
+ /**
27
+ * Get the local changes that not have been committed. Equivalent to `git status`
28
+ * Files with the status of deleted (`D`) and ignored (`!`) will not be included in the change set.
29
+ * @param dir tring of the directory thr operation should be performed on
30
+ * @returns set of absolute paths of un committed files
31
+ */
32
+ export declare function getLocalChanges(dir: string): Promise<Set<string>>;
@@ -15,7 +15,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  return (mod && mod.__esModule) ? mod : { "default": mod };
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.getDefaultBranchDiffByRef = exports.getDefaultBranchDiff = void 0;
18
+ exports.getLocalChanges = exports.getDiffRange = exports.getDefaultBranchDiffByRef = exports.getDefaultBranchDiff = void 0;
19
19
  const path_1 = __importDefault(require("path"));
20
20
  const Git_1 = require("../Git/Git");
21
21
  /**
@@ -36,35 +36,67 @@ exports.getDefaultBranchDiff = getDefaultBranchDiff;
36
36
  * combined with `git status`.
37
37
  * Files with the status of deleted (`D`) and ignored (`!`) will not be included in the change set.
38
38
  * @param dir: string of the directory thr operation should be performed on
39
- * @param red: string git ref. i.e HEAD, or commit hash
39
+ * @param refTo: string git ref. i.e HEAD, or commit hash
40
40
  * @returns set of absolute paths of changed files
41
41
  */
42
- function getDefaultBranchDiffByRef(dir, ref) {
42
+ function getDefaultBranchDiffByRef(dir, refTo) {
43
43
  return __awaiter(this, void 0, void 0, function* () {
44
44
  const git = new Git_1.Git(dir);
45
- const root = yield git.gitRoot();
46
45
  return git
47
46
  .getDefaultBranchName()
48
- .then((branchName) => __awaiter(this, void 0, void 0, function* () {
49
- return yield getChanges(git, branchName, ref);
50
- }))
51
- .then(changes => {
52
- const fullPaths = [...changes].map(p => path_1.default.resolve(path_1.default.join(root, p)));
53
- return new Set(fullPaths);
54
- })
47
+ .then(branch => getDiffRange(dir, branch, refTo))
55
48
  .catch(er => {
56
- if (er instanceof Error)
57
- throw Error(`Failed getting diff: ${er.message}`);
58
- else
59
- throw Error('Failed getting diff');
49
+ throw new NoLocalBranchException(er);
60
50
  });
61
51
  });
62
52
  }
63
53
  exports.getDefaultBranchDiffByRef = getDefaultBranchDiffByRef;
64
- function getChanges(git, branchName, ref) {
54
+ /**
55
+ * Works out the diff between a given range. Equivalent to `git diff ref1...ref2`
56
+ * @param dir string of the directory thr operation should be performed on
57
+ * @param fromRef string git ref. i.e HEAD, or commit hash
58
+ * @param toRef string git ref. i.e HEAD, or commit hash
59
+ * @returns set of absolute paths of changed files
60
+ */
61
+ function getDiffRange(dir, fromRef, toRef) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const git = new Git_1.Git(dir);
64
+ const root = yield git.gitRoot();
65
+ return getDiffChanges(git, fromRef, toRef)
66
+ .then(changes => resolvePaths(changes, root))
67
+ .catch(er => {
68
+ throw new DiffFailedException(er);
69
+ });
70
+ });
71
+ }
72
+ exports.getDiffRange = getDiffRange;
73
+ /**
74
+ * Get the local changes that not have been committed. Equivalent to `git status`
75
+ * Files with the status of deleted (`D`) and ignored (`!`) will not be included in the change set.
76
+ * @param dir tring of the directory thr operation should be performed on
77
+ * @returns set of absolute paths of un committed files
78
+ */
79
+ function getLocalChanges(dir) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ const git = new Git_1.Git(dir);
82
+ const root = yield git.gitRoot();
83
+ return git
84
+ .getLocalChangedAndCreated()
85
+ .then(changes => resolvePaths(changes, root))
86
+ .catch(er => {
87
+ throw new LocalChangeException(er);
88
+ });
89
+ });
90
+ }
91
+ exports.getLocalChanges = getLocalChanges;
92
+ function resolvePaths(paths, root) {
93
+ const fullPaths = [...paths].map(p => path_1.default.resolve(path_1.default.join(root, p)));
94
+ return new Set(fullPaths);
95
+ }
96
+ function getDiffChanges(git, ref1, ref2) {
65
97
  return __awaiter(this, void 0, void 0, function* () {
66
98
  const changes = yield Promise.all([
67
- git.diffRange(branchName, ref),
99
+ git.diffRange(ref1, ref2),
68
100
  git.getLocalChangedAndCreated(),
69
101
  ]);
70
102
  const allChanges = new Set();
@@ -72,3 +104,28 @@ function getChanges(git, branchName, ref) {
72
104
  return allChanges;
73
105
  });
74
106
  }
107
+ class GitException extends Error {
108
+ constructor(msg, err) {
109
+ if (err instanceof Error) {
110
+ super(`${msg}. Cause: '${err.message}'`);
111
+ }
112
+ else {
113
+ super(msg);
114
+ }
115
+ }
116
+ }
117
+ class NoLocalBranchException extends GitException {
118
+ constructor(err) {
119
+ super('Local branch operation failed', err);
120
+ }
121
+ }
122
+ class LocalChangeException extends GitException {
123
+ constructor(err) {
124
+ super('Getting local changes operation failed', err);
125
+ }
126
+ }
127
+ class DiffFailedException extends GitException {
128
+ constructor(err) {
129
+ super('Getting diff operation failed', err);
130
+ }
131
+ }
@@ -16,7 +16,7 @@ export declare class Git implements IGit {
16
16
  private static MIN_GIT_VERSION_MAJOR;
17
17
  private static MIN_GIT_VERSION_MINOR;
18
18
  private static MIN_GIT_VERSION_PATCH;
19
- private static gitInstance;
19
+ private gitInstance;
20
20
  private dir;
21
21
  constructor(dir: string);
22
22
  static versionCheck(git: SimpleGit): Promise<void>;
@@ -29,6 +29,7 @@ var FileStatus;
29
29
  })(FileStatus = exports.FileStatus || (exports.FileStatus = {}));
30
30
  class Git {
31
31
  constructor(dir) {
32
+ this.gitInstance = undefined;
32
33
  this.dir = dir;
33
34
  }
34
35
  static versionCheck(git) {
@@ -42,12 +43,12 @@ class Git {
42
43
  });
43
44
  }
44
45
  get git() {
45
- if (Git.gitInstance)
46
- return Promise.resolve(Git.gitInstance);
46
+ if (this.gitInstance)
47
+ return Promise.resolve(this.gitInstance);
47
48
  //eslint-disable-next-line
48
49
  const git = (0, simple_git_1.simpleGit)(this.dir);
49
50
  return Git.versionCheck(git).then(() => {
50
- Git.gitInstance = git;
51
+ this.gitInstance = git;
51
52
  return git;
52
53
  });
53
54
  }
@@ -104,6 +105,5 @@ class Git {
104
105
  }
105
106
  exports.Git = Git;
106
107
  Git.MIN_GIT_VERSION_MAJOR = 2;
107
- Git.MIN_GIT_VERSION_MINOR = 37;
108
+ Git.MIN_GIT_VERSION_MINOR = 30;
108
109
  Git.MIN_GIT_VERSION_PATCH = 0;
109
- Git.gitInstance = undefined;
@@ -1,3 +1,3 @@
1
- export { getDefaultBranchDiff, getDefaultBranchDiffByRef, } from './FilesChanged/BranchChanges';
1
+ export { getDefaultBranchDiff, getDefaultBranchDiffByRef, getDiffRange, getLocalChanges, } from './FilesChanged/BranchChanges';
2
2
  export { IGit } from './api/IGit';
3
3
  export { Git } from './Git/Git';
package/lib/src/index.js CHANGED
@@ -3,9 +3,11 @@
3
3
  * Copyright (c) 2023, FinancialForce.com, inc. All rights reserved.
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Git = exports.getDefaultBranchDiffByRef = exports.getDefaultBranchDiff = void 0;
6
+ exports.Git = exports.getLocalChanges = exports.getDiffRange = exports.getDefaultBranchDiffByRef = exports.getDefaultBranchDiff = void 0;
7
7
  var BranchChanges_1 = require("./FilesChanged/BranchChanges");
8
8
  Object.defineProperty(exports, "getDefaultBranchDiff", { enumerable: true, get: function () { return BranchChanges_1.getDefaultBranchDiff; } });
9
9
  Object.defineProperty(exports, "getDefaultBranchDiffByRef", { enumerable: true, get: function () { return BranchChanges_1.getDefaultBranchDiffByRef; } });
10
+ Object.defineProperty(exports, "getDiffRange", { enumerable: true, get: function () { return BranchChanges_1.getDiffRange; } });
11
+ Object.defineProperty(exports, "getLocalChanges", { enumerable: true, get: function () { return BranchChanges_1.getLocalChanges; } });
10
12
  var Git_1 = require("./Git/Git");
11
13
  Object.defineProperty(exports, "Git", { enumerable: true, get: function () { return Git_1.Git; } });
@@ -66,7 +66,7 @@ describe('Branch changes', () => {
66
66
  //Given
67
67
  mockGitImpl.getDefaultBranchName.mockRejectedValue(Error('no head ref error'));
68
68
  //When/Then
69
- yield expect((0, BranchChanges_1.getDefaultBranchDiffByRef)('./some/path/to/dir', 'abc12fcd')).rejects.toEqual(Error('Failed getting diff: no head ref error'));
69
+ yield expect((0, BranchChanges_1.getDefaultBranchDiffByRef)('./some/path/to/dir', 'abc12fcd')).rejects.toEqual(Error("Local branch operation failed. Cause: 'no head ref error'"));
70
70
  }));
71
71
  it('rejects and throws error when "getLocalChangedAndCreated" fails', () => __awaiter(void 0, void 0, void 0, function* () {
72
72
  //Given
@@ -74,7 +74,7 @@ describe('Branch changes', () => {
74
74
  mockGitImpl.diffRange.mockResolvedValue(new Set(['File.txt', 'AnotherClass.txt']));
75
75
  mockGitImpl.getLocalChangedAndCreated.mockRejectedValue(Error('op failed'));
76
76
  //When/Then
77
- yield expect((0, BranchChanges_1.getDefaultBranchDiffByRef)('./some/path/to/dir', 'abc12fcd')).rejects.toEqual(Error('Failed getting diff: op failed'));
77
+ yield expect((0, BranchChanges_1.getDefaultBranchDiffByRef)('./some/path/to/dir', 'abc12fcd')).rejects.toEqual(Error("Local branch operation failed. Cause: 'Getting diff operation failed. Cause: 'op failed''"));
78
78
  }));
79
79
  });
80
80
  describe('getDefaultBranchDiff', () => {
@@ -102,7 +102,51 @@ describe('Branch changes', () => {
102
102
  mockGitImpl.diffRange.mockResolvedValue(new Set(['File.txt', 'AnotherClass.txt']));
103
103
  mockGitImpl.getLocalChangedAndCreated.mockRejectedValue(Error('op failed'));
104
104
  //When/Then
105
- yield expect((0, BranchChanges_1.getDefaultBranchDiff)('./some/path/to/dir')).rejects.toEqual(Error('Failed getting diff: op failed'));
105
+ yield expect((0, BranchChanges_1.getDefaultBranchDiff)('./some/path/to/dir')).rejects.toEqual(Error("Local branch operation failed. Cause: 'Getting diff operation failed. Cause: 'op failed''"));
106
+ }));
107
+ });
108
+ describe('getDiffRange', () => {
109
+ it('returns the correct set of files', () => __awaiter(void 0, void 0, void 0, function* () {
110
+ //Given
111
+ mockGitImpl.diffRange.mockResolvedValue(new Set(['File.txt', 'AnotherClass.txt']));
112
+ mockGitImpl.getLocalChangedAndCreated.mockResolvedValue(new Set(['SomeFile.txt']));
113
+ //When
114
+ const res = yield (0, BranchChanges_1.getDiffRange)('./some/path/to/dir', 'ref1', 'ref2');
115
+ //Then
116
+ expect(mockedGit).toHaveBeenCalledWith('./some/path/to/dir');
117
+ expect(mockGitImpl.getLocalChangedAndCreated).toBeCalledTimes(1);
118
+ expect(mockGitImpl.diffRange).toHaveBeenCalledWith('ref1', 'ref2');
119
+ expect(res).toEqual(new Set([
120
+ `${mockRootDir}/abs/path/to/repo/File.txt`,
121
+ `${mockRootDir}/abs/path/to/repo/AnotherClass.txt`,
122
+ `${mockRootDir}/abs/path/to/repo/SomeFile.txt`,
123
+ ]));
124
+ }));
125
+ it('rejects and throws error when "getLocalChangedAndCreated" fails', () => __awaiter(void 0, void 0, void 0, function* () {
126
+ //Given
127
+ mockGitImpl.getDefaultBranchName.mockResolvedValue('default/branch/name');
128
+ mockGitImpl.diffRange.mockResolvedValue(new Set(['File.txt', 'AnotherClass.txt']));
129
+ mockGitImpl.getLocalChangedAndCreated.mockRejectedValue(Error('op failed'));
130
+ //When/Then
131
+ yield expect((0, BranchChanges_1.getDiffRange)('./some/path/to/dir', 'ref1', 'ref2')).rejects.toEqual(Error("Getting diff operation failed. Cause: 'op failed'"));
132
+ }));
133
+ });
134
+ describe('getLocalChanges', () => {
135
+ it('returns the correct set of files', () => __awaiter(void 0, void 0, void 0, function* () {
136
+ //Given
137
+ mockGitImpl.getLocalChangedAndCreated.mockResolvedValue(new Set(['SomeFile.txt']));
138
+ //When
139
+ const res = yield (0, BranchChanges_1.getLocalChanges)('./some/path/to/dir');
140
+ //Then
141
+ expect(mockedGit).toHaveBeenCalledWith('./some/path/to/dir');
142
+ expect(mockGitImpl.getLocalChangedAndCreated).toBeCalledTimes(1);
143
+ expect(res).toEqual(new Set([`${mockRootDir}/abs/path/to/repo/SomeFile.txt`]));
144
+ }));
145
+ it('rejects and throws error when op fails', () => __awaiter(void 0, void 0, void 0, function* () {
146
+ //Given
147
+ mockGitImpl.getLocalChangedAndCreated.mockRejectedValue(Error('op failed'));
148
+ //When/Then
149
+ yield expect((0, BranchChanges_1.getLocalChanges)('./some/path/to/dir')).rejects.toEqual(Error("Getting local changes operation failed. Cause: 'op failed'"));
106
150
  }));
107
151
  });
108
152
  });
@@ -28,10 +28,10 @@ describe('Git', () => {
28
28
  yield repoManager.tearDown();
29
29
  }));
30
30
  describe('version check', () => {
31
- it('does not fail when Git version is equal to 2.37.0', () => {
31
+ it('does not fail when Git version is equal to 2.30.0', () => {
32
32
  //Given/When
33
33
  const mock = {
34
- version: jest.fn().mockReturnValue({ major: 2, minor: 37, patch: 0 }),
34
+ version: jest.fn().mockReturnValue({ major: 2, minor: 30, patch: 0 }),
35
35
  };
36
36
  expect(Git_1.Git.versionCheck(mock)).resolves;
37
37
  });
@@ -41,7 +41,7 @@ describe('Git', () => {
41
41
  version: jest.fn().mockReturnValue({ major: 1 }),
42
42
  };
43
43
  //Then
44
- yield expect(Git_1.Git.versionCheck(mock)).rejects.toThrow(Error('Unsupported version of git. Min version must be 2.37.0'));
44
+ yield expect(Git_1.Git.versionCheck(mock)).rejects.toThrow(Error('Unsupported version of git. Min version must be 2.30.0'));
45
45
  }));
46
46
  it('fails when Git minor version is lower', () => __awaiter(void 0, void 0, void 0, function* () {
47
47
  //Given/When
@@ -49,7 +49,7 @@ describe('Git', () => {
49
49
  version: jest.fn().mockReturnValue({ major: 3, minor: 36 }),
50
50
  };
51
51
  //Then
52
- yield expect(Git_1.Git.versionCheck(mock)).rejects.toThrow(Error('Unsupported version of git. Min version must be 2.37.0'));
52
+ yield expect(Git_1.Git.versionCheck(mock)).rejects.toThrow(Error('Unsupported version of git. Min version must be 2.30.0'));
53
53
  }));
54
54
  it('fails when Git patch version is more than 0', () => {
55
55
  //Given/When
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apexdevtools/git-ops",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Library to do git operations to find changed files in a given git repository",
5
5
  "author": {
6
6
  "name": "Apex Dev Tools Team",