Skip to content

Format all code with cargo fmt #2385

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

Merged
merged 1 commit into from
Apr 10, 2021
Merged

Format all code with cargo fmt #2385

merged 1 commit into from
Apr 10, 2021

Conversation

exrook
Copy link
Contributor

@exrook exrook commented Apr 5, 2021

To assuage the concerns noted in #2338 regarding blocking other in progress PRs I have developed a process to rebase branches on top of this branch by formatting each commit. I have already rebased the following PRs which have no merge conflicts with master.

master...exrook:pr2384 2384
master...exrook:pr2374 2374 has conflicts now
master...exrook:pr2355 2355 has conflicts now
master...exrook:pr2352 2352
master...exrook:pr2328 2328
master...exrook:pr2244 2244
master...exrook:pr1956 1956

Additionally I have created .git-blame-ignore-revs and added the format commit to it to improve git-blame output, see:
https://git-scm.com/docs/git-blame#Documentation/git-blame.txt---ignore-revs-fileltfilegt

git config blame.ignoreRevsFile .git-blame-ignore-revs
Instructions/script for updating a branch

Convert current branch - Rebase method (leaves a nice history, manual, for branches with conflicts)

This method may be undesirable for branches with many non format related conflicts, as you will have to fix them for every commit.

git fetch upstream master # Make sure you have the latest upstream changes available

Do as rebase instructs to resolve any conflicts here!

git rebase dd019055ef5bf4309f15db934407e202caf52e14~ # The commit before the format changes

Format all commits, should not require intervention

REV_SPEC=$(git rev-list dd019055ef5bf4309f15db934407e202caf52e14~..HEAD | tail -n1)~~..HEAD
git filter-branch -f --tree-filter 'cargo fmt -- --config disable_all_formatting=false --config use_small_heuristics="Max"' $REV_SPEC

Rebase onto the formatted master, should not require intervention

git rebase master

Push rewritten commits

git push -f

Batch convert PRs: usage ./script.sh <pr numbers>

Will have problems with prs with merge conflicts. You probably want to do this on a fresh clone. Replace upstream with the name of your rust-lang/futures-rs remote

#!/bin/bash

export FILTER_BRANCH_SQUELCH_WARNING=1
PRS=$@
cd futures-rs
git checkout master
git fetch -f upstream master
for pull in $PRS; do
    echo PR $pull
    git fetch -f upstream pull/$pull/head:pr$pull
    echo FETCH DONE
    git checkout pr$pull
    echo CHECKOUT DONE
    git rebase dd019055ef5bf4309f15db934407e202caf52e14~ || git rebase --abort
    echo REBASE 1 DONE
    if test -z $(git rev-list dd019055ef5bf4309f15db934407e202caf52e14~..HEAD | tail -n1); then
        echo PR$pull ALREADY MERGED, SKIPPING
        continue
    fi
    REV_SPEC=$(git rev-list dd019055ef5bf4309f15db934407e202caf52e14~..HEAD | tail -n1)~~..HEAD
    git filter-branch -f --tree-filter 'cargo fmt -- --config disable_all_formatting=false --config use_small_heuristics="Max"' $REV_SPEC && \
        echo FILTER BRANCH DONE && \
        git rebase master || git rebase --abort
        echo REBASE 2 DONE && \
        git push -fu origin pr$pull && \
        echo PUSH DONE
    echo PR $pull DONE
done
cd ..

Convert current branch - Merge method (manual, for branches with many non-format conflicts)

cargo format
git add .
git commit -m "Update formatting"
git push

@exrook exrook requested a review from taiki-e as a code owner April 5, 2021 01:59
Copy link
Member

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. r=me after nit addressed

If this commit is showing up in your git blame, do the following:
git config blame.ignoreRevsFile .git-blame-ignore-revs
@exrook
Copy link
Contributor Author

exrook commented Apr 10, 2021

Recreated on top of master and fixed .rustfmt.toml
r? @taiki-e

Copy link
Member

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@taiki-e taiki-e merged commit dd01905 into rust-lang:master Apr 10, 2021
@taiki-e taiki-e mentioned this pull request Apr 23, 2021
@ibraheemdev
Copy link
Member

Why don't you add a cargo fmt action to the gh workflow?

@taiki-e
Copy link
Member

taiki-e commented May 7, 2021

Filed #2421 to add fmt check to CI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants