Execute a Git command without being in the repository and outputting the current directory











up vote
1
down vote

favorite












I'm writing a Python script to loop over all Git repositories in a certain folder. Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.



Is there a way to print out a current directory if you execute Git commands against a repository without being in that repository?



My current git-log command looks like this:



git -C ./%s log --pretty=format:-C,'"%%H","%%s"' | grep -E %s >> output.csv


I know I can use both git --git-dir=repo/.git log and git -C /repo log to execute commands in subfolders.



I also tried to use $(basename "$PWD") but it shows the current folder, but not subfolders.



Any idea on how to do this?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I'm writing a Python script to loop over all Git repositories in a certain folder. Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.



    Is there a way to print out a current directory if you execute Git commands against a repository without being in that repository?



    My current git-log command looks like this:



    git -C ./%s log --pretty=format:-C,'"%%H","%%s"' | grep -E %s >> output.csv


    I know I can use both git --git-dir=repo/.git log and git -C /repo log to execute commands in subfolders.



    I also tried to use $(basename "$PWD") but it shows the current folder, but not subfolders.



    Any idea on how to do this?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm writing a Python script to loop over all Git repositories in a certain folder. Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.



      Is there a way to print out a current directory if you execute Git commands against a repository without being in that repository?



      My current git-log command looks like this:



      git -C ./%s log --pretty=format:-C,'"%%H","%%s"' | grep -E %s >> output.csv


      I know I can use both git --git-dir=repo/.git log and git -C /repo log to execute commands in subfolders.



      I also tried to use $(basename "$PWD") but it shows the current folder, but not subfolders.



      Any idea on how to do this?










      share|improve this question













      I'm writing a Python script to loop over all Git repositories in a certain folder. Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.



      Is there a way to print out a current directory if you execute Git commands against a repository without being in that repository?



      My current git-log command looks like this:



      git -C ./%s log --pretty=format:-C,'"%%H","%%s"' | grep -E %s >> output.csv


      I know I can use both git --git-dir=repo/.git log and git -C /repo log to execute commands in subfolders.



      I also tried to use $(basename "$PWD") but it shows the current folder, but not subfolders.



      Any idea on how to do this?







      python git github






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 11:19









      Lowly0palace

      183




      183
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          From what I understand from your question, you want to add the current git repo name with each line from git log.



          Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log command with subprocess.Popen(), and print out each line with the repos name from stdout.



          Make sure to pip install GitPython before running this code.



          Here is an example:



          from os import listdir
          from os import chdir
          from os import getcwd

          from os.path import abspath

          from git import Repo
          from git import InvalidGitRepositoryError

          from subprocess import Popen
          from subprocess import PIPE

          # Current working directory with all git repositories
          # You can change this path to your liking
          ROOT_PATH = getcwd()

          # Go over each file in current working directory
          for file in listdir(ROOT_PATH):
          full_path = abspath(file)

          # Check if file is a git repository
          try:
          Repo(full_path)

          # Change to directory
          chdir(full_path)

          # Run git log command
          with Popen(
          args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
          shell=False,
          stdout=PIPE,
          bufsize=1,
          universal_newlines=True,
          ) as process:

          # Print out each line from stdout with repo name
          for line in process.stdout:
          print('%s %s' % (file, line.strip()))

          # Change back to path
          chdir(ROOT_PATH)

          # If we hit here, file is not a git repository
          except InvalidGitRepositoryError:
          continue


          This works for me when I run the script inside a folder with all my git repositories.



          Note: There is probably a nicer way to do this with the git command itself or with bash.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373523%2fexecute-a-git-command-without-being-in-the-repository-and-outputting-the-current%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            From what I understand from your question, you want to add the current git repo name with each line from git log.



            Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log command with subprocess.Popen(), and print out each line with the repos name from stdout.



            Make sure to pip install GitPython before running this code.



            Here is an example:



            from os import listdir
            from os import chdir
            from os import getcwd

            from os.path import abspath

            from git import Repo
            from git import InvalidGitRepositoryError

            from subprocess import Popen
            from subprocess import PIPE

            # Current working directory with all git repositories
            # You can change this path to your liking
            ROOT_PATH = getcwd()

            # Go over each file in current working directory
            for file in listdir(ROOT_PATH):
            full_path = abspath(file)

            # Check if file is a git repository
            try:
            Repo(full_path)

            # Change to directory
            chdir(full_path)

            # Run git log command
            with Popen(
            args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
            shell=False,
            stdout=PIPE,
            bufsize=1,
            universal_newlines=True,
            ) as process:

            # Print out each line from stdout with repo name
            for line in process.stdout:
            print('%s %s' % (file, line.strip()))

            # Change back to path
            chdir(ROOT_PATH)

            # If we hit here, file is not a git repository
            except InvalidGitRepositoryError:
            continue


            This works for me when I run the script inside a folder with all my git repositories.



            Note: There is probably a nicer way to do this with the git command itself or with bash.






            share|improve this answer



























              up vote
              3
              down vote



              accepted










              From what I understand from your question, you want to add the current git repo name with each line from git log.



              Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log command with subprocess.Popen(), and print out each line with the repos name from stdout.



              Make sure to pip install GitPython before running this code.



              Here is an example:



              from os import listdir
              from os import chdir
              from os import getcwd

              from os.path import abspath

              from git import Repo
              from git import InvalidGitRepositoryError

              from subprocess import Popen
              from subprocess import PIPE

              # Current working directory with all git repositories
              # You can change this path to your liking
              ROOT_PATH = getcwd()

              # Go over each file in current working directory
              for file in listdir(ROOT_PATH):
              full_path = abspath(file)

              # Check if file is a git repository
              try:
              Repo(full_path)

              # Change to directory
              chdir(full_path)

              # Run git log command
              with Popen(
              args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
              shell=False,
              stdout=PIPE,
              bufsize=1,
              universal_newlines=True,
              ) as process:

              # Print out each line from stdout with repo name
              for line in process.stdout:
              print('%s %s' % (file, line.strip()))

              # Change back to path
              chdir(ROOT_PATH)

              # If we hit here, file is not a git repository
              except InvalidGitRepositoryError:
              continue


              This works for me when I run the script inside a folder with all my git repositories.



              Note: There is probably a nicer way to do this with the git command itself or with bash.






              share|improve this answer

























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                From what I understand from your question, you want to add the current git repo name with each line from git log.



                Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log command with subprocess.Popen(), and print out each line with the repos name from stdout.



                Make sure to pip install GitPython before running this code.



                Here is an example:



                from os import listdir
                from os import chdir
                from os import getcwd

                from os.path import abspath

                from git import Repo
                from git import InvalidGitRepositoryError

                from subprocess import Popen
                from subprocess import PIPE

                # Current working directory with all git repositories
                # You can change this path to your liking
                ROOT_PATH = getcwd()

                # Go over each file in current working directory
                for file in listdir(ROOT_PATH):
                full_path = abspath(file)

                # Check if file is a git repository
                try:
                Repo(full_path)

                # Change to directory
                chdir(full_path)

                # Run git log command
                with Popen(
                args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
                shell=False,
                stdout=PIPE,
                bufsize=1,
                universal_newlines=True,
                ) as process:

                # Print out each line from stdout with repo name
                for line in process.stdout:
                print('%s %s' % (file, line.strip()))

                # Change back to path
                chdir(ROOT_PATH)

                # If we hit here, file is not a git repository
                except InvalidGitRepositoryError:
                continue


                This works for me when I run the script inside a folder with all my git repositories.



                Note: There is probably a nicer way to do this with the git command itself or with bash.






                share|improve this answer














                From what I understand from your question, you want to add the current git repo name with each line from git log.



                Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log command with subprocess.Popen(), and print out each line with the repos name from stdout.



                Make sure to pip install GitPython before running this code.



                Here is an example:



                from os import listdir
                from os import chdir
                from os import getcwd

                from os.path import abspath

                from git import Repo
                from git import InvalidGitRepositoryError

                from subprocess import Popen
                from subprocess import PIPE

                # Current working directory with all git repositories
                # You can change this path to your liking
                ROOT_PATH = getcwd()

                # Go over each file in current working directory
                for file in listdir(ROOT_PATH):
                full_path = abspath(file)

                # Check if file is a git repository
                try:
                Repo(full_path)

                # Change to directory
                chdir(full_path)

                # Run git log command
                with Popen(
                args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
                shell=False,
                stdout=PIPE,
                bufsize=1,
                universal_newlines=True,
                ) as process:

                # Print out each line from stdout with repo name
                for line in process.stdout:
                print('%s %s' % (file, line.strip()))

                # Change back to path
                chdir(ROOT_PATH)

                # If we hit here, file is not a git repository
                except InvalidGitRepositoryError:
                continue


                This works for me when I run the script inside a folder with all my git repositories.



                Note: There is probably a nicer way to do this with the git command itself or with bash.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 19 at 14:23

























                answered Nov 19 at 12:12









                RoadRunner

                9,14331138




                9,14331138






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373523%2fexecute-a-git-command-without-being-in-the-repository-and-outputting-the-current%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga