简体   繁体   中英

Get “git log --tags” of temporarily added remote

I have this repo which is basically a list of other repos. What I want to do is to run a script that loops over a list of urls and fetch information about the date of their last release (tag).

<for each url in my list>
git remote remove tmp
git remote add tmp https://url-to-another-repo.git
git fetch tmp master
git log tmp/master --tags="*" --max-count=1 --pretty="Last updated %ar"
<next url>

That works kinda ok but not the way I want. I am having trouble in getting git log to only fetch tags. Instead it also fetches the last commits.

When I clone the same repo

git clone https://url-to-another-repo.git
git log --tags="*" --max-count=1 --pretty="Last updated %ar"

It shows the right result - that is: only the last tagged commit.

--tags="*" does not do what you think: it does as if you listed all tags on the command line; so:

git log --tags="*"
# is roughly equivalent to
git log last-stable my/tag v1.0.1 v1.0.2  # whatever tags you have on your repo

git log --tags="*" tmp/master
# is roughly equivalent to
git log tmp/master last-stable my/tag v1.0.1 v1.0.2
  • in the first case: the first commit (the one you will get by adding the -1 option) will always be tagged, but you won't know if that tag is part of current branch,

  • in the second case, if tmp/master is the commit that comes on top, it won't be tagged, and if a tag happened to be ahead of tmp/master , again, you wouldn't know if that tag is part of the tmp/master branch.


  1. Using git log , you may try to combine --simplify-by-decoration with --decorate-refs=<pattern> :
git log --decorate-refs="refs/tags" --simplify-by-decoration

# you can also filter tags using patterns :
# will list tags "v1.0.1" and "v2.0.3", but not "testing"
git log --decorate-refs="refs/tags/v*" --simplify-by-decoration

If you have tags on forked branches, --simplify-by-decoration will also keep the merge commits in the list of commits; if you are looking for tags only on the "main" branch, add --first-parent :

git log --first-parent --decorate-refs="refs/tags" --simplify-by-decoration
  1. Another option is git describe :
# this will give you "the last tag on the branch" (for some definition of "last tag")
git describe --tags --abbrev=0 tmp/master

# you can combine it with 'git log' :
git log -1 $(git describe --tags --abbrev=0 tmp/master)

LeGEC answer was what solved my problem and should be the correct answer.

This was my final code.

#!/bin/bash
#
# Outputs a markdown table of the repos and their status listed in a csv list (;) with format
#
# Name1;url1;branch1;Description1;
# Name2;url2;branch2;Description2;

FunctionProcessRepo () {
    git remote remove tmp
    git remote add tmp $2
    git fetch tmp $3
    local last_tag=$(git describe --tags --abbrev=0 tmp/$3)
    local tag_flag="%S"  # use pretty-format %s (commit header) if no tags
    
    # If no tag found, use last commit
    if [ -z "$last_tag" ]
    then
        local tag_flag="%s"
        local last_tag="tmp/$3"
    fi

    # Fetch info
    local last_update=$(git log -1 $last_tag --pretty="format:%as")
    local last_update_relative=$(git log -1 $last_tag --pretty="format:%ar")
    local release_name=$(git log -1 $last_tag --pretty="format:$tag_flag")

    # Write to table
    echo "| [$1]($2) | $4 | $3 | $release_name | $last_update |" >> $file_output
}

file_input="repo-list.txt"
file_output="repo-table.md"

# Table header in markdown format
echo "| Name | Description | Branch | Last release/commit | Date |" > $file_output
echo "| ------- | ----------- | ------- | ----------- | ----------- |" >> $file_output

# Read from repo-list.txt
while IFS=";" read -r repo_name repo_url repo_branch repo_description end_of_line
do
    FunctionProcessRepo "$repo_name" $repo_url $repo_branch "$repo_description"
done < $file_input

it outputs a table like this:

Name Description Branch Last release/commit Date
Repo 1 A repo with no tags master Updated Readme.md 2021-01-13
Repo 2 A repo with tags master ver1.1 2021-02-16
Repo 3 A repo with tags master R2020.1 2021-02-16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM