Skip to contentSkip to conversation

tg-patch: fix pagination

PATCH In review Kirill Smelkov 7 participants 14 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover tg-patch: fix pagination

Kirill Smelkov (2):
Implement setup_pager just like in git
tg-patch: fix pagination

 tg-patch.sh |    3 +++
 tg.sh       |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)
#1 lore raw

PATCH 1/2 Implement setup_pager just like in git

setup_pager() spawns a pager process and redirect the rest of our output to it.

This will be needed to fix tg patch output in the next commit.

---
 tg.sh |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 53 insertions(+), 0 deletions(-)
tg.sh +53 -0
index 8c23d26..51a82af 100644
@@ -243,6 +243,59 @@ do_help()
 	fi
 }
 
+## Pager stuff
+
+# isatty FD
+isatty()
+{
+	tty -s 0<&$1 || return 1
+	return 0
+}
+
+# setup_pager
+# Spawn pager process and redirect the rest of our output to it
+setup_pager()
+{
+	isatty 1 || return 0
+
+	# TG_PAGER = GIT_PAGER | PAGER
+	# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-03/0792.html
+	case ${GIT_PAGER+XXX} in
+	'')
+		case ${PAGER+XXX} in
+		'')
+			# both GIT_PAGER & PAGER unset
+			TG_PAGER=''
+			;;
+		*)
+			TG_PAGER="$PAGER"
+			;;
+		esac
+		;;
+	*)
+		TG_PAGER="$GIT_PAGER"
+		;;
+	esac
+
+	[ -z "$TG_PAGER"  -o  "$TG_PAGER" = "cat" ]  && return 0
+
+
+	# now spawn pager
+	export LESS=${LESS:-FRSX}	# as in pager.c:pager_preexec()
+
+	_pager_fifo="$(mktemp -t tg-pager-fifo.XXXXXX)"
+	rm "$_pager_fifo" && mkfifo -m 600 "$_pager_fifo"
+
+	"$TG_PAGER" < "$_pager_fifo" &
+	exec > "$_pager_fifo"		# dup2(pager_fifo.in, 1)
+
+	# this is needed so e.g. `git diff` will still colorize it's output if
+	# requested in ~/.gitconfig with color.diff=auto
+	export GIT_PAGER_IN_USE=1
+
+	# atexit(close(1); wait pager)
+	trap "exec >&-; rm $_pager_fifo; wait" EXIT
+}
 
 ## Startup
 
Signature · 1 line
1.6.1.48.ge9b8
#2 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

Thanks, Kirill, for the patches. A couple of comments inline. I hope Petr has a chance to look too.

also sprach Kirill Smelkov <[email protected]> [2009.01.06.1616 +0100]:

tg.sh do_help() · line 253

Quoted 6 lines
tg.sh +6 -0
+# isatty FD
+isatty()
+{
+	tty -s 0<&$1 || return 1
+	return 0
+}

You don't need any of the return statements. Functions' return values are the return values of the last commands they execute. Since we are not using set -e, just "tty -s 0<&$1" in the body will have the same effect.

tg.sh do_help() · line 267

Quoted 7 lines
tg.sh +7 -0
+	# TG_PAGER = GIT_PAGER | PAGER
+	# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-03/0792.html
+	case ${GIT_PAGER+XXX} in
+	'')
+		case ${PAGER+XXX} in
+		'')
+			# both GIT_PAGER & PAGER unset

I find this very confusing. Why not simply

  TG_PAGER="${GIT_PAGER:-}"
  TG_PAGER="${TG_PAGER:-$PAGER}"

?

Quoted 1 line

+ [ -z "$TG_PAGER" -o "$TG_PAGER" = "cat" ] && return 0

What if I set my pager to /bin/cat? But I suppose then there's just a wasted FIFO and process, nothing big.

tg.sh do_help() · line 287

Quoted 2 lines
tg.sh +2 -0
+	_pager_fifo="$(mktemp -t tg-pager-fifo.XXXXXX)"
+	rm "$_pager_fifo" && mkfifo -m 600 "$_pager_fifo"

There's a race condition here. I can't see a real problem with it, though, nor do I know of a better way.

tg.sh do_help() · line 297

Quoted 9 lines
tg.sh +9 -0
+	"$TG_PAGER" < "$_pager_fifo" &
+	exec > "$_pager_fifo"		# dup2(pager_fifo.in, 1)
+
+	# this is needed so e.g. `git diff` will still colorize it's output if
+	# requested in ~/.gitconfig with color.diff=auto
+	export GIT_PAGER_IN_USE=1
+
+	# atexit(close(1); wait pager)
+	trap "exec >&-; rm $_pager_fifo; wait" EXIT

Consistency: $_pager_fifo is not passed as a quoted string to rm here. In the unlikely event that $TMPDIR contains a space, this would fail.

I definitely want Petr's opinion on this before I integrate it.

Signature · 5 lines
martin | http://madduck.net/ | http://two.sentenc.es/
 
if you see an onion ring -- answer it!
 
spamtraps: [email protected]
#3 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

Quoted 5 lines · martin f krafft

tg.sh do_help() · line 287

Quoted 2 lines
tg.sh +2 -0
+	_pager_fifo="$(mktemp -t tg-pager-fifo.XXXXXX)"
+	rm "$_pager_fifo" && mkfifo -m 600 "$_pager_fifo"

There's a race condition here. I can't see a real problem with it, though, nor do I know of a better way.

mktemp -d, and create the fifo inside the directory created by mktemp.

Signature · 4 lines
Adeodato Simó                                     dato at net.com.org.es
Debian Developer                                  adeodato at debian.org
 
A hacker does for love what other would not do for money.
#4 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

In reply to martin f krafft

Martin, thanks for your review.

I'll too reply inline:

Quoted 4 lines · martin f krafft

Thanks, Kirill, for the patches. A couple of comments inline. I hope Petr has a chance to look too.

also sprach Kirill Smelkov <[email protected]> [2009.01.06.1616 +0100]:

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+# isatty FD
+isatty()
+{
+	tty -s 0<&$1 || return 1
+	return 0
+}

You don't need any of the return statements. Functions' return values are the return values of the last commands they execute.

Agree, I'll rework isatty to be just

isatty()

{
	tty -s 0<&$1
}
Quoted 2 lines

Since we are not using set -e, just "tty -s 0<&$1" in the body will have the same effect.

We _are_ using set -e in tg.sh:

http://repo.or.cz/w/topgit.git?a=blob;f=tg.sh;h=8c23d26b9a62ddcc1869bb70299862c32edd4403;hb=HEAD#l254

And also I think tty -s 0<&$1 is a bit obscure, so maybe it is still better to put it into a function with well-known name such as isatty?

Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +7 -0
+	# TG_PAGER = GIT_PAGER | PAGER
+	# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-03/0792.html
+	case ${GIT_PAGER+XXX} in
+	'')
+		case ${PAGER+XXX} in
+		'')
+			# both GIT_PAGER & PAGER unset

I find this very confusing. Why not simply

Quoted 4 lines
  TG_PAGER="${GIT_PAGER:-}"
  TG_PAGER="${TG_PAGER:-$PAGER}"

?

I find it confusing too, but this is needed because they usually do something like this

    $ GIT_PAGER='' <some-git-command>

to force it to be pagerless.

For example in git-rebase.sh:

http://repo.or.cz/w/git.git?a=blob;f=git-rebase.sh;h=ebd4df3a0e821ddcfd1eabfcaac17f854e172a85;hb=HEAD#l415

And other places.

