promise.d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. import * as resp from "./typings/response";
  2. declare function simplegit(basePath?: string): simplegit.SimpleGit;
  3. declare namespace simplegit {
  4. interface SimpleGit {
  5. /**
  6. * Adds one or more files to source control
  7. *
  8. * @param {string|string[]} files
  9. * @returns {Promise<void>}
  10. */
  11. add(files: string | string[]): Promise<void>;
  12. /**
  13. * Add an annotated tag to the head of the current branch
  14. *
  15. * @param {string} tagName
  16. * @param {string} tagMessage
  17. * @returns {Promise<void>}
  18. */
  19. addAnnotatedTag(tagName: string, tagMessage: string): Promise<void>;
  20. /**
  21. * Add config to local git instance
  22. *
  23. * @param {string} key configuration key (e.g user.name)
  24. * @param {string} value for the given key (e.g your name)
  25. * @returns {Promise<string>}
  26. */
  27. addConfig(key: string, value: string): Promise<string>;
  28. /**
  29. * Adds a remote to the list of remotes.
  30. *
  31. * @param {string} remoteName Name of the repository - eg "upstream"
  32. * @param {string} remoteRepo Fully qualified SSH or HTTP(S) path to the remote repo
  33. * @returns {Promise<void>}
  34. */
  35. addRemote(remoteName: string, remoteRepo: string): Promise<void>;
  36. /**
  37. * Add a lightweight tag to the head of the current branch
  38. *
  39. * @param {string} name
  40. * @returns {Promise<string>}
  41. */
  42. addTag(name: string): Promise<string>;
  43. /**
  44. * Equivalent to `catFile` but will return the native `Buffer` of content from the git command's stdout.
  45. *
  46. * @param {string[]} options
  47. */
  48. binaryCatFile(options: string[]): Promise<any>;
  49. /**
  50. * List all branches
  51. */
  52. branch(): Promise<BranchSummary>;
  53. branch(options: Options | string[]): Promise<BranchSummary>;
  54. /**
  55. * List of local branches
  56. *
  57. * @returns {Promise<BranchSummary>}
  58. */
  59. branchLocal(): Promise<BranchSummary>;
  60. /**
  61. * Returns a list of objects in a tree based on commit hash.
  62. * Passing in an object hash returns the object's content, size, and type.
  63. *
  64. * Passing "-p" will instruct cat-file to determine the object type, and display its formatted contents.
  65. *
  66. * @param {string[]} [options]
  67. * @returns {Promise<string>}
  68. *
  69. * @see https://git-scm.com/docs/git-cat-file
  70. */
  71. catFile(options: string[]): Promise<string>;
  72. /**
  73. * Check if a pathname or pathnames are excluded by .gitignore
  74. *
  75. * @param {string|string[]} pathnames
  76. */
  77. checkIgnore(pathnames: string[]): Promise<string[]>;
  78. checkIgnore(path: string): Promise<string[]>;
  79. /**
  80. * Validates that the current repo is a Git repo.
  81. *
  82. * @returns {Promise<boolean>}
  83. */
  84. checkIsRepo(): Promise<boolean>;
  85. /**
  86. * Checkout a tag or revision, any number of additional arguments can be passed to the `git* checkout` command
  87. by supplying either a string or array of strings as the `what` parameter.
  88. *
  89. * @param {(string | string[])} what one or more commands to pass to `git checkout`.
  90. * @returns {Promise<void>}
  91. */
  92. checkout(what: string | string[]): Promise<void>;
  93. /**
  94. * Checkout a remote branch.
  95. *
  96. * @param {string} branchName name of branch.
  97. * @param {string} startPoint (e.g origin/development).
  98. * @returns {Promise<void>}
  99. */
  100. checkoutBranch(branchName: string, startPoint: string): Promise<void>;
  101. /**
  102. * Internally uses pull and tags to get the list of tags then checks out the latest tag.
  103. */
  104. checkoutLatestTag(branchName: string, startPoint: string): Promise<void>;
  105. /**
  106. * Checkout a local branch
  107. *
  108. * @param {string} branchName name of branch.
  109. * @returns {Promise<void>}
  110. */
  111. checkoutLocalBranch(branchName: string): Promise<void>;
  112. /**
  113. * @param {string} mode Required parameter "n" or "f"
  114. * @param {string[]} options
  115. */
  116. clean(
  117. mode: 'd' | 'f' | 'i' | 'n' | 'q' | 'x' | 'X',
  118. options?: string[]
  119. ): Promise<string>;
  120. /**
  121. * Clears the queue of pending commands and returns the wrapper instance for chaining.
  122. */
  123. clearQueue(): this;
  124. /**
  125. * Clone a repository into a new directory.
  126. *
  127. * @param {string} repoPath repository url to clone e.g. https://github.com/steveukx/git-js.git
  128. * @param {string} localPath local folder path to clone to.
  129. * @param {string[]} [options] options supported by [git](https://git-scm.com/docs/git-clone).
  130. * @returns {Promise<void>}
  131. */
  132. clone(repoPath: string, localPath: string, options?: Options | string[]): Promise<string>;
  133. clone(repoPath: string, options?: Options | string[]): Promise<string>;
  134. /**
  135. * Commits changes in the current working directory - when specific file paths are supplied, only changes on those
  136. * files will be committed.
  137. *
  138. * @param {string|string[]} message
  139. * @param {string|string[]} [files]
  140. * @param {Object} [options]
  141. */
  142. commit(
  143. message: string | string[],
  144. files?: string | string[],
  145. options?: Options
  146. ): Promise<resp.CommitSummary>;
  147. /**
  148. * Sets the path to a custom git binary, should either be `git` when there is an installation of git available on
  149. * the system path, or a fully qualified path to the executable.
  150. *
  151. * @param {string} command
  152. */
  153. customBinary(command: string): this;
  154. /**
  155. * Sets the working directory of the subsequent commands.
  156. *
  157. * @param {string} workingDirectory
  158. */
  159. cwd<path extends string>(workingDirectory: path): Promise<path>;
  160. /**
  161. * Delete a local branch
  162. *
  163. * @param {string} branchName name of branch
  164. */
  165. deleteLocalBranch(branchName: string):
  166. Promise<resp.BranchDeletionSummary>;
  167. /**
  168. * Get the diff of the current repo compared to the last commit with a set of options supplied as a string.
  169. *
  170. * @param {string[]} [options] options supported by [git](https://git-scm.com/docs/git-diff).
  171. * @returns {Promise<string>} raw string result.
  172. */
  173. diff(options?: string[]): Promise<string>;
  174. /**
  175. * Gets a summary of the diff for files in the repo, uses the `git diff --stat` format to calculate changes.
  176. *
  177. * in order to get staged (only): `--cached` or `--staged`.
  178. *
  179. * @param {string[]} [options] options supported by [git](https://git-scm.com/docs/git-diff).
  180. * @returns {Promise<DiffResult>} Parsed diff summary result.
  181. */
  182. diffSummary(options?: string[]): Promise<DiffResult>;
  183. /**
  184. * Sets an environment variable for the spawned child process, either supply both a name and value as strings or
  185. * a single object to entirely replace the current environment variables.
  186. *
  187. * @param {string|Object} name
  188. * @param {string} [value]
  189. */
  190. env(name: string, value: string): this;
  191. env(env: object): this;
  192. /**
  193. * Updates the local working copy database with changes from the default remote repo and branch.
  194. *
  195. * @param {string | string[]} [remote] remote to fetch from.
  196. * @param {string} [branch] branch to fetch from.
  197. * @param {string[]} [options] options supported by [git](https://git-scm.com/docs/git-fetch).
  198. * @returns {Promise<FetchResult>} Parsed fetch result.
  199. */
  200. fetch(remote?: string | string[], branch?: string, options?: Options): Promise<FetchResult>;
  201. /**
  202. * Gets the currently available remotes, setting the optional verbose argument to true includes additional
  203. * detail on the remotes themselves.
  204. *
  205. * @param {boolean} [verbose=false]
  206. */
  207. getRemotes(verbose: false | undefined): Promise<resp.RemoteWithoutRefs[]>;
  208. getRemotes(verbose: true): Promise<resp.RemoteWithRefs[]>;
  209. /**
  210. * Initialize a git repo
  211. *
  212. * @param {Boolean} [bare=false]
  213. */
  214. init(bare?: boolean): Promise<void>;
  215. /**
  216. * List remote
  217. *
  218. * @param {string[]} [args]
  219. */
  220. listRemote(args: string[]): Promise<string>;
  221. /**
  222. * Show commit logs from `HEAD` to the first commit.
  223. * If provided between `options.from` and `options.to` tags or branch.
  224. *
  225. * You can provide `options.file`, which is the path to a file in your repository. Then only this file will be considered.
  226. *
  227. * To use a custom splitter in the log format, set `options.splitter` to be the string the log should be split on.
  228. *
  229. * By default the following fields will be part of the result:
  230. * `hash`: full commit hash
  231. * `date`: author date, ISO 8601-like format
  232. * `message`: subject + ref names, like the --decorate option of git-log
  233. * `author_name`: author name
  234. * `author_email`: author mail
  235. * You can specify `options.format` to be an mapping from key to a format option like `%H` (for commit hash).
  236. * The fields specified in `options.format` will be the fields in the result.
  237. *
  238. * Options can also be supplied as a standard options object for adding custom properties supported by the git log command.
  239. * For any other set of options, supply options as an array of strings to be appended to the git log command.
  240. *
  241. * @param {LogOptions} [options]
  242. *
  243. * @returns Promise<ListLogSummary>
  244. *
  245. * @see https://git-scm.com/docs/git-log
  246. */
  247. log<T = resp.DefaultLogFields>(options?: LogOptions<T>): Promise<resp.ListLogSummary<T>>;
  248. /**
  249. * Runs a merge, `options` can be either an array of arguments
  250. * supported by the [`git merge`](https://git-scm.com/docs/git-merge)
  251. * or an options object.
  252. *
  253. * Conflicts during the merge result in an error response,
  254. * the response type whether it was an error or success will be a MergeSummary instance.
  255. * When successful, the MergeSummary has all detail from a the PullSummary
  256. *
  257. * @param {Options | string[]} [options] options supported by [git](https://git-scm.com/docs/git-merge).
  258. * @returns {Promise<any>}
  259. *
  260. * @see https://github.com/steveukx/git-js/blob/master/src/responses/MergeSummary.js
  261. * @see https://github.com/steveukx/git-js/blob/master/src/responses/PullSummary.js
  262. */
  263. merge(options: Options | string[]): Promise<MergeSummary>;
  264. /**
  265. * Merges from one branch to another, equivalent to running `git merge ${from} $[to}`, the `options` argument can
  266. * either be an array of additional parameters to pass to the command or null / omitted to be ignored.
  267. *
  268. * @param {string} from branch to merge from.
  269. * @param {string} to branch to merge to.
  270. * @param {string[]} [options] options supported by [git](https://git-scm.com/docs/git-merge).
  271. * @returns {Promise<string>}
  272. */
  273. mergeFromTo(from: string, to: string, options?: string[]): Promise<string>;
  274. /**
  275. * Mirror a git repo
  276. *
  277. * @param {string} repoPath
  278. * @param {string} localPath
  279. */
  280. mirror(repoPath: string, localPath: string): Promise<string>;
  281. /**
  282. * Moves one or more files to a new destination.
  283. *
  284. * @see https://git-scm.com/docs/git-mv
  285. *
  286. * @param {string|string[]} from
  287. * @param {string} to
  288. */
  289. mv(from: string | string[], to: string): Promise<resp.MoveSummary>;
  290. /**
  291. * Sets a handler function to be called whenever a new child process is created, the handler function will be called
  292. * with the name of the command being run and the stdout & stderr streams used by the ChildProcess.
  293. *
  294. * @example
  295. * require('simple-git')
  296. * .outputHandler(function (command, stdout, stderr) {
  297. * stdout.pipe(process.stdout);
  298. * })
  299. * .checkout('https://github.com/user/repo.git');
  300. *
  301. * @see https://nodejs.org/api/child_process.html#child_process_class_childprocess
  302. * @see https://nodejs.org/api/stream.html#stream_class_stream_readable
  303. * @param {Function} outputHandler
  304. */
  305. outputHandler(handler: outputHandler | void): this;
  306. /**
  307. * Fetch from and integrate with another repository or a local branch.
  308. *
  309. * @param {string} [remote] remote to pull from.
  310. * @param {string} [branch] branch to pull from.
  311. * @param {Options} [options] options supported by [git](https://git-scm.com/docs/git-pull).
  312. * @returns {Promise<PullResult>} Parsed pull result.
  313. */
  314. pull(remote?: string, branch?: string, options?: Options): Promise<PullResult>;
  315. /**
  316. * Update remote refs along with associated objects.
  317. *
  318. * @param {string} [remote] remote to push to.
  319. * @param {string} [branch] branch to push to.
  320. * @param {Options} [options] options supported by [git](https://git-scm.com/docs/git-push).
  321. * @returns {Promise<void>}
  322. */
  323. push(remote?: string, branch?: string, options?: Options): Promise<void>;
  324. /**
  325. * Pushes the current tag changes to a remote which can be either a URL or named remote. When not specified uses the
  326. * default configured remote spec.
  327. *
  328. * @param {string} [remote]
  329. */
  330. pushTags(remote?: string): Promise<string>;
  331. /**
  332. * Executes any command against the git binary.
  333. *
  334. * @param {string[]|Object} commands
  335. */
  336. raw(commands: string | string[]): Promise<string>;
  337. /**
  338. * Rebases the current working copy. Options can be supplied either as an array of string parameters
  339. * to be sent to the `git rebase` command, or a standard options object.
  340. *
  341. * @param {Object|String[]} [options]
  342. */
  343. rebase(options?: Options | string[]): Promise<string>;
  344. /**
  345. * Call any `git remote` function with arguments passed as an array of strings.
  346. *
  347. * @param {string[]} options
  348. */
  349. remote(options: string[]): Promise<void | string>;
  350. /**
  351. * Removes an entry from the list of remotes.
  352. *
  353. * @param {string} remoteName Name of the repository - eg "upstream"
  354. * @returns {*}
  355. */
  356. removeRemote(remote: string): Promise<void>;
  357. /**
  358. * Reset a repo
  359. *
  360. * @param {string|string[]} [mode=soft] Either an array of arguments supported by the 'git reset' command, or the string value 'soft' or 'hard' to set the reset mode.
  361. */
  362. reset(mode?: 'soft' | 'mixed' | 'hard' | 'merge' | 'keep'): Promise<null>;
  363. reset(commands?: string[]): Promise<void>;
  364. /**
  365. * Revert one or more commits in the local working copy
  366. *
  367. * @param {string} commit The commit to revert. Can be any hash, offset (eg: `HEAD~2`) or range (eg: `master~5..master~2`)
  368. * @param {Object} [options] Optional options object
  369. */
  370. revert(commit: String, options?: Options): Promise<void>;
  371. /**
  372. * Wraps `git rev-parse`. Primarily used to convert friendly commit references (ie branch names) to SHA1 hashes.
  373. *
  374. * Options should be an array of string options compatible with the `git rev-parse`
  375. *
  376. * @param {string[]} [options]
  377. *
  378. * @returns Promise<string>
  379. *
  380. * @see https://git-scm.com/docs/git-rev-parse
  381. */
  382. revparse(options?: string[]): Promise<string>;
  383. /**
  384. * Removes the named files from source control.
  385. *
  386. * @param {string|string[]} files
  387. */
  388. rm(paths: string | string[]): Promise<void>;
  389. /**
  390. * Removes the named files from source control but keeps them on disk rather than deleting them entirely. To
  391. * completely remove the files, use `rm`.
  392. *
  393. * @param {string|string[]} files
  394. */
  395. rmKeepLocal(paths: string | string[]): Promise<void>;
  396. /**
  397. * Show various types of objects, for example the file at a certain commit
  398. *
  399. * @param {string[]} [options]
  400. */
  401. show(options?: string[]): Promise<string>;
  402. /**
  403. * Disables/enables the use of the console for printing warnings and errors, by default messages are not shown in
  404. * a production environment.
  405. *
  406. * @param {boolean} silence
  407. * @returns {simplegit.SimpleGit}
  408. */
  409. silent(silence?: boolean): simplegit.SimpleGit;
  410. /**
  411. * Stash the local repo
  412. *
  413. * @param {Object|Array} [options]
  414. */
  415. stash(options?: Options | any[]): Promise<string>;
  416. /**
  417. * List the stash(s) of the local repo
  418. *
  419. * @param {Object|Array} [options]
  420. */
  421. stashList(options?: Options | string[]): Promise<resp.ListLogSummary>;
  422. /**
  423. * Show the working tree status.
  424. *
  425. * @returns {Promise<StatusResult>} Parsed status result.
  426. */
  427. status(): Promise<StatusResult>;
  428. /**
  429. * Call any `git submodule` function with arguments passed as an array of strings.
  430. *
  431. * @param {string[]} options
  432. */
  433. subModule(options: string[]): Promise<string>;
  434. /**
  435. * Add a submodule
  436. *
  437. * @param {string} repo
  438. * @param {string} path
  439. */
  440. submoduleAdd(repo: string, path: string): Promise<void>;
  441. /**
  442. * Initialize submodules
  443. *
  444. * @param {string[]} [args]
  445. */
  446. submoduleInit(options?: string[]): Promise<string>;
  447. /**
  448. * Update submodules
  449. *
  450. * @param {string[]} [args]
  451. */
  452. submoduleUpdate(options?: string[]): Promise<string>;
  453. /**
  454. * List all tags. When using git 2.7.0 or above, include an options object with `"--sort": "property-name"` to
  455. * sort the tags by that property instead of using the default semantic versioning sort.
  456. *
  457. * Note, supplying this option when it is not supported by your Git version will cause the operation to fail.
  458. *
  459. * @param {Object} [options]
  460. */
  461. tag(options?: Options | string[]): Promise<string>;
  462. /**
  463. * Gets a list of tagged versions.
  464. *
  465. * @param {Options} options
  466. * @returns {Promise<TagResult>} Parsed tag list.
  467. */
  468. tags(options?: Options): Promise<TagResult>;
  469. /**
  470. * Updates repository server info
  471. */
  472. updateServerInfo(): Promise<string>;
  473. }
  474. type Options = { [key: string]: null | string | any };
  475. type LogOptions<T = resp.DefaultLogFields> = Options & {
  476. format?: T;
  477. file?: string;
  478. from?: string;
  479. multiLine?: boolean;
  480. symmetric?: boolean;
  481. to?: string;
  482. };
  483. // responses
  484. // ---------------------
  485. interface BranchSummary extends resp.BranchSummary {}
  486. interface CommitSummary extends resp.CommitSummary {}
  487. interface MergeSummary extends resp.MergeSummary {}
  488. interface PullResult extends resp.PullResult {}
  489. interface FetchResult extends resp.FetchResult {}
  490. interface StatusResult extends resp.StatusResult {}
  491. interface DiffResult extends resp.DiffResult {}
  492. interface TagResult extends resp.TagResult {}
  493. type outputHandler = (
  494. command: string,
  495. stdout: NodeJS.ReadableStream,
  496. stderr: NodeJS.ReadableStream
  497. ) => void
  498. }
  499. export = simplegit;