Skip to content

Commit f604b7a

Browse files
committed
fix: accounts: a tag: query only matches account tags, not posting tags
Eg, `hledger accounts tag:t` lists only account a from this journal: account a ; t: 2025-01-01 a 1 b -1 ; t:
1 parent aabc892 commit f604b7a

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

hledger-lib/Hledger/Data/Journal.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ module Hledger.Data.Journal (
9898
journalAccountTypes,
9999
journalAddAccountTypes,
100100
journalPostingsAddAccountTags,
101+
journalPostingsKeepAccountTagsOnly,
101102
defaultBaseConversionAccount,
102103
-- journalPrices,
103104
journalBaseConversionAccount,
@@ -114,7 +115,7 @@ module Hledger.Data.Journal (
114115
-- * Tests
115116
samplejournal,
116117
samplejournalMaybeExplicit,
117-
tests_Journal
118+
tests_Journal,
118119
--
119120
)
120121
where
@@ -642,6 +643,13 @@ journalPostingsAddAccountTags :: Journal -> Journal
642643
journalPostingsAddAccountTags j = journalMapPostings addtags j
643644
where addtags p = p `postingAddTags` (journalInheritedAccountTags j $ paccount p)
644645

646+
-- | Remove all tags from the journal's postings except those provided by their account.
647+
-- This is useful for the accounts report.
648+
-- It does not remove tag declarations from the posting comments.
649+
journalPostingsKeepAccountTagsOnly :: Journal -> Journal
650+
journalPostingsKeepAccountTagsOnly j = journalMapPostings keepaccounttags j
651+
where keepaccounttags p = p{ptags=[]} `postingAddTags` (journalInheritedAccountTags j $ paccount p)
652+
645653
-- | The account name to use for conversion postings generated by --infer-equity.
646654
-- This is the first account declared with type V/Conversion,
647655
-- or otherwise the defaultBaseConversionAccount (equity:conversion).

hledger/Hledger/Cli/Commands/Accounts.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ accounts opts@CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=query,_r
6262
types = boolopt "types" rawopts
6363
positions = boolopt "positions" rawopts
6464
directives = boolopt "directives" rawopts
65+
-- Modified queries. These may not work with boolean queries (#2371).
6566
-- a depth limit will clip and exclude account names later, but we don't want to exclude accounts at this stage
6667
nodepthq = dbg4 "nodepthq" $ filterQuery (not . queryIsDepth) query
6768
-- just the acct: part of the query will be reapplied later, after clipping
6869
acctq = dbg4 "acctq" $ filterQuery queryIsAcct query
6970
dep = dbg4 "depth" $ queryDepth $ filterQuery queryIsDepth query
70-
matchedused = dbg5 "matchedused" $ nub $ map paccount $ journalPostings $ filterJournalPostings nodepthq j
71+
-- when finding accounts used by postings, we remove tags that were declared on the posting,
72+
-- so that a tag: query will match account tags and not posting tags.
73+
matchedused = dbg5 "matchedused" $ nub $ map paccount $ journalPostings $
74+
filterJournalPostings nodepthq $ journalPostingsKeepAccountTagsOnly j
7175
matcheddeclared = dbg5 "matcheddeclared" $
7276
nub $
7377
filter (matchesAccountExtra (journalAccountType j) (journalInheritedAccountTags j) nodepthq) $

hledger/test/accounts.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,14 @@ trade:A-B:A
9292
trade:A-B:B
9393
a
9494
b
95+
96+
# ** 11. A tag: query only matches account tags, not posting tags.
97+
<
98+
account a ; t:
99+
100+
2025-01-01
101+
a 1
102+
b -1 ; t:
103+
104+
$ hledger -f- accounts tag:t
105+
a

0 commit comments

Comments
 (0)