So I think it would be better to preserve the same semantics for tg patch callers, though it's a pity that it's hard (maybe I'm wrong ?) to see whether an env var is unset.

I'll add additional note about why this is needed.

Quoted 5 lines
Quoted 2 lines

+ [ -z "$TG_PAGER" -o "$TG_PAGER" = "cat" ] && return 0

What if I set my pager to /bin/cat? But I suppose then there's just a wasted FIFO and process, nothing big.

Yes. I'd drop "cat" from here completely, but since I was mimicing pager.c I left it:

http://repo.or.cz/w/git.git?a=blob;f=pager.c;h=f19ddbc87df04f117cd5e39189c8322fd5f29d68;hb=HEAD#l55

I'm ok with dropping cat. Should we?

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	_pager_fifo="$(mktemp -t tg-pager-fifo.XXXXXX)"
+	rm "$_pager_fifo" && mkfifo -m 600 "$_pager_fifo"

There's a race condition here. I can't see a real problem with it, though, nor do I know of a better way.

Yes I know and agree here is a race.

But netheir did I knew how to overcome this (I though we'll need mkfifo to support creating fifos with temp names, but mkfifo lacks support for this)

The real problem here is that in bash, it's hard to get such constructs going without fifos at all -- just using pipes like we would do in C, because:

exec >file

redirects the rest of current process output to file, but

exec |less

does _not_ redirect the rest of the current process output through pipe to less.

Likewise

    exec > >(less)

works (process redirection), but is a bashishm, so = not an option for us.

Fortunately Adeodato suggested to use mktemp -d, so this is reworked to:

    _pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
    _pager_fifo="$_pager_fifo_dir/0"
    mkfifo -m 600 "$_pager_fifo"

+ appopriate cleanup.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+	"$TG_PAGER" < "$_pager_fifo" &
+	exec > "$_pager_fifo"		# dup2(pager_fifo.in, 1)
+
+	# this is needed so e.g. `git diff` will still colorize it's output if
+	# requested in ~/.gitconfig with color.diff=auto
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+	export GIT_PAGER_IN_USE=1
+
+	# atexit(close(1); wait pager)
+	trap "exec >&-; rm $_pager_fifo; wait" EXIT

Consistency: $_pager_fifo is not passed as a quoted string to rm here. In the unlikely event that $TMPDIR contains a space, this would fail.

Thanks, corrected.

Quoted 1 line

I definitely want Petr's opinion on this before I integrate it.

Ok, let's wait what Petr says. Here is improved patch:

Changes since v1:

o isatty() simplified
o added a note about different meaning of GIT_PAGER='' and unset GIT_PAGER
o fixed race wrt to mkfifo creation
o fixed potential whitespace problem in "$_pager_fifo" cleanup

(interdiff)

tg.sh +6 -5
index 51a82af..bf9cf5c 100644
@@ -248,8 +248,7 @@ do_help()
 # isatty FD
 isatty()
 {
-	tty -s 0<&$1 || return 1
-	return 0
+	tty -s 0<&$1
 }
 
 # setup_pager
@@ -259,6 +258,7 @@ setup_pager()
 	isatty 1 || return 0
 
 	# TG_PAGER = GIT_PAGER | PAGER
+	# (but differentiate between GIT_PAGER='' and unset variables)
 	# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-03/0792.html
 	case ${GIT_PAGER+XXX} in
 	'')
@@ -283,8 +283,9 @@ setup_pager()
 	# now spawn pager
 	export LESS=${LESS:-FRSX}	# as in pager.c:pager_preexec()
 
-	_pager_fifo="$(mktemp -t tg-pager-fifo.XXXXXX)"
-	rm "$_pager_fifo" && mkfifo -m 600 "$_pager_fifo"
+	_pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
+	_pager_fifo="$_pager_fifo_dir/0"
+	mkfifo -m 600 "$_pager_fifo"
 
 	"$TG_PAGER" < "$_pager_fifo" &
 	exec > "$_pager_fifo"		# dup2(pager_fifo.in, 1)
@@ -294,7 +295,7 @@ setup_pager()
 	export GIT_PAGER_IN_USE=1
 
 	# atexit(close(1); wait pager)
-	trap "exec >&-; rm $_pager_fifo; wait" EXIT
+	trap "exec >&-; rm "$_pager_fifo"; rmdir "$_pager_fifo_dir"; wait" EXIT
 }
 
 ## Startup
Quoted 1 line

From 2193b7c703c2d31c8739eec617b8c0e8c1d09b79 Mon Sep 17 00:00:00 2001

From: Kirill Smelkov <[email protected]>
Date: Tue, 6 Jan 2009 17:56:37 +0300
Subject: [PATCH (topgit) v2] Implement setup_pager just like in git

setup_pager() spawns a pager process and redirect the rest of our output to it.

This will be needed to fix tg patch output in the next commit.

---
 tg.sh |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 54 insertions(+), 0 deletions(-)
tg.sh +54 -0

60 lines not on this page · raw message

