1

I want to fetch code using a particular SHA-id.This is what I am trying

- git: 
        repo: http://<git-url>/Vara-Internal/mongo-db-dev.git
        dest: "{{ app_path }}/{{ app_dir }}"
        version: "{{ GIT_TAG }}"
        refspec: '+refs/heads/{{ GIT_TAG }}:refs/remotes/origin/{{ GIT_TAG }}'
        update: yes
        force: true
      register: cloned 

It throwing me this error

FAILED! => {"changed": false, "cmd": ["/usr/bin/git", "fetch", "--tags", "origin", "+refs/heads/dbde451f203a112d0838fb09bc19ed28bd231e6e:refs/remotes/origin/dbde451f203a112d0838fb09bc19ed28bd231e6e"], "failed": true, "msg": "Failed to download remote objects and refs:  fatal: Couldn't find remote ref refs/heads/dbde451f203a112d0838fb09bc19ed28bd231e6e\n"}
Atul Agrawal
  • 1,208
  • 3
  • 16
  • 35

2 Answers2

2

(Note: I know very little about ansible, being just a casual user.)

Most (but not all) Git servers will not let you retrieve objects by hash IDs at all. Most servers require that the object have a name, such as refs/heads/master (branch master) or refs/tags/v1.1 (the tag named v1.1).

If a server does allow retrieval by hash ID (see VonC's answer here), you must not qualify the hash ID: it's not refs/heads/dbde451f203a112d0838fb09bc19ed28bd231e6e bit rather simply dbde451f203a112d0838fb09bc19ed28bd231e6e. That would become the src part of a src:dst refspec.

Typically you would use a tag name here, not a hash ID, but if your server does allow retrieval by hash ID, the refspec line would logically have to read:

refspec: '+{{ GIT_TAG }}:refs/remotes/origin/some-particular-name'

as it is not a good idea to use hash IDs as names (for several reasons, the main one being that you will confuse humans).

torek
  • 330,127
  • 43
  • 437
  • 552
  • could you please explain what is the use + sign in refspec? – Atul Agrawal Jul 07 '17 at 17:20
  • The leading `+` means the same as `--force`: overwrite the destination reference even if the normal rules would disallow this. – torek Jul 07 '17 at 17:25
  • Actually i need this to revert out changes in last deployment.and when i used +{{ GIT_TAG }}:refs/heads/{{ GIT_TAG }}:refs/remotes/origin/{{ GIT_TAG }} it throwing me Invalid refspec error. – Atul Agrawal Jul 07 '17 at 17:27
  • It's possible that ansible is overly picky about the form of refspec, but it's *far* more likely that your server *does not accept hash IDs*. – torek Jul 07 '17 at 17:29
  • Thanks for your response, but i took secondary way to do the same. – Atul Agrawal Jul 07 '17 at 17:57
1

I used indirect method to the same.I have save commit ids in a file and added following code

- name: Reverting Changes
      command: "git reset --hard {{ GIT_TAG }}"
      args:
        chdir: "{{ app_path }}/{{ app_dir }}"
      when: build_type  == "revert"

i.e. I took an extra variable which is build_type and checked whether this is for revert the changes.If it is then i read the commit id from my git information file and hard reset my branch.

Atul Agrawal
  • 1,208
  • 3
  • 16
  • 35