Go Help

Mercurial in Gedit

Introduction

In this chapter we will make the following tools for Gedit with Mercurial:

  1. hgAdd
  2. hgCommit
  3. hgStatus
  4. hgLog
  5. hgRollback

First, I suggest that you add the terminal tab at the bottom of Gedit, if you have not done so already (see Plugins Chapter).  With this you can quickly execute Mercurial commands like hg commit -m ‘message’ within Gedit, rather than keep a terminal window open.  This is a good way to start to learn Mercurial.

Next, we will make some External Tools to automate the most common tasks.  If you have not done so already, look at  Go Format, Go Build, etc. to learn about the basics of External Tools.  I will use the short version to describe these new tools.  The External Tools below are set to act on the file in your current edit tab, where applicable.  If you want them to act on all the files in the current scope of Mercurial, you can easily change them.  In another chapter, I will write some scripts for Nautilus File manager to work on groups of files.

I assume that you have followed  ‘Mercurial Version Control – The Basics’ and created a Project directory with hg init, then, created or copied a Go source file into that directory,

Create hgAdd External Tool

  1. Open Gedit and Go to Tools>Manage External Tools.
  2. Create a new tool named, hgAdd
  3. Copy and Paste the script below into the code panel, overwriting the existing code.
  4. Assign a short cut key, if you wish.
  5. Make the other settings as documented in ‘Tool Settings’ in the script below.
  6. Set the Language to Go, Plain Text and sh.

#!/bin/bash

# hgAdd – Executes Mercurial hg add on file in current tab
#
#    Manual – http://www.gohelp.wordpress.com – ‘Gedit – An IDE for Go’
#—————————————————————
#   *Tool Settings: Save = Current Document, Input = Nothing,
#                   OutPut = Display in Bottom Pane,
#                   Language = Go, Plain Text, sh
#

