git-coco 0.8.0 → 0.8.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/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![NPM Version](https://img.shields.io/npm/v/git-coco.svg)](https://www.npmjs.com/package/git-coco)
7
7
  [![NPM Downloads](https://img.shields.io/npm/dt/git-coco.svg)](https://www.npmjs.com/package/git-coco)
8
8
 
9
- `coco`, the Commit Copilot, is not just your scribe for git commit messages. With the power of [LangChain🦜🔗](https://js.langchain.com/) and LLMs, it now brings you a suite of tools to streamline your git workflow!
9
+ `coco`, the Commit Copilot, transcends being merely a robotic scribe for crafting git commit messages. Leveraging the capabilities of [LangChain🦜🔗](https://js.langchain.com/) and LLMs, `coco` is committed to providing a suite of insightful tools aimed at enhancing and streamlining your git workflow!
10
10
 
11
11
  ## Commands
12
12
 
package/dist/index.d.ts CHANGED
@@ -292,6 +292,12 @@ declare class Logger {
292
292
  }
293
293
 
294
294
  type TokenCounter = Awaited<ReturnType<typeof getTokenCounter>>;
295
+ /**
296
+ * Retrieves the token counter for a given model name.
297
+ *
298
+ * @param {TikTokenModel} modelName - The name of the Tiktoken model.
299
+ * @returns A promise that resolves to a function that calculates the number of tokens in a given text.
300
+ */
295
301
  declare const getTokenCounter: (modelName: TiktokenModel) => Promise<(text: string) => number>;
296
302
 
297
303
  type FileChangeStatus = 'modified' | 'renamed' | 'added' | 'deleted' | 'untracked' | 'unknown';
@@ -682,6 +682,7 @@ function getSummarizationChain(model, options = { type: 'map_reduce' }) {
682
682
 
683
683
  /**
684
684
  * Get Recursive Character Text Splitter
685
+ *
685
686
  * @param options
686
687
  * @returns
687
688
  */
@@ -689,12 +690,29 @@ function getTextSplitter(options = {}) {
689
690
  return new RecursiveCharacterTextSplitter(options);
690
691
  }
691
692
 
693
+ /**
694
+ * Parses the default file diff for a given nodeFile.
695
+ *
696
+ * @param nodeFile - The file change object.
697
+ * @param commit - The commit to diff against. Defaults to '--staged'.
698
+ * @param git - The SimpleGit instance.
699
+ * @returns A Promise that resolves to the file diff as a string.
700
+ */
692
701
  async function parseDefaultFileDiff(nodeFile, commit = '--staged', git) {
693
702
  if (commit !== '--staged') {
694
703
  return await git.diff([`${commit}~1..${commit}`, '--', nodeFile.filePath]);
695
704
  }
696
705
  return await git.diff([commit, nodeFile.filePath]);
697
706
  }
707
+ /**
708
+ * Parses the diff for a renamed file.
709
+ *
710
+ * @param nodeFile - The file change object.
711
+ * @param commit - The commit hash or '--staged'.
712
+ * @param git - The SimpleGit instance.
713
+ * @param logger - The logger instance.
714
+ * @returns A Promise that resolves to the diff string.
715
+ */
698
716
  async function parseRenamedFileDiff(nodeFile, commit, git, logger) {
699
717
  let result = '';
700
718
  const oldFilePath = nodeFile?.oldFilePath || nodeFile.filePath;
@@ -733,6 +751,18 @@ async function parseRenamedFileDiff(nodeFile, commit, git, logger) {
733
751
  }
734
752
  return result;
735
753
  }
754
+ /**
755
+ * Retrieves the diff for a given file change in a specific commit.
756
+ * If the file is deleted, it returns a message indicating that the file has been deleted.
757
+ * If the file is renamed, it parses the renamed file diff and returns it.
758
+ * Otherwise, it retrieves the default diff from the index and returns it.
759
+ *
760
+ * @param nodeFile - The file change object.
761
+ * @param commit - The commit hash.
762
+ * @param git - The SimpleGit instance.
763
+ * @param logger - The logger instance.
764
+ * @returns A promise that resolves to the diff as a string.
765
+ */
736
766
  async function getDiff(nodeFile, commit, { git, logger, }) {
737
767
  if (nodeFile.status === 'deleted') {
738
768
  return 'This file has been deleted.';
@@ -930,6 +960,14 @@ function getPrompt({ template, variables, fallback }) {
930
960
  : fallback);
931
961
  }
932
962
 
963
+ /**
964
+ * Determines the status of a file based on its changes in the Git repository.
965
+ *
966
+ * @param file - The file to check the status of.
967
+ * @param location - The location to check the status in ('index' or 'working_dir'). Defaults to 'index'.
968
+ * @returns The status of the file ('added', 'deleted', 'modified', 'renamed', 'untracked', or 'unknown').
969
+ * @throws Error if the file type is invalid.
970
+ */
933
971
  function getStatus(file, location = 'index') {
934
972
  if ('index' in file && 'working_dir' in file) {
935
973
  const statusCode = file[location];
@@ -966,6 +1004,14 @@ function getStatus(file, location = 'index') {
966
1004
  }
967
1005
  }
968
1006
 
1007
+ /**
1008
+ * Returns the summary text for a file change.
1009
+ *
1010
+ * @param file - The file status or diff result.
1011
+ * @param change - The partial file change object.
1012
+ * @returns The summary text for the file change.
1013
+ * @throws Error if the file type is invalid.
1014
+ */
969
1015
  function getSummaryText(file, change) {
970
1016
  const status = change.status || getStatus(file);
971
1017
  let filePath;
@@ -984,6 +1030,12 @@ function getSummaryText(file, change) {
984
1030
  return `${status}: ${filePath}`;
985
1031
  }
986
1032
 
1033
+ /**
1034
+ * Retrieves the changes in the Git repository.
1035
+ *
1036
+ * @param {GetChangesInput} options - The options for retrieving the changes.
1037
+ * @returns {Promise<GetChangesResult>} A promise that resolves to the changes in the Git repository.
1038
+ */
987
1039
  async function getChanges({ git, options }) {
988
1040
  const { ignoredFiles = DEFAULT_IGNORED_FILES, ignoredExtensions = DEFAULT_IGNORED_EXTENSIONS } = options || {};
989
1041
  const staged = [];
@@ -1130,7 +1182,8 @@ async function getUserReviewDecision({ label, descriptions, enableRetry = true,
1130
1182
  }
1131
1183
 
1132
1184
  /**
1133
- * Verify template string contains all required input variables
1185
+ * Verify template string contains all required input variables
1186
+ *
1134
1187
  * @param text template string
1135
1188
  * @param inputVariables template variables
1136
1189
  * @returns boolean or error message
@@ -1276,6 +1329,10 @@ async function handleResult({ result, mode, interactiveHandler }) {
1276
1329
  process.exit(0);
1277
1330
  }
1278
1331
 
1332
+ /**
1333
+ * Retrieves the SimpleGit instance for the repository.
1334
+ * @returns {SimpleGit} The SimpleGit instance.
1335
+ */
1279
1336
  const getRepo = () => {
1280
1337
  let git;
1281
1338
  try {
@@ -1288,20 +1345,37 @@ const getRepo = () => {
1288
1345
  return git;
1289
1346
  };
1290
1347
 
1348
+ /**
1349
+ * Retrieves a TikToken for the specified model.
1350
+ *
1351
+ * @param {TiktokenModel} modelName - The name of the TiktokenModel.
1352
+ * @returns A Promise that resolves to the TikToken.
1353
+ */
1291
1354
  const getTikToken = async (modelName) => {
1292
1355
  return await encoding_for_model(modelName);
1293
1356
  };
1357
+ /**
1358
+ * Retrieves the token counter for a given model name.
1359
+ *
1360
+ * @param {TikTokenModel} modelName - The name of the Tiktoken model.
1361
+ * @returns A promise that resolves to a function that calculates the number of tokens in a given text.
1362
+ */
1294
1363
  const getTokenCounter = async (modelName) => {
1295
1364
  return getTikToken(modelName).then((tokenizer) => (text) => {
1296
- // console.log('Running GetTokenCount', { tokenizer, length: text.length })
1297
1365
  const tokens = tokenizer.encode(text);
1298
- // console.log('Tokens', { tokenCount: tokens.length })
1299
1366
  return tokens.length;
1300
1367
  });
1301
1368
  };
1302
1369
 
1303
- async function createCommit(commitMsg, git) {
1304
- return await git.commit(commitMsg);
1370
+ /**
1371
+ * Creates a commit with the specified commit message.
1372
+ *
1373
+ * @param message The commit message.
1374
+ * @param git The SimpleGit instance.
1375
+ * @returns A Promise that resolves to the CommitResult.
1376
+ */
1377
+ async function createCommit(message, git) {
1378
+ return await git.commit(message);
1305
1379
  }
1306
1380
 
1307
1381
  const handler$2 = async (argv, logger) => {
@@ -1445,6 +1519,15 @@ const CHANGELOG_PROMPT = new PromptTemplate({
1445
1519
  inputVariables,
1446
1520
  });
1447
1521
 
1522
+ /**
1523
+ * Retrieves the commit log range between two specified commits.
1524
+ *
1525
+ * @param from - The starting commit.
1526
+ * @param to - The ending commit.
1527
+ * @param options - Additional options for retrieving the commit log range.
1528
+ * @returns A promise that resolves to an array of commit log messages.
1529
+ * @throws If there is an error retrieving the commit log range.
1530
+ */
1448
1531
  async function getCommitLogRange(from, to, { noMerges, git }) {
1449
1532
  try {
1450
1533
  const logOptions = { from: `${from}^1`, to, '--no-merges': noMerges };
@@ -1458,10 +1541,26 @@ async function getCommitLogRange(from, to, { noMerges, git }) {
1458
1541
  }
1459
1542
  }
1460
1543
 
1544
+ /**
1545
+ * Retrieves the name of the current branch.
1546
+ *
1547
+ * @param {GetCurrentBranchName} options - The options for retrieving the branch name.
1548
+ * @returns {Promise<string>} - A promise that resolves to the name of the current branch.
1549
+ */
1461
1550
  async function getCurrentBranchName({ git }) {
1462
1551
  return await git.revparse(['--abbrev-ref', 'HEAD']);
1463
1552
  }
1464
1553
 
1554
+ /**
1555
+ * Retrieves the commit log for the current branch.
1556
+ *
1557
+ * @param {Object} options - The options for retrieving the commit log.
1558
+ * @param {SimpleGit} options.git - The SimpleGit instance.
1559
+ * @param {Logger} options.logger - The logger for logging messages.
1560
+ * @param {string} [options.comparisonBranch='main'] - The branch to compare against.
1561
+ * @param {string} [options.comparisonRemote='origin'] - The remote to compare against.
1562
+ * @returns {Promise<string[]>} The array of commit messages in the commit log.
1563
+ */
1465
1564
  async function getCommitLogCurrentBranch({ git, logger, comparisonBranch = 'main', comparisonRemote = 'origin', }) {
1466
1565
  try {
1467
1566
  // Get the current branch name
@@ -1702,14 +1801,10 @@ async function installNpmPackage({ name, flags = [], cwd = process.cwd(), }) {
1702
1801
  */
1703
1802
  function isPackageInstalled(packageName, projectPath) {
1704
1803
  try {
1705
- // Construct the path to the package.json file
1706
1804
  const packageJsonPath = path__default.join(projectPath, 'package.json');
1707
- // Read the package.json file
1708
1805
  const packageJson = JSON.parse(fs__default.readFileSync(packageJsonPath, 'utf8'));
1709
- // Check both dependencies and devDependencies
1710
1806
  const dependencies = packageJson.dependencies || {};
1711
1807
  const devDependencies = packageJson.devDependencies || {};
1712
- // Return true if the package is found in either
1713
1808
  return dependencies.hasOwnProperty(packageName) || devDependencies.hasOwnProperty(packageName);
1714
1809
  }
1715
1810
  catch (error) {
package/dist/index.js CHANGED
@@ -703,6 +703,7 @@ function getSummarizationChain(model, options = { type: 'map_reduce' }) {
703
703
 
704
704
  /**
705
705
  * Get Recursive Character Text Splitter
706
+ *
706
707
  * @param options
707
708
  * @returns
708
709
  */
@@ -710,12 +711,29 @@ function getTextSplitter(options = {}) {
710
711
  return new text_splitter.RecursiveCharacterTextSplitter(options);
711
712
  }
712
713
 
714
+ /**
715
+ * Parses the default file diff for a given nodeFile.
716
+ *
717
+ * @param nodeFile - The file change object.
718
+ * @param commit - The commit to diff against. Defaults to '--staged'.
719
+ * @param git - The SimpleGit instance.
720
+ * @returns A Promise that resolves to the file diff as a string.
721
+ */
713
722
  async function parseDefaultFileDiff(nodeFile, commit = '--staged', git) {
714
723
  if (commit !== '--staged') {
715
724
  return await git.diff([`${commit}~1..${commit}`, '--', nodeFile.filePath]);
716
725
  }
717
726
  return await git.diff([commit, nodeFile.filePath]);
718
727
  }
728
+ /**
729
+ * Parses the diff for a renamed file.
730
+ *
731
+ * @param nodeFile - The file change object.
732
+ * @param commit - The commit hash or '--staged'.
733
+ * @param git - The SimpleGit instance.
734
+ * @param logger - The logger instance.
735
+ * @returns A Promise that resolves to the diff string.
736
+ */
719
737
  async function parseRenamedFileDiff(nodeFile, commit, git, logger) {
720
738
  let result = '';
721
739
  const oldFilePath = nodeFile?.oldFilePath || nodeFile.filePath;
@@ -754,6 +772,18 @@ async function parseRenamedFileDiff(nodeFile, commit, git, logger) {
754
772
  }
755
773
  return result;
756
774
  }
775
+ /**
776
+ * Retrieves the diff for a given file change in a specific commit.
777
+ * If the file is deleted, it returns a message indicating that the file has been deleted.
778
+ * If the file is renamed, it parses the renamed file diff and returns it.
779
+ * Otherwise, it retrieves the default diff from the index and returns it.
780
+ *
781
+ * @param nodeFile - The file change object.
782
+ * @param commit - The commit hash.
783
+ * @param git - The SimpleGit instance.
784
+ * @param logger - The logger instance.
785
+ * @returns A promise that resolves to the diff as a string.
786
+ */
757
787
  async function getDiff(nodeFile, commit, { git, logger, }) {
758
788
  if (nodeFile.status === 'deleted') {
759
789
  return 'This file has been deleted.';
@@ -951,6 +981,14 @@ function getPrompt({ template, variables, fallback }) {
951
981
  : fallback);
952
982
  }
953
983
 
984
+ /**
985
+ * Determines the status of a file based on its changes in the Git repository.
986
+ *
987
+ * @param file - The file to check the status of.
988
+ * @param location - The location to check the status in ('index' or 'working_dir'). Defaults to 'index'.
989
+ * @returns The status of the file ('added', 'deleted', 'modified', 'renamed', 'untracked', or 'unknown').
990
+ * @throws Error if the file type is invalid.
991
+ */
954
992
  function getStatus(file, location = 'index') {
955
993
  if ('index' in file && 'working_dir' in file) {
956
994
  const statusCode = file[location];
@@ -987,6 +1025,14 @@ function getStatus(file, location = 'index') {
987
1025
  }
988
1026
  }
989
1027
 
1028
+ /**
1029
+ * Returns the summary text for a file change.
1030
+ *
1031
+ * @param file - The file status or diff result.
1032
+ * @param change - The partial file change object.
1033
+ * @returns The summary text for the file change.
1034
+ * @throws Error if the file type is invalid.
1035
+ */
990
1036
  function getSummaryText(file, change) {
991
1037
  const status = change.status || getStatus(file);
992
1038
  let filePath;
@@ -1005,6 +1051,12 @@ function getSummaryText(file, change) {
1005
1051
  return `${status}: ${filePath}`;
1006
1052
  }
1007
1053
 
1054
+ /**
1055
+ * Retrieves the changes in the Git repository.
1056
+ *
1057
+ * @param {GetChangesInput} options - The options for retrieving the changes.
1058
+ * @returns {Promise<GetChangesResult>} A promise that resolves to the changes in the Git repository.
1059
+ */
1008
1060
  async function getChanges({ git, options }) {
1009
1061
  const { ignoredFiles = DEFAULT_IGNORED_FILES, ignoredExtensions = DEFAULT_IGNORED_EXTENSIONS } = options || {};
1010
1062
  const staged = [];
@@ -1151,7 +1203,8 @@ async function getUserReviewDecision({ label, descriptions, enableRetry = true,
1151
1203
  }
1152
1204
 
1153
1205
  /**
1154
- * Verify template string contains all required input variables
1206
+ * Verify template string contains all required input variables
1207
+ *
1155
1208
  * @param text template string
1156
1209
  * @param inputVariables template variables
1157
1210
  * @returns boolean or error message
@@ -1297,6 +1350,10 @@ async function handleResult({ result, mode, interactiveHandler }) {
1297
1350
  process.exit(0);
1298
1351
  }
1299
1352
 
1353
+ /**
1354
+ * Retrieves the SimpleGit instance for the repository.
1355
+ * @returns {SimpleGit} The SimpleGit instance.
1356
+ */
1300
1357
  const getRepo = () => {
1301
1358
  let git;
1302
1359
  try {
@@ -1309,20 +1366,37 @@ const getRepo = () => {
1309
1366
  return git;
1310
1367
  };
1311
1368
 
1369
+ /**
1370
+ * Retrieves a TikToken for the specified model.
1371
+ *
1372
+ * @param {TiktokenModel} modelName - The name of the TiktokenModel.
1373
+ * @returns A Promise that resolves to the TikToken.
1374
+ */
1312
1375
  const getTikToken = async (modelName) => {
1313
1376
  return await tiktoken.encoding_for_model(modelName);
1314
1377
  };
1378
+ /**
1379
+ * Retrieves the token counter for a given model name.
1380
+ *
1381
+ * @param {TikTokenModel} modelName - The name of the Tiktoken model.
1382
+ * @returns A promise that resolves to a function that calculates the number of tokens in a given text.
1383
+ */
1315
1384
  const getTokenCounter = async (modelName) => {
1316
1385
  return getTikToken(modelName).then((tokenizer) => (text) => {
1317
- // console.log('Running GetTokenCount', { tokenizer, length: text.length })
1318
1386
  const tokens = tokenizer.encode(text);
1319
- // console.log('Tokens', { tokenCount: tokens.length })
1320
1387
  return tokens.length;
1321
1388
  });
1322
1389
  };
1323
1390
 
1324
- async function createCommit(commitMsg, git) {
1325
- return await git.commit(commitMsg);
1391
+ /**
1392
+ * Creates a commit with the specified commit message.
1393
+ *
1394
+ * @param message The commit message.
1395
+ * @param git The SimpleGit instance.
1396
+ * @returns A Promise that resolves to the CommitResult.
1397
+ */
1398
+ async function createCommit(message, git) {
1399
+ return await git.commit(message);
1326
1400
  }
1327
1401
 
1328
1402
  const handler$2 = async (argv, logger) => {
@@ -1466,6 +1540,15 @@ const CHANGELOG_PROMPT = new prompts.PromptTemplate({
1466
1540
  inputVariables,
1467
1541
  });
1468
1542
 
1543
+ /**
1544
+ * Retrieves the commit log range between two specified commits.
1545
+ *
1546
+ * @param from - The starting commit.
1547
+ * @param to - The ending commit.
1548
+ * @param options - Additional options for retrieving the commit log range.
1549
+ * @returns A promise that resolves to an array of commit log messages.
1550
+ * @throws If there is an error retrieving the commit log range.
1551
+ */
1469
1552
  async function getCommitLogRange(from, to, { noMerges, git }) {
1470
1553
  try {
1471
1554
  const logOptions = { from: `${from}^1`, to, '--no-merges': noMerges };
@@ -1479,10 +1562,26 @@ async function getCommitLogRange(from, to, { noMerges, git }) {
1479
1562
  }
1480
1563
  }
1481
1564
 
1565
+ /**
1566
+ * Retrieves the name of the current branch.
1567
+ *
1568
+ * @param {GetCurrentBranchName} options - The options for retrieving the branch name.
1569
+ * @returns {Promise<string>} - A promise that resolves to the name of the current branch.
1570
+ */
1482
1571
  async function getCurrentBranchName({ git }) {
1483
1572
  return await git.revparse(['--abbrev-ref', 'HEAD']);
1484
1573
  }
1485
1574
 
1575
+ /**
1576
+ * Retrieves the commit log for the current branch.
1577
+ *
1578
+ * @param {Object} options - The options for retrieving the commit log.
1579
+ * @param {SimpleGit} options.git - The SimpleGit instance.
1580
+ * @param {Logger} options.logger - The logger for logging messages.
1581
+ * @param {string} [options.comparisonBranch='main'] - The branch to compare against.
1582
+ * @param {string} [options.comparisonRemote='origin'] - The remote to compare against.
1583
+ * @returns {Promise<string[]>} The array of commit messages in the commit log.
1584
+ */
1486
1585
  async function getCommitLogCurrentBranch({ git, logger, comparisonBranch = 'main', comparisonRemote = 'origin', }) {
1487
1586
  try {
1488
1587
  // Get the current branch name
@@ -1723,14 +1822,10 @@ async function installNpmPackage({ name, flags = [], cwd = process.cwd(), }) {
1723
1822
  */
1724
1823
  function isPackageInstalled(packageName, projectPath) {
1725
1824
  try {
1726
- // Construct the path to the package.json file
1727
1825
  const packageJsonPath = path.join(projectPath, 'package.json');
1728
- // Read the package.json file
1729
1826
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
1730
- // Check both dependencies and devDependencies
1731
1827
  const dependencies = packageJson.dependencies || {};
1732
1828
  const devDependencies = packageJson.devDependencies || {};
1733
- // Return true if the package is found in either
1734
1829
  return dependencies.hasOwnProperty(packageName) || devDependencies.hasOwnProperty(packageName);
1735
1830
  }
1736
1831
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "git-coco",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "zero-effort git commits with coco.",
5
5
  "author": "gfargo <ghfargo@gmail.com>",
6
6
  "license": "MIT",
@@ -81,7 +81,7 @@
81
81
  "dependencies": {
82
82
  "@inquirer/prompts": "3.3.0",
83
83
  "chalk": "4.1.2",
84
- "diff": "5.1.0",
84
+ "diff": "5.2.0",
85
85
  "ini": "4.1.1",
86
86
  "langchain": "0.0.196",
87
87
  "minimatch": "9.0.3",
@@ -89,7 +89,7 @@
89
89
  "p-queue": "5.0.0",
90
90
  "performance-now": "2.1.0",
91
91
  "pretty-ms": "7.0.1",
92
- "simple-git": "3.21.0",
92
+ "simple-git": "3.23.0",
93
93
  "tiktoken": "1.0.11",
94
94
  "yargs": "17.7.2"
95
95
  }