SYNOPSIS
========
-**notmuch** **git** [-h] [-C *repo*] [-p *prefix*] [-v] [-l *log level*] *subcommand*
+**notmuch** **git** [-h] [-N] [-C *repo*] [-p *prefix*] [-v] [-l *log level*] *subcommand*
+
+**nmbug** [-h] [-C *repo*] [-p *prefix*] [-v] [-l *log level*] *subcommand*
DESCRIPTION
===========
show help message and exit
+.. option:: -N, --nmbug
+
+ Set defaults for :option:`--tag-prefix` and :option:`--git-dir` suitable for the
+ :any:`notmuch` bug tracker
+
.. option:: -C <repo>, --git-dir <repo>
Operate on git repository *repo*. See :ref:`repo_location` for
defaults.
-.. option:: -p <prefix>, --tag-prefix <prefix>
+.. option:: -p <prefix>, --tag-prefix <prefix>
Operate only on tags with prefix *prefix*. See :ref:`prefix_val` for
defaults.
- Environment variable :envvar:`NOTMUCH_GIT_DIR`.
+- If invoked as `nmbug` or with the :option:`--nmbug` option,
+ :code:`$HOME/.nmbug`; otherwise
+ :code:`$XDG_DATA_HOME/notmuch/$NOTMUCH_PROFILE/git`.
+
.. _prefix_val:
PREFIX VALUE
- Environment variable :envvar:`NOTMUCH_GIT_PREFIX`.
+- If invoked as `nmbug` or with the :option:`--nmbug` option,
+ :code:`notmuch::`, otherwise the empty string.
+
ENVIRONMENT
===========
+Variable :envvar:`NOTMUCH_PROFILE` influences :ref:`repo_location`.
+If it is unset, 'default' is assumed.
+
.. envvar:: NOTMUCH_GIT_DIR
Default location of git repository. Overriden by :option:`--git-dir`.
This wraps 'git init' with a few extra steps to support subsequent
status and commit commands.
"""
+ from pathlib import Path
+ parent = Path(NOTMUCH_GIT_DIR).parent
+ try:
+ _os.makedirs(parent)
+ except FileExistsError:
+ pass
+
_spawn(args=['git', '--git-dir', NOTMUCH_GIT_DIR, 'init',
'--initial-branch=master', '--quiet', '--bare'], wait=True)
_git(args=['config', 'core.logallrefupdates', 'true'], wait=True)
stdout=_subprocess.PIPE, wait=True)
if status != 0:
_LOG.error("failed to run notmuch config")
- sys.exit(1)
+ _sys.exit(1)
return stdout.rstrip()
+# based on BaseDirectory.save_data_path from pyxdg (LGPL2+)
+def xdg_data_path(profile):
+ resource = _os.path.join('notmuch',profile,'git')
+ assert not resource.startswith('/')
+ _home = _os.path.expanduser('~')
+ xdg_data_home = _os.environ.get('XDG_DATA_HOME') or \
+ _os.path.join(_home, '.local', 'share')
+ path = _os.path.join(xdg_data_home, resource)
+ return path
+
if __name__ == '__main__':
import argparse
help='Git repository to operate on.')
parser.add_argument(
'-p', '--tag-prefix', metavar='PREFIX',
- default = _os.getenv('NOTMUCH_GIT_PREFIX', 'notmuch::'),
+ default = None,
help='Prefix of tags to operate on.')
+ parser.add_argument(
+ '-N', '--nmbug', action='store_true',
+ help='Set defaults for --tag-prefix and --git-dir for the notmuch bug tracker')
parser.add_argument(
'-l', '--log-level',
choices=['critical', 'error', 'warning', 'info', 'debug'],
args = parser.parse_args()
+ nmbug_mode = False
+ notmuch_profile = _os.getenv('NOTMUCH_PROFILE','default')
+
+ if args.nmbug or _os.path.basename(__file__) == 'nmbug':
+ nmbug_mode = True
+
if args.git_dir:
NOTMUCH_GIT_DIR = args.git_dir
else:
- NOTMUCH_GIT_DIR = _os.path.expanduser(
- _os.getenv('NOTMUCH_GIT_DIR', _os.path.join('~', '.nmbug')))
- _NOTMUCH_GIT_DIR = _os.path.join(NOTMUCH_GIT_DIR, '.git')
- if _os.path.isdir(_NOTMUCH_GIT_DIR):
- NOTMUCH_GIT_DIR = _NOTMUCH_GIT_DIR
+ if nmbug_mode:
+ default = _os.path.join('~', '.nmbug')
+ else:
+ default = xdg_data_path(notmuch_profile)
+
+ NOTMUCH_GIT_DIR = _os.path.expanduser(_os.getenv('NOTMUCH_GIT_DIR', default))
+
+ _NOTMUCH_GIT_DIR = _os.path.join(NOTMUCH_GIT_DIR, '.git')
+ if _os.path.isdir(_NOTMUCH_GIT_DIR):
+ NOTMUCH_GIT_DIR = _NOTMUCH_GIT_DIR
+
+ if args.tag_prefix:
+ TAG_PREFIX = args.tag_prefix
+ else:
+ if nmbug_mode:
+ prefix = 'notmuch::'
+ else:
+ prefix = ''
+
+ TAG_PREFIX = _os.getenv('NOTMUCH_GIT_PREFIX', prefix)
- TAG_PREFIX = args.tag_prefix
_ENCODED_TAG_PREFIX = _hex_quote(TAG_PREFIX, safe='+@=,') # quote ':'
if args.log_level:
if _notmuch_config_get('built_with.sexp_queries') != 'true':
_LOG.error("notmuch git needs sexp query support")
- sys.exit(1)
+ _sys.exit(1)
if not getattr(args, 'func', None):
parser.print_usage()
_sys.exit(1)
+ # The following two lines are used by the test suite.
+ _LOG.debug('prefix = {:s}'.format(TAG_PREFIX))
+ _LOG.debug('repository = {:s}'.format(NOTMUCH_GIT_DIR))
+
if args.func == help:
arg_names = ['command']
else:
test_expect_success "notmuch git -p '' -C tags.git clone remote.git"
test_begin_subtest "commit"
-notmuch git -C tags.git -p '' commit
+notmuch git -C tags.git commit
git -C tags.git ls-tree -r --name-only HEAD | xargs dirname | sort -u | sed s,tags/,id:, > OUTPUT
notmuch search --output=messages '*' | sort > EXPECTED
test_expect_equal_file_nonempty EXPECTED OUTPUT
test_begin_subtest "commit, with quoted tag"
-notmuch git -C clone2.git -p '' clone tags.git
+notmuch git -C clone2.git clone tags.git
git -C clone2.git ls-tree -r --name-only HEAD | grep /inbox > BEFORE
notmuch tag '+"quoted tag"' '*'
-notmuch git -C clone2.git -p '' commit
+notmuch git -C clone2.git commit
notmuch tag '-"quoted tag"' '*'
git -C clone2.git ls-tree -r --name-only HEAD | grep /inbox > AFTER
test_expect_equal_file_nonempty BEFORE AFTER
test_begin_subtest "commit (incremental)"
notmuch tag +test id:20091117190054.GU3165@dottiness.seas.harvard.edu
-notmuch git -C tags.git -p '' commit
+notmuch git -C tags.git commit
git -C tags.git ls-tree -r --name-only HEAD |
grep 20091117190054 | sort > OUTPUT
echo "--------------------------------------------------" >> OUTPUT
notmuch tag -test id:20091117190054.GU3165@dottiness.seas.harvard.edu
-notmuch git -C tags.git -p '' commit
+notmuch git -C tags.git commit
git -C tags.git ls-tree -r --name-only HEAD |
grep 20091117190054 | sort >> OUTPUT
cat <<EOF > EXPECTED
grep 20091117190054 | sort > OUTPUT
echo "--------------------------------------------------" >> OUTPUT
notmuch tag -test::one id:20091117190054.GU3165@dottiness.seas.harvard.edu
-notmuch git -C tags.git -p '' commit
+notmuch git -C tags.git commit
git -C tags.git ls-tree -r --name-only HEAD |
grep 20091117190054 | sort >> OUTPUT
cat <<EOF > EXPECTED
test_begin_subtest "checkout"
notmuch dump > BEFORE
notmuch tag -inbox '*'
-notmuch git -C tags.git -p '' checkout
+notmuch git -C tags.git checkout
notmuch dump > AFTER
test_expect_equal_file_nonempty BEFORE AFTER
test_begin_subtest "archive"
-notmuch git -C tags.git -p '' archive | tar tf - | \
+notmuch git -C tags.git archive | tar tf - | \
grep 20091117190054.GU3165@dottiness.seas.harvard.edu | sort > OUTPUT
cat <<EOF > EXPECTED
tags/20091117190054.GU3165@dottiness.seas.harvard.edu/
tags/20091117190054.GU3165@dottiness.seas.harvard.edu/signed
tags/20091117190054.GU3165@dottiness.seas.harvard.edu/unread
EOF
-notmuch git -C tags.git -p '' checkout
+notmuch git -C tags.git checkout
test_expect_equal_file EXPECTED OUTPUT
test_begin_subtest "status"
notmuch tag +test id:20091117190054.GU3165@dottiness.seas.harvard.edu
-notmuch git -C tags.git -p '' status > OUTPUT
+notmuch git -C tags.git status > OUTPUT
cat <<EOF > EXPECTED
A 20091117190054.GU3165@dottiness.seas.harvard.edu test
EOF
-notmuch git -C tags.git -p '' checkout
+notmuch git -C tags.git checkout
test_expect_equal_file EXPECTED OUTPUT
test_begin_subtest "fetch"
notmuch tag +test2 id:20091117190054.GU3165@dottiness.seas.harvard.edu
-notmuch git -C remote.git -p '' commit
+notmuch git -C remote.git commit
notmuch tag -test2 id:20091117190054.GU3165@dottiness.seas.harvard.edu
-notmuch git -C tags.git -p '' fetch
-notmuch git -C tags.git -p '' status > OUTPUT
+notmuch git -C tags.git fetch
+notmuch git -C tags.git status > OUTPUT
cat <<EOF > EXPECTED
a 20091117190054.GU3165@dottiness.seas.harvard.edu test2
EOF
-notmuch git -C tags.git -p '' checkout
+notmuch git -C tags.git checkout
test_expect_equal_file EXPECTED OUTPUT
test_begin_subtest "merge"
-notmuch git -C tags.git -p '' merge
+notmuch git -C tags.git merge
notmuch dump id:20091117190054.GU3165@dottiness.seas.harvard.edu | grep -v '^#' > OUTPUT
cat <<EOF > EXPECTED
+inbox +signed +test2 +unread -- id:20091117190054.GU3165@dottiness.seas.harvard.edu
test_begin_subtest "push"
notmuch tag +test3 id:20091117190054.GU3165@dottiness.seas.harvard.edu
-notmuch git -C tags.git -p '' commit
+notmuch git -C tags.git commit
notmuch tag -test3 id:20091117190054.GU3165@dottiness.seas.harvard.edu
-notmuch git -C tags.git -p '' push
-notmuch git -C remote.git -p '' checkout
+notmuch git -C tags.git push
+notmuch git -C remote.git checkout
notmuch dump id:20091117190054.GU3165@dottiness.seas.harvard.edu | grep -v '^#' > OUTPUT
cat <<EOF > EXPECTED
+inbox +signed +test2 +test3 +unread -- id:20091117190054.GU3165@dottiness.seas.harvard.edu
EOF
test_expect_equal_file EXPECTED OUTPUT
+test_begin_subtest "--nmbug argument sets defaults"
+notmuch git -ldebug --nmbug status |& grep '^\(prefix\|repository\)' | notmuch_dir_sanitize > OUTPUT
+cat <<EOF > EXPECTED
+prefix = notmuch::
+repository = CWD/home/.nmbug
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "invoke as nmbug sets defaults"
+"$NOTMUCH_BUILDDIR"/nmbug -ldebug status |& grep '^\(prefix\|repository\)' | notmuch_dir_sanitize > OUTPUT
+cat <<EOF > EXPECTED
+prefix = notmuch::
+repository = CWD/home/.nmbug
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "env variable NOTMUCH_GIT_DIR works when invoked as nmbug"
+NOTMUCH_GIT_DIR=`pwd`/foo "$NOTMUCH_BUILDDIR"/nmbug -ldebug status |& grep '^repository' | notmuch_dir_sanitize > OUTPUT
+cat <<EOF > EXPECTED
+repository = CWD/foo
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "env variable NOTMUCH_GIT_DIR works when invoked as 'notmuch git'"
+NOTMUCH_GIT_DIR=`pwd`/remote.git notmuch git -ldebug status |& grep '^repository' | notmuch_dir_sanitize > OUTPUT
+cat <<EOF > EXPECTED
+repository = CWD/remote.git
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "env variable NOTMUCH_GIT_PREFIX works when invoked as nmbug"
+NOTMUCH_GIT_PREFIX=foo:: "$NOTMUCH_BUILDDIR"/nmbug -ldebug status |& grep '^prefix' | notmuch_dir_sanitize > OUTPUT
+cat <<EOF > EXPECTED
+prefix = foo::
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "env variable NOTMUCH_GIT_PREFIX works when invoked as 'notmuch git'"
+NOTMUCH_GIT_PREFIX=env:: notmuch git -ldebug status |& grep '^prefix' | notmuch_dir_sanitize > OUTPUT
+cat <<EOF > EXPECTED
+prefix = env::
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+
+test_begin_subtest "init, xdg default location"
+repo=home/.local/share/notmuch/default/git
+notmuch git -ldebug init |& grep '^repository' | notmuch_dir_sanitize > OUTPUT
+git -C $repo rev-parse --absolute-git-dir | notmuch_dir_sanitize >> OUTPUT
+cat <<EOF > EXPECTED
+repository = CWD/$repo
+CWD/$repo
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "init, xdg default location, with profile"
+repo=home/.local/share/notmuch/work/git
+NOTMUCH_PROFILE=work notmuch git -ldebug init |& grep '^repository' | notmuch_dir_sanitize > OUTPUT
+git -C $repo rev-parse --absolute-git-dir | notmuch_dir_sanitize >> OUTPUT
+cat <<EOF > EXPECTED
+repository = CWD/$repo
+CWD/$repo
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
test_done