Skip to contentSkip to conversation

Improve help output

TopGit In review martin f. krafft 3 participants 16 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

The following patches improve the help output by TopGit. They are not all related, but they conflict.

I would just push them to the master branch, but I don't want to risk my chances of Petr getting angry with his new release assistant...

#1 lore raw

PATCH 1/1 Check for help invocation before setup

The user ought to be able to call tg help from anywhere in the filesystem, not just Git repositories, so the help parsing has to happen before the calls to git git binary.

Debian bug: #501982

---
 tg.sh |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)
tg.sh +14 -3
index 2961106..40c4ab7 100644
@@ -235,6 +235,20 @@ do_help()
 	fi
 }
 
+## Startup
+
+args_saved="$@"
+while [ -n "$1" ]; do
+	case "$1" in
+	help|--help|-h)
+		shift
+		do_help "$1"
+		exit 1;;
+	esac
+	shift
+done
+set -- $args_saved
+unset args_saved
 
 ## Initial setup
 
@@ -268,9 +282,6 @@ cmd="$1"
 shift
 
 case "$cmd" in
-help|--help|-h)
-	do_help "$1"
-	exit 1;;
 --hooks-path)
 	# Internal command
 	echo "@hooksdir@";;
Signature · 1 line
1.6.0.2
#2 lore raw

PATCH 1/1 Change tg help exit code to 0

Printing --help is not an error, but a successful operation, if the help output could be printed.

---
 tg.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tg.sh +1 -1
index 40c4ab7..ea22544 100644
@@ -243,7 +243,7 @@ while [ -n "$1" ]; do
 	help|--help|-h)
 		shift
 		do_help "$1"
-		exit 1;;
+		exit 0;;
 	esac
 	shift
 done
Signature · 1 line
1.6.0.2
#3 lore raw

PATCH 1/1 Check for cmddir earlier

Without cmddir, tg is basically useless, even do_help() needs it, so check it first and die hard if not found

---
 tg.sh |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
tg.sh +3 -3
index ea22544..f3d1323 100644
@@ -237,6 +237,9 @@ do_help()
 
 ## Startup
 
+[ -d "@cmddir@" ] ||
+	die "No command directory: '@cmddir@'"
+
 args_saved="$@"
 while [ -n "$1" ]; do
 	case "$1" in
@@ -263,9 +266,6 @@ tg="tg"
 setup_ours
 setup_hook "pre-commit"
 
-[ -d "@cmddir@" ] ||
-	die "No command directory: '@cmddir@'"
-
 ## Dispatch
 
 # We were sourced from another script for our utility functions;
Signature · 1 line
1.6.0.2
#4 lore raw

PATCH 1/1 Print help output when no command is given

As much as I love your message (although you really ought not be throwing around ducks!), this is more user-friendly.

---
 tg.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tg.sh +1 -1
index f3d1323..e0d62e7 100644
@@ -278,7 +278,7 @@ if [ "$1" = "-r" ]; then
 fi
 
 cmd="$1"
-[ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
+[ -n "$cmd" ] || { do_help; exit 1; }
 shift
 
 case "$cmd" in
Signature · 1 line
1.6.0.2
#5 lore raw

Re: [TopGit PATCH] Check for help invocation before setup

In reply to martin f. krafft
Quoted 14 lines · martin f. krafft
tg.sh +6 -0
index 2961106..40c4ab7 100644
@@ -235,6 +235,20 @@ do_help()
 	fi
 }
 
+## Startup
+
+args_saved="$@"
+while [ -n "$1" ]; do
+	case "$1" in
+	help|--help|-h)

tg.sh do_help() · line 253

Quoted 10 lines
tg.sh +8 -0
+		shift
+		do_help "$1"
+		exit 1;;
+	esac
+	shift
+done
+set -- $args_saved
+unset args_saved
 
 ## Initial setup

Huh, why do you actually need $args_saved at all? :-) This is bound to do horrible things with space-containing arguments etc., I think. You don't need to do the outer shift and then drop $args_saved altogether, no?