DIR=”${GEDIT_CURRENT_DOCUMENT_DIR}”
DOCPATH=”${GEDIT_CURRENT_DOCUMENT_PATH//%20/ }”

echo Current Directory “${DIR}”

hg add -v “${DOCPATH}”

Test

  1. I assume that you have followed  ‘Mercurial Version Control – The Basics’ and created a Project directory with hg init,
  2. then, created or copied a Go source file into that directory,
  3. Now, open that file with Gedit.
  4. Go to Tools>External Tools>hgAdd.
  5. The bottom pane should show the name of the current directory, followed by the name of the file you added to Mercurial.

Create hgCommit External Tool

  1. Make sure that you have Zenity installed.  See the end of the Gedit Plugins Chapter.
  2. Open Gedit and Go to Tools>Manage External Tools.
  3. Create a new tool named, hgCommit
  4. Copy and Paste the script below into the code panel, overwriting the existing code.
  5. Assign a short cut key, if you wish.
  6. Make the other settings as documented in ‘Tool Settings’ in the script below.
  7. Set the Language to Go, Plain Text and sh.

#!/bin/bash

# hgCommit – Executes Mercurial hg commit on file in current tab
#
#    Manual – http://www.gohelp.wordpress.com – ‘Gedit – An IDE for Go’
#—————————————————————
#   *Tool Settings: Save = Current Document, Input = Nothing,
#                   OutPut = Display in Bottom Pane,
#                   Language = Go, Plain Text, sh
#

DOCPATH=”${GEDIT_CURRENT_DOCUMENT_PATH//%20/ }” # ex tool puts %20 for spaces

hgComment=`zenity –entry \
–title=”hg Commit” \
–text=”Enter a Comment for Commit or Cancel to stop Commit” `

okCancel=`echo $?`

if [ “${okCancel}” -eq 0 ]
then
hg commit -v -m “${hgComment}” “${DOCPATH}” # to commit current file
fi

Test

  1. I assume that you have followed the instructions for hgAdd
  2. now open that file in Gedit
  3. and Go to Tools>External Tools>hgCommit.
  4. A pop up window should appear for you to enter a comment to go in the change log.  If you change your mind about doing a commit at this point, just click on Cancel, otherwise click on OK.  A message should appear in the bottom pane with the name of the file and the change set number.

Create hgStatus External Tool

  1. Open Gedit and Go to Tools>Manage External Tools.
  2. Create a new tool named, hgStatus
  3. Copy and Paste the script below into the code panel, overwriting the existing code.
  4. Assign a short cut key, if you wish.
  5. Make the other settings as documented in ‘Tool Settings’ in the script below.
  6. Set the Language to Go, Plain Text and sh.

#!/bin/bash

# hgStatus – Executes Mercurial hg status on current project
#
#    Manual – http://www.gohelp.wordpress.com – ‘Gedit – An IDE for Go’
#—————————————————————
#   *Tool Settings: Save = Nothing, Input = Nothing,
#                   OutPut = Display in Bottom Pane,
#                   Language = Go, Plain Text, sh
#

DIR=”${GEDIT_CURRENT_DOCUMENT_DIR}”

echo Current Directory “${DIR}”

hg status -R “${DIR}”

Test

  1. I assume that you have followed the instructions for hgAdd
  2. now open that file in Gedit
  3. and Go to Tools>External Tools>hgStatus.
  4. The bottom pane should show the name of the current directory, followed by the version control status of the files in your directory.

Create hgLog External Tool

  1. Make sure that you have Zenity installed.  See the end of the Gedit Plugins Chapter.
  2. Open Gedit and Go to Tools>Manage External Tools.
  3. Create a new tool named, hgLog
  4. Copy and Paste the script below into the code panel, overwriting the existing code.
  5. Assign a short cut key, if you wish.
  6. Make the other settings as documented in ‘Tool Settings’ in the script below.
  7. Set the Language to Go, Plain Text and sh.

#!/bin/bash

# hgLog – Mercurial Show log of changes
#
#    Manual – http://www.gohelp.wordpress.com – ‘Gedit – An IDE for Go’
#————————————————————————-
#   *Tool Settings: Save = Nothing, Input = Nothing,
#                   OutPut = see below,
#                   Language = Go, Plain Text, sh
#
#           Set Tool Output to:
#
#OUTPUT=1  Bottom Pane or Create New Document – real-time, Display
#OUTPUT=2  Bottom Pane – for script error     – save to file, Popup Window
#————————————————————————–
OUTPUT=2    # Set OUTPUT Switch

DIR=”${GEDIT_CURRENT_DOCUMENT_DIR}”
DOCPATH=”${GEDIT_CURRENT_DOCUMENT_PATH//%20/ }” # ex tool puts %20 for spaces

if  ! ( test -z “$MyTemp” ) # if MyTemp exists in environment
then
TMPDIR=”${MyTemp}”
else
TMPDIR=”${DIR}”  # else set TMPDIR to current document directory
fi

case $OUTPUT in
1) hg log -R “${DIR}”
echo “Current change set = ” $(hg identify -n)
;;
2) hg log -R “${DIR}” > “${TMPDIR}”/hglog.tmp
echo “Current change set = ” $(hg identify -n) >> “${TMPDIR}”/hglog.tmp

nLines=`wc -l “${TMPDIR}”/hglog.tmp|awk ‘{print $1}’`
if [ “${nLines}” -gt 28 ]
then
nLines=28
fi
zHeight=$(( nLines * 25 ))

longLine=`wc -L “${TMPDIR}”/hglog.tmp|awk ‘{print $1}’`
if [ “${longLine}” -gt 80 ]
then
longLine=80
fi
zWidth=$(( longLine * 9 ))

zenity –text-info \
–height=${zHeight} \
–width=${zWidth} \
–title=”Mercurial Log” \
–filename=”${TMPDIR}”/hglog.tmp \ &
sleep 1
rm “${TMPDIR}”/hglog.tmp
;;

*) echo “Please Set hgLog OUTPUT option”
esac

Test

  1. I assume that you have followed the instructions for hgCommit
  2. now open that file in Gedit
  3. and Go to Tools>External Tools>hgLog.
  4. Either a new edit tab or a pop up window should show the current changeset log with the number of the current changeset at the bottom.

Usage example.  Run hgLog External Tool.  The changeset log comes up in a new tab or a window, with the number of the current changeset at the bottom, you can look at the changes and go to a previous state by going to the terminal in the bottom panel and typing, hg update ‘number of changeset’.

Create hgRollback External Tool

  1. Open Gedit and Go to Tools>Manage External Tools.
  2. Create a new tool named, hgRollback
  3. Copy and Paste the script below into the code panel, overwriting the existing code.
  4. Assign a short cut key, if you wish.
  5. Make the other settings as documented in ‘Tool Settings’ in the script below.
  6. Set the Language to Go, Plain Text and sh.

#!/bin/sh

# hgRollback – Executes Mercurial hg rollback on current directory
#
#    Manual – http://www.gohelp.wordpress.com – ‘Gedit – An IDE for Go’
#—————————————————————
#   *Tool Settings: Save = Nothing, Input = Nothing,
#                   OutPut = Display in Bottom Pane,
#                   Language = Go, Plain Text, sh
#

DIR=”${GEDIT_CURRENT_DOCUMENT_DIR}”

echo “Previous change set = ” $(hg identify -n)

hg rollback -R “${DIR}”
echo “Current change set = ” $(hg identify -n)

Test

  1. I assume that you have followed the instructions for hgCommit
  2. now open that file in Gedit
  3. and Go to Tools>External Tools>hgRollback.
  4. The bottom pane should show the number for the previous changeset, the rolling back message, and the number for the new changeset.

Create a free website or blog at WordPress.com.

%d bloggers like this: