有没有工具可以在Git中重新定位一个分支,同时在每个提交时运行go fmt?

huangapple go评论86阅读模式
英文:

Any tools rebase a branch in Git while running go fmt each commit separately?

问题

我希望使用像go fmtindent这样的源代码格式化工具来重建/重新基于Git分支X中的所有提交。

我期望的工作流大致包括从master分支创建一个新分支,并在以下步骤中迭代$_表示X中的提交:

git cherry-pick $_
go fmt ...
git commit -a --amend

或者甚至可以使用以下步骤:

git cherry-pick -n $_
go fmt ...
git cherry-pick --continue

不过,我不认为-n--continue可以这样一起使用。完成后,我会在X上进行一个go fmt提交,并使用go diff X new进行比较。

然而,这个过程中可能会出现许多问题,比如-a试图更改原始提交中未更改的文件,go fmt混淆了Git的补丁,Git更改了提交日期等等。

虽然这些问题都不是特别麻烦,但如果有一个更简洁的工具或简化的工作流程可以更清晰地完成这个任务,我会很乐意了解。

英文:

I wish to rebuild/rebase all commits in a Git branch X using a source code formatting tool like go fmt or indent.

I'd expect the workflow to roughly consist of making a new branch off master and iterating the following with $_ ranging over the commits in X:

git cherry-pick $_
go fmt ...
git commit -a --amend

Or maybe even

git cherry-pick -n $_
go fmt ...
git cherry-pick --continue

I wouldn't expect -n and --continue to play together like that, though. Also, one should naturally do a go fmt commit to X and go diff X new when done.

However, there are many steps that can go wrong with this procedure, like the -a seeking to change files that weren't changed in the original commit, go fmt confusing Git's patching, Git changing the commit dates, etc.

None of that is particularly troublesome, but if a quick tool or simpler workflow does this more cleanly, then I'd love to know about it.

答案1

得分: 1

但是如果有一个更简洁的工具或者更简单的工作流能更干净地完成这个任务,那我很想知道。

正如Joseph K. Strauss评论中提到的那样,git filter-branch应该足够了,再加上使用三个点符号的go fmt

git filter-branch --tree-filter 'go fmt ...'

你可以在“git filter-branch - discard the changes to a set of files in a range of commits”中阅读更多关于filter-branch的内容。

这个命令将对所有本地分支运行,并且会改变历史(因为每个修改的提交都会生成新的SHA1)。可能需要使用git push --force将新的历史发布到上游仓库:请提醒其他协作者重置他们的本地仓库。

英文:

> but if a quick tool or simpler workflow does this more cleanly, then I'd love to know about it.

As Joseph K. Strauss mentions in the comments, git filter-branch should be enough, plus go fmt using the three dot notation:

git filter-branch --tree-filter 'go fmt ...'

You can read (much) more on filter-branch at "git filter-branch - discard the changes to a set of files in a range of commits".

That command will run against all local branch, and will change the history (since new SHA1 will be generated for each modified commit).
A git push --force might be needed to publish the new history to its upstream repo: do warn the other collaborators, for them to reset their local repos.

huangapple
  • 本文由 发表于 2015年1月2日 14:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/27738571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定