Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9157257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:58:14+00:00 2026-06-17T12:58:14+00:00

I would like to automatically format my files before they are added to Git’s

  • 0

I would like to automatically format my files before they are added to Git’s index. Right now, I have a pre-commit hook that looks like this:

#!/bin/bash

set -e
exec 1>&2

workingext="working.$$"
ext="bak.$$"
git diff -z --cached --name-only | egrep -z '\.(pl|pm|t)$' | \
        while read -d'' -r f; do
    # First modify the file in the index
    mv "$f" "$f.$workingext"
    git show :"$f" > "$f"
    perl -c "$f"
    perltidy -pbp -nst -b -bext="$ext" "$f";
    rm -f "$f.$ext"
    git add "$f"
    mv "$f.$workingext" "$f"
    # Then the working copy
    perl -c "$f"
    perltidy -pbp -nst -b -bext="$ext" "$f";
    rm -f "$f.$ext"
done

Basically, I back up the working copies, check out the indexed copies, format them, add them to the index, and then restore the working copies and format them too, so that the differences between the working and indexed copies don’t grow too large. I check the syntax of the files using perl -c first so that the formatter perltidy can’t get too confused by a syntactically invalid file.

So far, this seems to be working pretty well. However, I’d like to know if there’s a way to add the contents of one file to the index under the name of another file. That is, a command like git update-index-from-file --from-file foo.pl.tmp foo.pl would update the file foo.pl such that its contents in the index came entirely from foo.pl.tmp, but whose version in the working directory remained unmodified. This might avoid the dance of renamings I’m doing right now (although that might be useful to ensure I don’t lose the contents of the index irretrievably).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:58:15+00:00Added an answer on June 17, 2026 at 12:58 pm

    Yes, that’s possible. First, you need to create a blob from your file by running this command:

    git hash-object -w foo.pl.tmp
    

    See here: http://www.kernel.org/pub/software/scm/git/docs/git-hash-object.html

    Computes the object ID value for an object with specified type with the contents of the named file (which can be outside of the work tree), and optionally writes the resulting object into the object database. Reports its object ID to its standard output.

    Using the blob sha that git hash-object sent to STDOUT, you can now add this blob as “foo.pl” to your index by running

    git update-index --add --cacheinfo 100644 *YOURSHA* foo.pl
    

    From the manpage of update-index:

    –cacheinfo is used to register a file that is not in the current working directory. This is useful for minimum-checkout merging.

    To pretend you have a file with mode and sha1 at path, say:

    $ git update-index --cacheinfo mode sha1 path
    

    But I’d like to note that your situation looks a lot like you should actually use a smudge filter to do that stuff. See here to learn about them: https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#Keyword-Expansion

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have that the height of a canvas change automatically. When
I have a java.hprof.txt file (automatically generated after an OutOfMemoryError) which I would like
I would like to automatically generate unit testing report in html format for Android
I have this bit on codebehind: txtDBirth.Text = DateTime.Now.ToString(Mon dd yyyy) We would like
I'm writing a forum-type discussion board in Perl and would like to change automatically
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to build a php script that automatically generates a new id
We would like to implement a web form that automatically saves content at regular
I would like to change a program to automatically detect whether a terminal is
I would like to know if there are any tools to automatically generate EJB3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.