Signature · 6 lines
1.6.1.48.ge9b8



Thanks,
Kirill
#5 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

Quoted 14 lines · Kirill Smelkov
Quoted 6 lines · martin f krafft

I find this very confusing. Why not simply

  TG_PAGER="${GIT_PAGER:-}"
  TG_PAGER="${TG_PAGER:-$PAGER}"

?

I find it confusing too, but this is needed because they usually do something like this

    $ GIT_PAGER='' <some-git-command>

to force it to be pagerless.

[...]

Quoted 3 lines

So I think it would be better to preserve the same semantics for tg patch callers, though it's a pity that it's hard (maybe I'm wrong ?) to see whether an env var is unset.

Admittedly I haven't really studied your patch, but the ${} constructs can in fact tell empty from unset:

  $ EMPTY=
  $ unset UNDEFINED
  $ echo ${UNDEFINED-foo}
  foo
  $ echo ${UNDEFINED:-foo}
  foo
  $ echo ${EMPTY-foo}
  $ echo ${EMPTY:-foo}
  foo

'man bash' indeed says

When not performing substring expansion, bash tests for a parameter that is unset or null; omitting the colon results in a test only for a parameter that is unset.

So I suppose you could use

  ${GIT_PAGER-${PAGER-less}}

or similar.

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#6 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

In reply to Kirill Smelkov
Quoted 19 lines · Kirill Smelkov

Martin, thanks for your review.

I'll too reply inline:

Quoted 4 lines · martin f krafft

Thanks, Kirill, for the patches. A couple of comments inline. I hope Petr has a chance to look too.

also sprach Kirill Smelkov <[email protected]> [2009.01.06.1616 +0100]:

Quoted 9 lines

tg.sh do_help() · line 252

Quoted 6 lines
tg.sh +6 -0
+# isatty FD
+isatty()
+{
+	tty -s 0<&$1 || return 1
+	return 0
+}

You don't need any of the return statements. Functions' return values are the return values of the last commands they execute.

Quoted 6 lines

Agree, I'll rework isatty to be just

isatty()

{
	tty -s 0<&$1
}

why not test -t 0 ? I'm not sure it's POSIX though.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#7 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

Quoted 11 lines · Kirill Smelkov
Quoted 1 line

From 2193b7c703c2d31c8739eec617b8c0e8c1d09b79 Mon Sep 17 00:00:00 2001

From: Kirill Smelkov <[email protected]>
Date: Tue, 6 Jan 2009 17:56:37 +0300
Subject: [PATCH (topgit) v2] Implement setup_pager just like in git

setup_pager() spawns a pager process and redirect the rest of our output to it.

This will be needed to fix tg patch output in the next commit.

But you never use it...?

Quoted 23 lines
---
 tg.sh |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 54 insertions(+), 0 deletions(-)
tg.sh +11 -0
index 8c23d26..bf9cf5c 100644
@@ -243,6 +243,60 @@ do_help()
 	fi
 }
 
+## Pager stuff
+
+# isatty FD
+isatty()
+{
+	tty -s 0<&$1
+}
+
+# setup_pager
+# Spawn pager process and redirect the rest of our output to it
+setup_pager()

tg.sh do_help() · line 266

Quoted 10 lines
tg.sh +10 -0
+{
+	isatty 1 || return 0
+
+	# TG_PAGER = GIT_PAGER | PAGER
+	# (but differentiate between GIT_PAGER='' and unset variables)
+	# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-03/0792.html
+	case ${GIT_PAGER+XXX} in
+	'')
+		case ${PAGER+XXX} in
+		'')

I'm pretty sure there's been a nice trick for this, but I can't remember it at all now.

Quoted 23 lines
tg.sh +23 -0
+			# both GIT_PAGER & PAGER unset
+			TG_PAGER=''
+			;;
+		*)
+			TG_PAGER="$PAGER"
+			;;
+		esac
+		;;
+	*)
+		TG_PAGER="$GIT_PAGER"
+		;;
+	esac
+
+	[ -z "$TG_PAGER"  -o  "$TG_PAGER" = "cat" ]  && return 0
+
+
+	# now spawn pager
+	export LESS=${LESS:-FRSX}	# as in pager.c:pager_preexec()
+
+	_pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
+	_pager_fifo="$_pager_fifo_dir/0"
+	mkfifo -m 600 "$_pager_fifo"
+

