aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
AgeCommit message (Collapse)Author
2020-09-20Increase version v0.2.0 -> v0.2.1HEADv0.2.1masterTeddy Wing
2020-08-30Increase version v0.1.0 -> v0.2.0v0.2.0Teddy Wing
2020-08-26Add `diff_options` to parse Git diff optionsTeddy Wing
In order to accept Git diff options on the command line, we need a way to extract these from args. Add a function to parse diff options. These were sourced from: https://github.com/git/git/blob/675a4aaf3b226c0089108221b96559e0baae5de9/Documentation/diff-options.txt The function loops through the input arguments and extracts all diff options and their values. Still needs some work to integrate it into `git-sugdiff`, but it currently works as advertised.
2020-08-04Add license (GNU GPLv3+)Teddy Wing
2020-08-03Config: Add a version argumentTeddy Wing
Allow getting the program version from the command line.
2020-08-02lib.rs: Make `owner_repo` module privateTeddy Wing
Turns out this didn't need to be public at all.
2020-08-02git-sugpatch: Move the suggestion arguments loop to a new functionTeddy Wing
Split this out into a separate function because I want to reuse it in `git-sugapply`.
2020-08-02lib.rs: Make `owner_repo` `pub(crate)` againTeddy Wing
Made this `pub` for one of the test tries in 38a871f28bad90e238021d8cc46b9fa926f9df75, but in the end it wasn't necessary. Restore this to the prior visibility.
2020-08-02git-sugpatch: Error if no remote and suggestion ID argumentTeddy Wing
We want to allow not having a remote when URL arguments are given, but require it when a suggestion ID argument is given (otherwise we wouldn't have an owner-repo pair to make the GitHub request). Had some trouble with the `OwnerRepo.o_r` value. It was being moved into the closure, so tried a loop. There was a similar problem with the loop. However, by returning, I was able to get a reference to the `Result` instead of having it be moved.
2020-08-02git-sugpatch: Accept multiple suggestion args; Accept suggestion ID argsTeddy Wing
Add the ability to pass multiple suggestions to the binary. The diffs will appear in the output one after another. Also allow suggestions to be specified by their comment ID instead of their full URL. This uses the Git repo's remote to determine the owner-repo pair. When a URL argument is given, the owner-repo pair is extracted from the URL. It's now possible to call the binary like this: $ git-sugpatch 459692838 \ > https://github.com/teddywing/suggestion-test/pull/1#discussion_r459691747
2020-08-01Get configTeddy Wing
We need the following values in order to build a `Client`: * GitHub token * Repo owner * Repo name Get the token from a command-line argument, or else the Git config, or else an environment variable. Get the repo identifiers from the repo's remote URL. Use the remote specified as a command-line argument, otherwise get it from the Git config, or else default to "origin". TODO: Only try to get the owner-name pair from the remote if a comment ID was given, not if a URL argument was passed.
2020-08-01lib.rs: Move code to `owner_repo.rs`Teddy Wing
Need to add other code to the library.
2020-08-01Move `github-suggestion-cli` to the root crateTeddy Wing
This will be used by the binaries.
2020-08-01Move library to a separate crateTeddy Wing
Keep the binaries in the root crate, and add a new crate for the `github-suggestion` library. I want to add some library code to the CLI programs to extract common functionality between the different binaries.
2020-07-28Move `Client` to a new moduleTeddy Wing
2020-07-28Move `Suggestion` to a new moduleTeddy Wing
Planning to separate the `Suggestion` and `Client` code.
2020-07-28Suggestion.apply(): Error when patch doesn't applyTeddy Wing
Don't apply the patch when it doesn't apply, instead fail with an error. Previously, the `apply()` method would apply the patch to the lines from `Suggestion`, even if the file was different from what the suggestion expected. So, you could end up with the `Suggestion` lines being overwritten with the suggestion text, even when they didn't match the original side of the suggestion. Fix this by getting the diff text from `diff_with_repo()` and applying it using `git-apply`. This gives us a suitable error when the patch doesn't apply to the file, leaving the file alone.
2020-07-27Test `Suggestion.diff()`Teddy Wing
Add a test for diff generation. Split `diff()` into `diff_with_repo()` to allow us to test with a new repo instead of defaulting to the current one. In the test, create a new Git repo using `git2`, add and commit a file, and check that the diff generated by a Suggestion is correct. Got the `IndexEntry` format from https://docs.rs/git2/0.13.8/src/git2/index.rs.html#858-871.
2020-07-26Suggestion.apply_to(): Change `from` to be `BufRead`Teddy Wing
Take a `BufRead`er instead of a file path to read from. This gives us more flexibility. We can't use the `Read` trait because of the error described in b4a7561ccc1ed7dab6cd2c786902dec8c4ad9be6. Fix the bug in `Suggestion.diff()` so that the suggestion is applied to the commit it corresponds to instead of HEAD.
2020-07-26Suggestion: Update tests to add new `commit` fieldTeddy Wing
2020-07-26Suggestion: Add `diff` methodTeddy Wing
Create a patch file for the suggestion. Instead of building the patch text manually, as in my earlier attempts (`patch` and `unified_diff`), apply the patch to the file in memory, and then ask libgit2 to give us a Git patch by comparing the original blob and the patched buffer. Doing this because I was unsuccessful in manually composing a patch file that Git would accept. I could create unified diffs that the `patch` program would accept, but these wouldn't work with `git-apply`. My current guess is that these didn't work because I didn't have both before and after context in my manually-created patch files. Since this tool is intended to be used with Git, I want a patch file that will work transparently with Git utilities. The easiest alternative to manually generating the patch file seemed to be to have Git create the patch text. Add a new binary, `git-sugpatch`, that outputs the patch text to standard output. TODO: This gets the original from the current HEAD in `apply_to()`. We should instead be using the contents of `blob`, because the original is not necessarily the working copy.
2020-07-26Remove `patches_file` testTeddy Wing
This is no longer absolutely necessary now that we have the `suggestion_apply_to_writes_patch_to_writer` test.
2020-07-26Suggestion.apply_to(): Move CRLF check to a new functionTeddy Wing
2020-07-26Suggestion.apply_to(): Use line endings consistent with fileTeddy Wing
Make the suggestion's line endings consistent with those used in the file. By default, the suggestion block we get from the GitHub API uses CRLF line endings. These need to be converted to be consistent with the file the change is being applied to.
2020-07-26suggestion_apply_to_writes_patch_to_writer: Fix line numbersTeddy Wing
Now that I fixed the line numbers in the code (0ea9202eb81e18647b40563fd3dbc61b626ce2ec), we need to correct them in the test as well. The empty line after the `r#"` delimiter is the first line.
2020-07-25Suggestion.apply_to(): Use correct line numbersTeddy Wing
We had been using the enumerator's index, which starts at 0, but line numbers start at 1. Use the correct line number.
2020-07-25Suggestion.apply_to(): Fix single-line patchesTeddy Wing
Single-line changes weren't getting applied, because if only a single line changed, the `original_start_line` field is null. That caused the first `if` branch to always evaluate to true. Swap the `if` branches to fix this.
2020-07-25Suggestion.apply_to(): Read from given reader, not original fileTeddy Wing
We can't read from the original file, because we want to be able to use that as the writer in `apply()`. Previously, this would result in the original file simply being truncated, as we weren't reading from the correct file. To fix this, we need to give `apply_to()` both a reader and a writer. Now, in `apply()`, we can pass it the temporary file to read, and the original to write to. Used a `Path` as an input to `apply_to()` instead of a `Read`er because I got an error that didn't really make sense to me when I did so: error[E0275]: overflow evaluating the requirement `&tokio_reactor::poll_evented::PollEvented<_>: std::io::Read` | = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`git_suggested_patch`) = note: required because of the requirements on the impl of `std::io::Read` for `&tokio_reactor::poll_evented::PollEvented<tokio_reactor::poll_evented::PollEvented<_>>` = note: required because of the requirements on the impl of `std::io::Read` for `&tokio_reactor::poll_evented::PollEvented<tokio_reactor::poll_evented::PollEvented<tokio_reactor::poll_evented::PollEvented<_>>>` A search for the error message turned up a few active issues: - https://github.com/rust-lang/rust/issues/57854 - https://github.com/rust-lang/rust/issues/62755 - https://github.com/rust-lang/rust/issues/39959 The above issues indicate that this could come from having a variable with an inferred type. However, for the moment the issue isn't resolved, and the error message doesn't indicate where the problem is actually coming from. As I was unable to track down the source of the problem, I decided not to bother using a `Read` argument.
2020-07-25Suggestion: Make `original_start_line` an `Option`Teddy Wing
This field can be null in the API response.
2020-07-25Add git-sugapply binTeddy Wing
An executable to apply a suggested change to the appropriate file in the repo. Add a new `SuggestionUrl` type that allows us to extract the necessary data to fetch a suggestion comment from the GitHub API using a GitHub pull request comment URL.
2020-07-25Suggestion.apply(): Use `Suggestion.apply_to()`Teddy Wing
Replace common code with a call to `Suggestion.apply_to()`.
2020-07-25Suggestion: Add `apply_to` methodTeddy Wing
I didn't feel like writing a test for `Suggestion.apply()`, which writes files, so decided to write a different version that used the original file and a writer. Changed the `Suggestion.suggestion()` function to not prepend "+" to suggestion lines so we can use the function to extract the suggested lines unmodified from the comment. Kept the old version around as `suggestion_patch()` because we may need it to generate patches. Copied the code from `Suggestion.apply()`, except write the changes to `writer` instead of the file at `Suggestion.path`.
2020-07-25suggestion_patch_generates_patch: Add missing new fieldsTeddy Wing
Add the new fields from `Suggestion`.
2020-07-25Suggestion: Rename `suggestion` field to `comment`Teddy Wing
The name "suggestion" sounds like the suggested diff change, when really this field will be deserialised with the whole GitHub comment, including adjacent text as well as the suggestion.
2020-07-25Suggestion.apply: Try writing to the original fileTeddy Wing
Haven't tested this yet. To keep the original file's metadata, try writing the changes to the original instead of the temporary file. First copy the original to a new file that we can reference after truncating the original, then write the change onto the original.
2020-07-25Suggestion: Try applying a change to the file (WIP)Teddy Wing
Not working yet, but the idea is to get the file referenced by the suggestion and apply the suggested change directly to it. Here, we read the original file, and write its contents to the new file, replacing the lines modified by the suggestion with the suggested change. Currently doesn't work because you can't pass file instances to `fs::rename`, only paths. I'd like to try a slightly different system so I can keep the original file's creation date. With the current system, it would be overwritten by the new temporary file created to store the change.
2020-07-25lib.rs: Skip network testTeddy Wing
2020-07-24Read a blob from a repositoryTeddy Wing
Work out the code required to get a file blob's contents from a Git repository using the 'git2' crate. We'll be using this to get the original file for patching.
2020-07-24Add test stub for patch functionalityTeddy Wing
Will test patching a file using the generated suggestion patch.
2020-07-22Try modifying diff hunk with 'unidiff'Teddy Wing
Testing out 'unidiff' to build the patch. Trying to see if it will adjust the line numbers automatically when the patch is modified. Doesn't look like it. Seems like it has a nice interface, though. Still having trouble applying the patch due to complaints of patch format errors.
2020-07-22Suggestion.suggestion: Handle "\r\n"Teddy Wing
Update the regex matcher to allow both LF line endings and CRLF.
2020-07-21suggestion_patch_generates_patch: Add attributionTeddy Wing
Describe the source of the test data.
2020-07-21Suggesstion: Implement `patch` to generate a patch for the suggestionTeddy Wing
Still working on it, but have an initial draft working. There's no file name in the diff hunk, so I'm assuming I'm going to have to add one later. Build a patch from the diff hunk and suggestion comment. 1. Remove `-` lines 2. Change `+` lines to ` ` 3. Change last line to `-` 4. Append suggestion to the diff with a `+` prefix
2020-07-19Extract diff hunk and comment body from GitHub responseTeddy Wing
Split the `Suggestion` struct into a new `Client` struct for the GitHub request, and `Suggestion` for the required response data.
2020-07-19Fetch a GitHub PR suggestion commentTeddy Wing
Add a `Suggestion` struct to represent a GitHub PR suggestion comment. Use the 'github-rs' library to fetch a given comment from the site by its ID. Converted the 'github-rs' error into a string because its `github_rs::client::Error` type is private, so I can't define an error variant source with that type.