Skip to content

Commit d37826e

Browse files
authored
Merge pull request #80 from mimre25/add-sort-by-time
Add sort by time
2 parents bf62752 + 09e2111 commit d37826e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ script into your `$PATH` and `$fpath` respectively.
3939

4040
```
4141
git-fixup [-s|--squash] [-f|--fixup] [-a|--amend] [-c|--commit] [--no-verify]
42-
[--rebase] [-b|--base <rev>] [<ref>]
42+
[--rebase] [-b|--base <rev>] [-r|--reverse] [<ref>]
4343
```
4444

4545
For this tool to make any sense you should enable the `rebase.autosquash`
@@ -143,6 +143,10 @@ If omitted, the default base commit is resolved in the following order:
143143
4. Finally, the root commit (i.e. full history) if nothing of the above is
144144
satisfied.
145145

146+
### --reverse
147+
148+
Commits are sorted by time. `-r` reverses the sort order.
149+
146150
## Configuration
147151

148152
`git-fixup` uses configuration from the ENVIRONMENT or from `git config`
@@ -193,6 +197,20 @@ a simple [default menu](the-default-menu) will be used.
193197
See [External menu](#external-menu) for more details and a more advanced
194198
example.
195199

200+
### fixup.additionalSortFlags
201+
202+
Or `GITFIXUPADDITIONALSORTFLAGS`
203+
204+
Sets the flags that are passed to sort in addition to the default sort flags
205+
that enable sorting by time.
206+
207+
For example,
208+
209+
```bash
210+
# Always sort the commits by time reversed
211+
$ git config --global fixup.additionalSortFlags '-r'
212+
```
213+
196214
## Tab completion
197215

198216
Tab completion for zsh/fish is implemented. The suggestions for the tab completion

git-fixup

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ no-rebase Don't do a rebase after commit
1818
n,no-verify Bypass the pre-commit and commit-msg hooks
1919
b,base=rev Use <rev> as base of the revision range for the search
2020
A,all Show all candidates
21+
r,reverse Reverse the sort
2122
"
2223
# shellcheck disable=SC2034
2324
SUBDIRECTORY_OK=yes
@@ -69,7 +70,7 @@ print_sha () {
6970
local sha=$1
7071
local type=$2
7172

72-
git --no-pager log --format="%H [$type] %s <%ae>" -n 1 "$sha"
73+
git --no-pager log --format="%h %ai [$type] %s <%ae>" -n 1 "$sha"
7374
}
7475

7576
# Call git commit
@@ -164,6 +165,11 @@ show_menu () {
164165
fi
165166
}
166167

168+
sort_commits () {
169+
# shellcheck disable=SC2086
170+
sort $sort_flags $additional_sort_flags
171+
}
172+
167173
git_commit_args=()
168174
target=
169175
op=${GITFIXUPACTION:-$(git config --default=fixup fixup.action)}
@@ -172,7 +178,8 @@ fixup_menu=${GITFIXUPMENU:-$(git config --default="" fixup.menu)}
172178
create_commit=${GITFIXUPCOMMIT:-$(git config --default=false --type bool fixup.commit)}
173179
base=${GITFIXUPBASE:-$(git config --default="" fixup.base)}
174180
show_all=false
175-
181+
sort_flags="-k2 -k3 -k4" # default flags to sort by time (eg 2025-01-03 10:04:43 +0100, hence 3 fields)
182+
additional_sort_flags=${GITFIXUPADDITIONALSORTFLAGS:-$(git config --default="" fixup.additionalSortFlags)}
176183
while test $# -gt 0; do
177184
case "$1" in
178185
-s|--squash)
@@ -209,6 +216,9 @@ while test $# -gt 0; do
209216
-A|--all)
210217
show_all=true
211218
;;
219+
-r|--reverse)
220+
additional_sort_flags="-r"
221+
;;
212222
--)
213223
shift
214224
break
@@ -264,7 +274,7 @@ else
264274
fi
265275

266276
if test "$create_commit" = "true"; then
267-
target=$(print_candidates | show_menu)
277+
target=$(print_candidates | sort_commits | show_menu)
268278
if test -z "$target"; then
269279
exit
270280
fi
@@ -273,5 +283,5 @@ if test "$create_commit" = "true"; then
273283
call_rebase "${target%% *}"
274284
fi
275285
else
276-
print_candidates
286+
print_candidates | sort_commits
277287
fi

0 commit comments

Comments
 (0)