tg.sh do_help() · line 299

Quoted 10 lines
tg.sh +10 -0
+	"$TG_PAGER" < "$_pager_fifo" &
+	exec > "$_pager_fifo"		# dup2(pager_fifo.in, 1)
+
+	# this is needed so e.g. `git diff` will still colorize it's output if
+	# requested in ~/.gitconfig with color.diff=auto
+	export GIT_PAGER_IN_USE=1
+
+	# atexit(close(1); wait pager)
+	trap "exec >&-; rm "$_pager_fifo"; rmdir "$_pager_fifo_dir"; wait" EXIT
+}

Frankly, I would have been just much happier if something like git pager--helper would be provided for external tools to use. Seeing how it gets reimplemented like this just pains me greatly.

Quoted 7 lines · Pierre Habouzit
Quoted 4 lines · Kirill Smelkov

isatty()

{
	tty -s 0<&$1
}

why not test -t 0 ? I'm not sure it's POSIX though.

It's SUS for many issues already it seems.

Signature · 3 lines
				Petr "Pasky" Baudis
The average, healthy, well-adjusted adult gets up at seven-thirty
in the morning feeling just terrible. -- Jean Kerr
#11 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

In reply to Kirill Smelkov

tg.sh do_help() · line 298

Quoted 3 lines · Kirill Smelkov

Martin, thanks for your review.

tg.sh +2 -0
+       # atexit(close(1); wait pager)
+       trap "exec >&-; rm "$_pager_fifo"; rmdir "$_pager_fifo_dir"; wait" EXIT

I think you need to escape the double quotes.

Bert

#12 lore raw

PATCH 2/2 tg-patch: fix pagination

In reply to Kirill Smelkov

Previously, when I was invoking tg patch the following used to happen:

1. .topmsg content was sent directly to _terminal_
2. for each file in the patch, its diff was generated with git diff and sent to *PAGER*
3. trailing 'tg: ...' was sent to terminal again

So the problem is that while tg patch >file works as expected, plain tg patch does not -- in pager there is only a part of the whole patch (first file diff) and header and trailer are ommitted.

I've finally decided to fix this inconvenience, and the way it works is like in git -- we just hook setup_pager function in commands which need to be paginated.

---
 tg-patch.sh |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
tg-patch.sh +3 -0
index a704375..dc699d2 100644
@@ -24,6 +24,9 @@ done
 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
 	die "not a TopGit-controlled branch"
 
+
+setup_pager
+
 git cat-file blob "$name:.topmsg"
 echo
 [ -n "$(git grep '^[-]--' "$name" -- ".topmsg")" ] || echo '---'
Signature · 1 line
1.6.1.48.ge9b8
#8 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

In reply to Petr Baudis
Quoted 40 lines · Thomas Rast
Quoted 14 lines · Kirill Smelkov
Quoted 6 lines · martin f krafft

I find this very confusing. Why not simply

  TG_PAGER="${GIT_PAGER:-}"
  TG_PAGER="${TG_PAGER:-$PAGER}"

?

I find it confusing too, but this is needed because they usually do something like this

    $ GIT_PAGER='' <some-git-command>

to force it to be pagerless.

[...]

Quoted 3 lines

So I think it would be better to preserve the same semantics for tg patch callers, though it's a pity that it's hard (maybe I'm wrong ?) to see whether an env var is unset.

Admittedly I haven't really studied your patch, but the ${} constructs can in fact tell empty from unset:

  $ EMPTY=
  $ unset UNDEFINED
  $ echo ${UNDEFINED-foo}
  foo
  $ echo ${UNDEFINED:-foo}
  foo
  $ echo ${EMPTY-foo}
  $ echo ${EMPTY:-foo}
  foo

'man bash' indeed says

When not performing substring expansion, bash tests for a parameter that is unset or null; omitting the colon results in a test only for a parameter that is unset.

Quoted 5 lines

So I suppose you could use

  ${GIT_PAGER-${PAGER-less}}

or similar.

Good eyes, thanks!

I'll rework it.

Quoted 5 lines · Bert Wesarg

Review

Quoted 3 lines · Kirill Smelkov

Martin, thanks for your review.

(quoted diff) +2 -0
+       # atexit(close(1); wait pager)
+       trap "exec >&-; rm "$_pager_fifo"; rmdir "$_pager_fifo_dir"; wait" EXIT

I think you need to escape the double quotes.

Good eyes -- corrected and thanks!

Quoted 6 lines · Petr Baudis
Quoted 5 lines · Kirill Smelkov
Quoted 1 line

From 2193b7c703c2d31c8739eec617b8c0e8c1d09b79 Mon Sep 17 00:00:00 2001

From: Kirill Smelkov <[email protected]>
Date: Tue, 6 Jan 2009 17:56:37 +0300
Subject: [PATCH (topgit) v2] Implement setup_pager just like in git

Quoted 8 lines
Quoted 6 lines

setup_pager() spawns a pager process and redirect the rest of our output to it.

This will be needed to fix tg patch output in the next commit.

But you never use it...?

What do you mean?

It is used in the next patch as posted in original series:

http://marc.info/?l=git&m=123125495000600&w=2

For completeness, I'll include both patches in this email.

Quoted 28 lines
Quoted 28 lines
---
 tg.sh |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 54 insertions(+), 0 deletions(-)
tg.sh +16 -0
index 8c23d26..bf9cf5c 100644
@@ -243,6 +243,60 @@ do_help()
 	fi
 }
 
+## Pager stuff
+
+# isatty FD
+isatty()
+{
+	tty -s 0<&$1
+}
+
+# setup_pager
+# Spawn pager process and redirect the rest of our output to it
+setup_pager()
+{
+	isatty 1 || return 0
+
+	# TG_PAGER = GIT_PAGER | PAGER
+	# (but differentiate between GIT_PAGER='' and unset variables)
Quoted 8 lines

tg.sh do_help() · line 266

Quoted 5 lines
tg.sh +5 -0
+	# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-03/0792.html
+	case ${GIT_PAGER+XXX} in
+	'')
+		case ${PAGER+XXX} in
+		'')

I'm pretty sure there's been a nice trick for this, but I can't remember it at all now.

Already corrected to ${GIT_PAGER-${PAGER-less}}, thanks to Thomas.

Quoted 29 lines
Quoted 29 lines
tg.sh +29 -0
+			# both GIT_PAGER & PAGER unset
+			TG_PAGER=''
+			;;
+		*)
+			TG_PAGER="$PAGER"
+			;;
+		esac
+		;;
+	*)
+		TG_PAGER="$GIT_PAGER"
+		;;
+	esac
+
+	[ -z "$TG_PAGER"  -o  "$TG_PAGER" = "cat" ]  && return 0
+
+
+	# now spawn pager
+	export LESS=${LESS:-FRSX}	# as in pager.c:pager_preexec()
+
+	_pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
+	_pager_fifo="$_pager_fifo_dir/0"
+	mkfifo -m 600 "$_pager_fifo"
+
+	"$TG_PAGER" < "$_pager_fifo" &
+	exec > "$_pager_fifo"		# dup2(pager_fifo.in, 1)
+
+	# this is needed so e.g. `git diff` will still colorize it's output if
+	# requested in ~/.gitconfig with color.diff=auto
+	export GIT_PAGER_IN_USE=1
Quoted 8 lines

tg.sh do_help() · line 299

Quoted 4 lines
tg.sh +4 -0
+
+	# atexit(close(1); wait pager)
+	trap "exec >&-; rm "$_pager_fifo"; rmdir "$_pager_fifo_dir"; wait" EXIT
+}

Frankly, I would have been just much happier if something like git pager--helper would be provided for external tools to use. Seeing how it gets reimplemented like this just pains me greatly.

After we settle on implementation, would it make sense to include this setup_pager into git-sh-setup?

I propose we include this stuff into tg.sh first, so that topgit would work correctly with previous versions of git.

Quoted 10 lines · Pierre Habouzit
Quoted 7 lines · Kirill Smelkov
tg.sh +0 -0
 > isatty()
 > {
Quoted 2 lines
	tty -s 0<&$1
}

why not test -t 0 ? I'm not sure it's POSIX though.

It's SUS for many issues already it seems.

Pierre and Petr - thanks for the info. Yes, test -t $fd looks better.

Here is improved patch:

Changes since v1:

 o Simplify TG_PAGER setup  (thanks to Thomas Rast)
 o Properly escape "        (thanks to Bert Wesarg)
 o Simpler isatty           (thanks to Pierre Habouzit & Petr Baudis)

(interdiff)

tg.sh +5 -21
index bf9cf5c..b64fc3a 100644
@@ -248,7 +248,7 @@ do_help()
 # isatty FD
 isatty()
 {
-	tty -s 0<&$1
+	test -t $1
 }
 
 # setup_pager
@@ -257,25 +257,9 @@ setup_pager()
 {
 	isatty 1 || return 0
 
-	# TG_PAGER = GIT_PAGER | PAGER
-	# (but differentiate between GIT_PAGER='' and unset variables)
-	# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-03/0792.html
-	case ${GIT_PAGER+XXX} in
-	'')
-		case ${PAGER+XXX} in
-		'')
-			# both GIT_PAGER & PAGER unset
-			TG_PAGER=''
-			;;
-		*)
-			TG_PAGER="$PAGER"
-			;;
-		esac
-		;;
-	*)
-		TG_PAGER="$GIT_PAGER"
-		;;
-	esac
+	# TG_PAGER = GIT_PAGER | PAGER | less
+	# NOTE: GIT_PAGER='' is significant
+	TG_PAGER=${GIT_PAGER-${PAGER-less}}
 
 	[ -z "$TG_PAGER"  -o  "$TG_PAGER" = "cat" ]  && return 0
 
@@ -295,7 +279,7 @@ setup_pager()
 	export GIT_PAGER_IN_USE=1
 
 	# atexit(close(1); wait pager)
-	trap "exec >&-; rm "$_pager_fifo"; rmdir "$_pager_fifo_dir"; wait" EXIT
+	trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
 }
 
 ## Startup

From: Kirill Smelkov <[email protected]>
To: Petr Baudis <[email protected]>
Cc: Git Mailing List <[email protected]>
Bcc: Kirill Smelkov <[email protected]>
Subject: Implement setup_pager just like in git

setup_pager() spawns a pager process and redirect the rest of our output to it.

This will be needed to fix tg patch output in the next commit.

---
 tg.sh |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
tg.sh +38 -0
index 8c23d26..b64fc3a 100644
@@ -243,6 +243,44 @@ do_help()
 	fi
 }
 
+## Pager stuff
+
+# isatty FD
+isatty()
+{
+	test -t $1
+}
+
+# setup_pager
+# Spawn pager process and redirect the rest of our output to it
+setup_pager()
+{
+	isatty 1 || return 0
+
+	# TG_PAGER = GIT_PAGER | PAGER | less
+	# NOTE: GIT_PAGER='' is significant
+	TG_PAGER=${GIT_PAGER-${PAGER-less}}
+
+	[ -z "$TG_PAGER"  -o  "$TG_PAGER" = "cat" ]  && return 0
+
+
+	# now spawn pager
+	export LESS=${LESS:-FRSX}	# as in pager.c:pager_preexec()
+
+	_pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
+	_pager_fifo="$_pager_fifo_dir/0"
+	mkfifo -m 600 "$_pager_fifo"
+
+	"$TG_PAGER" < "$_pager_fifo" &
+	exec > "$_pager_fifo"		# dup2(pager_fifo.in, 1)
+
+	# this is needed so e.g. `git diff` will still colorize it's output if
+	# requested in ~/.gitconfig with color.diff=auto
+	export GIT_PAGER_IN_USE=1
+
+	# atexit(close(1); wait pager)
+	trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
+}
 
 ## Startup
 
Signature · 4 lines
tg: (8c77c34..) t/setup-pager (depends on: master)


Second patch which uses setup_pager:
Quoted 1 line

From 1b723ebf740c58bc25ac97eff0a31b07373d8d1e Mon Sep 17 00:00:00 2001

From: Kirill Smelkov <[email protected]>
Date: Tue, 6 Jan 2009 18:03:21 +0300
Subject: [TopGit PATCH] tg-patch: fix pagination