Signature · 3 lines
				Petr "Pasky" Baudis
People who take cold baths never have rheumatism, but they have
cold baths.
#6 lore raw

Re: [TopGit PATCH] Check for help invocation before setup

also sprach Petr Baudis <[email protected]> [2008.11.19.1924 +0100]:

Quoted 13 lines
Quoted 13 lines
tg.sh +12 -0
+args_saved="$@"
+while [ -n "$1" ]; do
+	case "$1" in
+	help|--help|-h)
+		shift
+		do_help "$1"
+		exit 1;;
+	esac
+	shift
+done
+set -- $args_saved
+unset args_saved
Quoted 6 lines
Quoted 1 line

## Initial setup

Huh, why do you actually need $args_saved at all? :-) This is bound to do horrible things with space-containing arguments etc., I think. You don't need to do the outer shift and then drop $args_saved altogether, no?

I figured I need to restore $@ for others to consume, e.g. when you check for -r later.

The challenge is to identify help|--help|-h anywhere on the command line. Thus, you need to iterate, or do some weird matching against $*.

When you iterate in a for loop, it's not easy to get at the next argument, except if you use a state machine. I wanted to avoid that.

But you are right, I am wreaking havoc with space-containing arguments. I will have to go back and rework this.

I take it you agree with the general principle though?

Signature · 9 lines
 .''`.   martin f. krafft <[email protected]>
: :'  :  proud Debian developer, author, administrator, and user
`. `'`   http://people.debian.org/~madduck - http://debiansystem.info
  `-  Debian - when you have better things to do than fixing systems
 
"it is easier to be a lover than a husband for the simple reason
 that it is more difficult to be witty every day
 than to say pretty things from time to time."
                                                   -- honoré de balzac
#7 lore raw

Re: [TopGit PATCH] Check for help invocation before setup

Quoted 24 lines · martin f krafft

also sprach Petr Baudis <[email protected]> [2008.11.19.1924 +0100]:

Quoted 13 lines
Quoted 13 lines
tg.sh +12 -0
+args_saved="$@"
+while [ -n "$1" ]; do
+	case "$1" in
+	help|--help|-h)
+		shift
+		do_help "$1"
+		exit 1;;
+	esac
+	shift
+done
+set -- $args_saved
+unset args_saved
Quoted 6 lines
Quoted 1 line

## Initial setup

Huh, why do you actually need $args_saved at all? :-) This is bound to do horrible things with space-containing arguments etc., I think. You don't need to do the outer shift and then drop $args_saved altogether, no?

I figured I need to restore $@ for others to consume, e.g. when you check for -r later.

Quoted 6 lines

The challenge is to identify help|--help|-h anywhere on the command line. Thus, you need to iterate, or do some weird matching against $*.

When you iterate in a for loop, it's not easy to get at the next argument, except if you use a state machine. I wanted to avoid that.

Oh, oops - I missed the loop, I should get some lunch. ;-) Thus, something like this?

	check_help() { while [ -n "$1" ]; do ...; shift; done }
	check_help "$@"
Signature · 3 lines
				Petr "Pasky" Baudis
People who take cold baths never have rheumatism, but they have
cold baths.
#8 lore raw

Re: [TopGit PATCH] Check for help invocation before setup

also sprach Petr Baudis <[email protected]> [2008.11.19.1939 +0100]:

Quoted 5 lines

Oh, oops - I missed the loop, I should get some lunch. ;-) Thus, something like this?

	check_help() { while [ -n "$1" ]; do ...; shift; done }
	check_help "$@"

Why didn't I think of that???

I just sent an updated patch without threading, sorry. tg-mail just fires without confirmation... :/

Signature · 7 lines
 .''`.   martin f. krafft <[email protected]>
: :'  :  proud Debian developer, author, administrator, and user
`. `'`   http://people.debian.org/~madduck - http://debiansystem.info
  `-  Debian - when you have better things to do than fixing systems
 
"the pure and simple truth is rarely pure and never simple."
                                                      -- oscar wilde
#9 lore raw

Re: [TopGit PATCH] Check for help invocation before setup

In reply to martin f. krafft

Hi,

Quoted 5 lines · martin f. krafft

The user ought to be able to call tg help from anywhere in the filesystem, not just Git repositories, so the help parsing has to happen before the calls to git git binary.

Debian bug: #501982

I really second this. Plus, I think its crucial to check that we are not inside the .git directory before setting up topgit. Because git rev-parse --git-dir only works in the top .git dir, not deeper (i.e. inside .git/refs) and will always return ".".

	$(git rev-parse --is-inside-work-tree) ||
		die "Not in a git working directory"

Bert

#10 lore raw

TopGit: ensure worktree (was: [TopGit PATCH] Check for help invocation before setup)

I think you wanted to CC Petr, not Junio. It's also useful to CC Debian bugs if you know of them. :)

also sprach Bert Wesarg <[email protected]> [2008.11.21.1019 +0100]:

Quoted 7 lines

I really second this. Plus, I think its crucial to check that we are not inside the .git directory before setting up topgit. Because git rev-parse --git-dir only works in the top .git dir, not deeper (i.e. inside .git/refs) and will always return ".".

	$(git rev-parse --is-inside-work-tree) ||
		die "Not in a git working directory"

Thanks, noted. I am still working on this patch, since evaluating $argv/checking for --help at this early stage basically requires a rewrite of the CLI parsing, ideally using POSIX getopt or something similar, which takes much of the load away.

Anyway, Patch for your proposal forthcoming.

Signature · 6 lines
 .''`.   martin f. krafft <[email protected]>
: :'  :  proud Debian developer, author, administrator, and user
`. `'`   http://people.debian.org/~madduck - http://debiansystem.info
  `-  Debian - when you have better things to do than fixing systems
 
(a)bort, (r)etry, (p)retend this never happened
#11 lore raw

PATCH 1/1 Ensure we are inside a Git worktree

Bert Wesarg suggests to check that we are not inside the .git directory before setting up topgit. Because git rev-parse --git-dir only works in the top .git dir, not deeper (i.e. inside .git/refs) and will always return "."

This patch thus causes topgit to die early on when it's called from outside of a Git repository, or from underneath .git and outputs error messages accordingly.

This temporarily makes it even more impossible to call tg --help, but I expect to have that fixed soon too.

---
 tg.sh |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
tg.sh +16 -0
index 8c23d26..f8c8de4 100644
@@ -17,6 +17,20 @@ die()
 	exit 1
 }
 
+# Make sure we are in the worktree, not under .git; die otherwise
+ensure_git_repo_or_die()
+{
+	local is_inside_repo is_inside_git_dir
+	is_inside_repo=1
+	is_inside_git_dir=$(git rev-parse --is-inside-git-dir 2>/dev/null) ||
+		is_inside_repo=0
+
+	case "$is_inside_repo/$is_inside_git_dir" in
+	0*) die "Cannot run outside of a Git repository.";;
+	1/true) die "Cannot run from inside \`.git\` hierarchy, please switch to work-tree.";;
+	esac
+}
+
 # setup_hook NAME
 setup_hook()
 {
@@ -249,6 +263,8 @@ do_help()
 [ -d "@cmddir@" ] ||
 	die "No command directory: '@cmddir@'"
 
+ensure_git_repo_or_die
+
 ## Initial setup
 
 set -e
Signature · 1 line
tg: (2ea19b6..) fixes/ensure-worktree (depends on: upstream)
#12 lore raw

Re: TopGit: ensure worktree (was: [TopGit PATCH] Check for help invocation before setup)

In reply to martin f krafft
Quoted 2 lines · martin f krafft

I think you wanted to CC Petr, not Junio. It's also useful to CC Debian bugs if you know of them. :)

I CC'ed Junio, because I think this git rev-parse --git-dir is a bug. For example you can try starting git gui inside a .git dir.

Quoted 10 lines

also sprach Bert Wesarg <[email protected]> [2008.11.21.1019 +0100]:

Quoted 7 lines

I really second this. Plus, I think its crucial to check that we are not inside the .git directory before setting up topgit. Because git rev-parse --git-dir only works in the top .git dir, not deeper (i.e. inside .git/refs) and will always return ".".

$(git rev-parse --is-inside-work-tree) ||
die "Not in a git working directory"

Quoted 6 lines

Thanks, noted. I am still working on this patch, since evaluating $argv/checking for --help at this early stage basically requires a rewrite of the CLI parsing, ideally using POSIX getopt or something similar, which takes much of the load away.

Anyway, Patch for your proposal forthcoming.

A patch would depent on your --help patch, Because help should work everywhere. So either I wait for you or do do it.

Bert

Quoted 18 lines

--

 .''`.   martin f. krafft <[email protected]>
: :'  :  proud Debian developer, author, administrator, and user
`. `'`   http://people.debian.org/~madduck - http://debiansystem.info

`- Debian - when you have better things to do than fixing systems

(a)bort, (r)etry, (p)retend this never happened

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkkmpDAACgkQIgvIgzMMSnUSKgCghXE+J9KegZ03tetnM3TIcyeM zn0AoJmYVzh/Kjxk7NFEpQw7USR0daXy
=xl31
-----END PGP SIGNATURE-----

#13 lore raw

Re: TopGit: ensure worktree (was: [TopGit PATCH] Check for help invocation before setup)

also sprach Bert Wesarg <[email protected]> [2008.11.21.1315 +0100]:

Quoted 2 lines

I CC'ed Junio, because I think this git rev-parse --git-dir is a bug. For example you can try starting git gui inside a .git dir.

You might want to let him know of your intention. :)

Quoted 2 lines

A patch would depent on your --help patch, Because help should work everywhere. So either I wait for you or do do it.

No, not depend, but the patch makes the --help situation slightly worse, for now you cannot call --help inside .git anymore either. This will force us to fix the --help situation quicker.

I've run out of time, for a while at least. If you want to pick up the pieces and convert argument parsing to POSIX getopt, or maybe even better, git-rev-parse --parseopt, then please go for it. You can find my current TopGit branch at

http://git.debian.org/?p=collab-maint/topgit.git;a=shortlog;h=refs/heads/fixes/independent-help
git://git.debian.org/git/collab-maint/topgit.git, branch fixes/independent-help

If you use Debian: debcheckout topgit (requires devscripts 2.10.40)

Thanks,

Signature · 7 lines
 .''`.   martin f. krafft <[email protected]>
: :'  :  proud Debian developer, author, administrator, and user
`. `'`   http://people.debian.org/~madduck - http://debiansystem.info
  `-  Debian - when you have better things to do than fixing systems
 
"heuristic is computer science jargon for 'doesn't actually work.'"
                                                     -- charlie reiman
#14 lore raw

Re: TopGit: ensure worktree (was: [TopGit PATCH] Check for help invocation before setup)

In reply to Bert Wesarg
Quoted 5 lines · Bert Wesarg
Quoted 2 lines · martin f krafft

I think you wanted to CC Petr, not Junio. It's also useful to CC Debian bugs if you know of them. :)

I CC'ed Junio, because I think this git rev-parse --git-dir is a bug. For example you can try starting git gui inside a .git dir.

Oh, I send a patch only to petr, but forgot to cc the list ;-)

But your patch should be fine, thanks.

Bert

#15 lore raw

Re: TopGit: ensure worktree (was: [TopGit PATCH] Check for help invocation before setup)

also sprach Bert Wesarg <[email protected]> [2008.11.21.1318 +0100]:

Quoted 1 line

Oh, I send a patch only to petr, but forgot to cc the list ;-)

Maybe you can try to remember to CC me on topgit stuff in the future too, since I am helping Petr out a bit as his release apprentice. :)

Signature · 6 lines
 .''`.   martin f. krafft <[email protected]>
: :'  :  proud Debian developer, author, administrator, and user
`. `'`   http://people.debian.org/~madduck - http://debiansystem.info
  `-  Debian - when you have better things to do than fixing systems
 
i stopped fighting my inner demons.. we're all on the same side now.