Skip to content

Commit c3e2d18

Browse files
committed
setup_reflog_action: document the rules for using GIT_REFLOG_ACTION
The set_reflog_action helper (in git-sh-setup) is designed to be used once at the very top of a program, like this in "git am", for example: set_reflog_action am The helper function sets the given string to GIT_REFLOG_ACTION only when GIT_REFLOG_ACTION is not yet set. Thanks to this, "git am", when run as the top-level program, will use "am" in GIT_REFLOG_ACTION and the reflog entries made by whatever it does will record the updates of refs done by "am". Because of the conditional assignment, when "git am" is run as a subprogram (i.e. an implementation detail) of "git rebase" that already sets GIT_REFLOG_ACTION to its own name, the call in "git am" to the helper function at the beginning will *not* have any effect. So "git rebase" can do this: set_reflog_action rebase ... do its own preparation, like checking out "onto" commit ... decide to do "format-patch" to "am" pipeline git format-patch --stdout >mbox git am mbox and the reflog entries made inside "git am" invocation will say "rebase", not "am". Calls to "git" commands that update refs would use GIT_REFLOG_ACTION to record who did that update. Most such calls in scripted Porcelains do not define custom reflog message and rely on GIT_REFLOG_ACTION to contain its (or its caller's, when it is called as a subprogram) name. If a scripted Porcelain wants to record a custom reflog message for a single invocation of "git" command (e.g. when "git rebase" uses "git checkout" to detach HEAD at the commit a series is to be replayed on), it needs to set GIT_REFLOG_ACTION to the custom message and export it while calling the "git" command, but such an assignment must be restricted to that single "git" invocation and should not be left behind to affect later codepath. Document the rules to avoid future confusion. Signed-off-by: Junio C Hamano <[email protected]>
1 parent b3b8ceb commit c3e2d18

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

Documentation/git-sh-setup.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ usage::
4141
die with the usage message.
4242

4343
set_reflog_action::
44-
set the message that will be recorded to describe the
45-
end-user action in the reflog, when the script updates a
46-
ref.
44+
Set GIT_REFLOG_ACTION environment to a given string (typically
45+
the name of the program) unless it is already set. Whenever
46+
the script runs a `git` command that updates refs, a reflog
47+
entry is created using the value of this string to leave the
48+
record of what command updated the ref.
4749

4850
git_editor::
4951
runs an editor of user's choice (GIT_EDITOR, core.editor, VISUAL or

Documentation/git.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,16 @@ GIT_LITERAL_PATHSPECS::
841841
literal paths to Git (e.g., paths previously given to you by
842842
`git ls-tree`, `--raw` diff output, etc).
843843

844+
'GIT_REFLOG_ACTION'::
845+
When a ref is updated, reflog entries are created to keep
846+
track of the reason why the ref was updated (which is
847+
typically the name of the high-level command that updated
848+
the ref), in addition to the old and new values of the ref.
849+
A scripted Porcelain command can use set_reflog_action
850+
helper function in `git-sh-setup` to set its name to this
851+
variable when it is invoked as the top level command by the
852+
end user, to be recorded in the body of the reflog.
853+
844854

845855
Discussion[[Discussion]]
846856
------------------------

git-sh-setup.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,40 @@ $LONG_USAGE"
103103
esac
104104
fi
105105
106+
# Set the name of the end-user facing command in the reflog when the
107+
# script may update refs. When GIT_REFLOG_ACTION is already set, this
108+
# will not overwrite it, so that a scripted Porcelain (e.g. "git
109+
# rebase") can set it to its own name (e.g. "rebase") and then call
110+
# another scripted Porcelain (e.g. "git am") and a call to this
111+
# function in the latter will keep the name of the end-user facing
112+
# program (e.g. "rebase") in GIT_REFLOG_ACTION, ensuring whatever it
113+
# does will be record as actions done as part of the end-user facing
114+
# operation (e.g. "rebase").
115+
#
116+
# NOTE NOTE NOTE: consequently, after assigning a specific message to
117+
# GIT_REFLOG_ACTION when calling a "git" command to record a custom
118+
# reflog message, do not leave that custom value in GIT_REFLOG_ACTION,
119+
# after you are done. Other callers of "git" commands that rely on
120+
# writing the default "program name" in reflog expect the variable to
121+
# contain the value set by this function.
122+
#
123+
# To use a custom reflog message, do either one of these three:
124+
#
125+
# (a) use a single-shot export form:
126+
# GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz" \
127+
# git command-that-updates-a-ref
128+
#
129+
# (b) save the original away and restore:
130+
# SAVED_ACTION=$GIT_REFLOG_ACTION
131+
# GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz"
132+
# git command-that-updates-a-ref
133+
# GIT_REFLOG_ACITON=$SAVED_ACTION
134+
#
135+
# (c) assign the variable in a subshell:
136+
# (
137+
# GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz"
138+
# git command-that-updates-a-ref
139+
# )
106140
set_reflog_action() {
107141
if [ -z "${GIT_REFLOG_ACTION:+set}" ]
108142
then

0 commit comments

Comments
 (0)