Previously, when I was invoking tg patch the following used to happen:

1. .topmsg content was sent directly to _terminal_
2. for each file in the patch, its diff was generated with git diff and sent to *PAGER*
3. trailing 'tg: ...' was sent to terminal again

So the problem is that while tg patch >file works as expected, plain tg patch does not -- in pager there is only a part of the whole patch (first file diff) and header and trailer are ommitted.

I've finally decided to fix this inconvenience, and the way it works is like in git -- we just hook setup_pager function in commands which need to be paginated.

---
 tg-patch.sh |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
tg-patch.sh +3 -0
index a704375..dc699d2 100644
@@ -24,6 +24,9 @@ done
 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
 	die "not a TopGit-controlled branch"
 
+
+setup_pager
+
 git cat-file blob "$name:.topmsg"
 echo
 [ -n "$(git grep '^[-]--' "$name" -- ".topmsg")" ] || echo '---'
Signature · 5 lines
1.6.1.48.ge9b8


Thanks,
Kirill
#9 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

also sprach Kirill Smelkov <[email protected]> [2009.01.08.1100 +1300]:

Quoted 9 lines
Quoted 5 lines

So I suppose you could use

  ${GIT_PAGER-${PAGER-less}}

or similar.

Good eyes, thanks!

I'll rework it.

I am not 100% on this, but I think nested {}'s are a bashism.

Quoted 8 lines · Bert Wesarg
Quoted 5 lines · Kirill Smelkov

Review

Quoted 3 lines

Martin, thanks for your review.

(quoted diff) +2 -0
+       # atexit(close(1); wait pager)
+       trap "exec >&-; rm "$_pager_fifo"; rmdir "$_pager_fifo_dir"; wait" EXIT

I think you need to escape the double quotes.

Good eyes -- corrected and thanks!

You could also just use single quotes inside the double quotes.

Signature · 11 lines
martin | http://madduck.net/ | http://two.sentenc.es/
 
"he gave me his card
 he said, 'call me if they die'
 i shook his hand and said goodbye
 ran out to the street
 when a bowling ball came down the road
 and knocked me off my feet"
                                                        -- bob dylan
 
spamtraps: [email protected]
#10 lore raw

Re: [PATCH (topgit) 1/2] Implement setup_pager just like in git

Quoted 5 lines · martin f krafft

also sprach Kirill Smelkov <[email protected]> [2009.01.08.1100 +1300]:

Quoted 4 lines
Quoted 4 lines

So I suppose you could use

  ${GIT_PAGER-${PAGER-less}}
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

or similar.

Good eyes, thanks!

I'll rework it.

I am not 100% on this, but I think nested {}'s are a bashism.

It seems to be ok:

kirr@roro3:~$ dash 
$ unset GIT_PAGER
$ unset PAGER
$ echo ${GIT_PAGER-${PAGER-less}}
less
$ PAGER=more
$ echo ${GIT_PAGER-${PAGER-less}}
more
$ GIT_PAGER=''
$ echo ${GIT_PAGER-${PAGER-less}}
$ GIT_PAGER=/bin/cat
$ echo ${GIT_PAGER-${PAGER-less}}
/bin/cat
Quoted 10 lines
Quoted 8 lines · Bert Wesarg
Quoted 5 lines · Kirill Smelkov

Review

Quoted 3 lines

Martin, thanks for your review.

(quoted diff) +2 -0
+       # atexit(close(1); wait pager)
+       trap "exec >&-; rm "$_pager_fifo"; rmdir "$_pager_fifo_dir"; wait" EXIT

I think you need to escape the double quotes.

Good eyes -- corrected and thanks!

You could also just use single quotes inside the double quotes.

Thanks for the tip - I'll keep it in mind. Or is it the preferred way?

Thanks,
Kirill

#13 lore raw
In reply to Kirill Smelkov
Quoted 7 lines · Kirill Smelkov

Kirill Smelkov (2):
Implement setup_pager just like in git
tg-patch: fix pagination

 tg-patch.sh |    3 +++
 tg.sh       |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)

Petr, Martin,

What's the state of this patch series?

It fixes tg patch, and it seems we addressed all uglyness in setup_pager, so is there any other reason not to merge this?

Thanks,
Kirill